Commit | Line | Data |
---|---|---|
b8585c7c AM |
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.NonNull; | |
19 | import org.eclipse.jdt.annotation.Nullable; | |
20 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; | |
b1aad44e | 21 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
35f39420 | 22 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
b8585c7c AM |
23 | |
24 | /** | |
25 | * Utility methods for ITmfTrace's. | |
26 | * | |
27 | * @author Alexandre Montplaisir | |
28 | */ | |
29 | public final class TmfTraceUtils { | |
30 | ||
b1aad44e GB |
31 | private TmfTraceUtils() { |
32 | } | |
b8585c7c AM |
33 | |
34 | /** | |
35 | * Get an analysis module belonging to this trace, with the specified ID and | |
36 | * class. | |
37 | * | |
38 | * @param trace | |
39 | * The trace for which you want the modules | |
40 | * @param moduleClass | |
41 | * Returned modules must extend this class | |
42 | * @param id | |
43 | * The ID of the analysis module | |
44 | * @return The analysis module with specified class and ID, or null if no | |
45 | * such module exists. | |
46 | */ | |
47 | public static @Nullable <T extends IAnalysisModule> T getAnalysisModuleOfClass(ITmfTrace trace, | |
48 | Class<T> moduleClass, String id) { | |
49 | Iterable<T> modules = getAnalysisModulesOfClass(trace, moduleClass); | |
50 | for (T module : modules) { | |
51 | if (id.equals(module.getId())) { | |
52 | return module; | |
53 | } | |
54 | } | |
55 | return null; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Return the analysis modules that are of a given class. Module will be | |
60 | * casted to the requested class. | |
61 | * | |
62 | * @param trace | |
63 | * The trace for which you want the modules | |
64 | * @param moduleClass | |
65 | * Returned modules must extend this class | |
66 | * @return List of modules of class moduleClass | |
67 | */ | |
68 | public static @NonNull <T> Iterable<T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) { | |
69 | Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules(); | |
70 | Set<T> modules = new HashSet<>(); | |
b1aad44e | 71 | for (IAnalysisModule module : analysisModules) { |
b8585c7c AM |
72 | if (moduleClass.isAssignableFrom(module.getClass())) { |
73 | modules.add(moduleClass.cast(module)); | |
74 | } | |
75 | } | |
76 | return modules; | |
77 | } | |
35f39420 AM |
78 | |
79 | /** | |
b1aad44e GB |
80 | * Return the first result of the first aspect that resolves as non null for |
81 | * the event received in parameter. If the returned value is not null, it | |
82 | * can be safely cast to the aspect's class proper return type. | |
35f39420 AM |
83 | * |
84 | * @param trace | |
85 | * The trace for which you want the event aspects | |
86 | * @param aspectClass | |
b1aad44e GB |
87 | * The class of the aspect(s) to resolve |
88 | * @param event | |
89 | * The event for which to get the aspect | |
90 | * @return The first result of the | |
91 | * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null | |
92 | * for the event or {@code null} otherwise | |
35f39420 | 93 | */ |
b1aad44e GB |
94 | public static <T extends ITmfEventAspect> Object resolveEventAspectOfClassForEvent( |
95 | ITmfTrace trace, Class<T> aspectClass, @NonNull ITmfEvent event) { | |
35f39420 | 96 | Iterable<ITmfEventAspect> aspects = trace.getEventAspects(); |
35f39420 AM |
97 | for (ITmfEventAspect aspect : aspects) { |
98 | if (aspectClass.isAssignableFrom(aspect.getClass())) { | |
b1aad44e GB |
99 | Object obj = aspect.resolve(event); |
100 | if (obj != null) { | |
101 | return obj; | |
102 | } | |
35f39420 AM |
103 | } |
104 | } | |
b1aad44e | 105 | return null; |
35f39420 | 106 | } |
b8585c7c | 107 | } |