tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.trace;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
23
24 /**
25 * Utility methods for ITmfTrace's.
26 *
27 * @author Alexandre Montplaisir
28 */
29 @NonNullByDefault
30 public final class TmfTraceUtils {
31
32 private TmfTraceUtils() {
33 }
34
35 /**
36 * Get an analysis module belonging to this trace, with the specified ID and
37 * class.
38 *
39 * @param trace
40 * The trace for which you want the modules
41 * @param moduleClass
42 * Returned modules must extend this class
43 * @param id
44 * The ID of the analysis module
45 * @return The analysis module with specified class and ID, or null if no
46 * such module exists.
47 */
48 public static @Nullable <T extends IAnalysisModule> T getAnalysisModuleOfClass(ITmfTrace trace,
49 Class<T> moduleClass, String id) {
50 Iterable<T> modules = getAnalysisModulesOfClass(trace, moduleClass);
51 for (T module : modules) {
52 if (id.equals(module.getId())) {
53 return module;
54 }
55 }
56 return null;
57 }
58
59 /**
60 * Return the analysis modules that are of a given class. Module will be
61 * casted to the requested class.
62 *
63 * @param trace
64 * The trace for which you want the modules
65 * @param moduleClass
66 * Returned modules must extend this class
67 * @return List of modules of class moduleClass
68 */
69 public static <T> Iterable<T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) {
70 Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules();
71 Set<T> modules = new HashSet<>();
72 for (IAnalysisModule module : analysisModules) {
73 if (moduleClass.isAssignableFrom(module.getClass())) {
74 modules.add(moduleClass.cast(module));
75 }
76 }
77 return modules;
78 }
79
80 /**
81 * Return the first result of the first aspect that resolves as non null for
82 * the event received in parameter. If the returned value is not null, it
83 * can be safely cast to the aspect's class proper return type.
84 *
85 * @param trace
86 * The trace for which you want the event aspects
87 * @param aspectClass
88 * The class of the aspect(s) to resolve
89 * @param event
90 * The event for which to get the aspect
91 * @return The first result of the
92 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
93 * for the event or {@code null} otherwise
94 */
95 public static @Nullable <T extends ITmfEventAspect> Object resolveEventAspectOfClassForEvent(
96 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
97 Iterable<ITmfEventAspect> aspects = trace.getEventAspects();
98 for (ITmfEventAspect aspect : aspects) {
99 if (aspectClass.isAssignableFrom(aspect.getClass())) {
100 Object obj = aspect.resolve(event);
101 if (obj != null) {
102 return obj;
103 }
104 }
105 }
106 return null;
107 }
108 }
This page took 0.034145 seconds and 5 git commands to generate.