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 | ||
1d83ed07 | 18 | import org.eclipse.jdt.annotation.NonNullByDefault; |
b8585c7c AM |
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 | */ | |
1d83ed07 | 29 | @NonNullByDefault |
b8585c7c AM |
30 | public final class TmfTraceUtils { |
31 | ||
b1aad44e GB |
32 | private TmfTraceUtils() { |
33 | } | |
b8585c7c AM |
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 | */ | |
1d83ed07 | 69 | public static <T> Iterable<T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) { |
b8585c7c AM |
70 | Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules(); |
71 | Set<T> modules = new HashSet<>(); | |
b1aad44e | 72 | for (IAnalysisModule module : analysisModules) { |
b8585c7c AM |
73 | if (moduleClass.isAssignableFrom(module.getClass())) { |
74 | modules.add(moduleClass.cast(module)); | |
75 | } | |
76 | } | |
77 | return modules; | |
78 | } | |
35f39420 AM |
79 | |
80 | /** | |
b1aad44e GB |
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. | |
35f39420 AM |
84 | * |
85 | * @param trace | |
86 | * The trace for which you want the event aspects | |
87 | * @param aspectClass | |
b1aad44e GB |
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 | |
35f39420 | 94 | */ |
1d83ed07 AM |
95 | public static @Nullable <T extends ITmfEventAspect> Object resolveEventAspectOfClassForEvent( |
96 | ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) { | |
35f39420 | 97 | Iterable<ITmfEventAspect> aspects = trace.getEventAspects(); |
35f39420 AM |
98 | for (ITmfEventAspect aspect : aspects) { |
99 | if (aspectClass.isAssignableFrom(aspect.getClass())) { | |
b1aad44e GB |
100 | Object obj = aspect.resolve(event); |
101 | if (obj != null) { | |
102 | return obj; | |
103 | } | |
35f39420 AM |
104 | } |
105 | } | |
b1aad44e | 106 | return null; |
35f39420 | 107 | } |
b8585c7c | 108 | } |