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; | |
21 | ||
22 | /** | |
23 | * Utility methods for ITmfTrace's. | |
24 | * | |
25 | * @author Alexandre Montplaisir | |
26 | */ | |
27 | public final class TmfTraceUtils { | |
28 | ||
29 | private TmfTraceUtils() {} | |
30 | ||
31 | /** | |
32 | * Get an analysis module belonging to this trace, with the specified ID and | |
33 | * class. | |
34 | * | |
35 | * @param trace | |
36 | * The trace for which you want the modules | |
37 | * @param moduleClass | |
38 | * Returned modules must extend this class | |
39 | * @param id | |
40 | * The ID of the analysis module | |
41 | * @return The analysis module with specified class and ID, or null if no | |
42 | * such module exists. | |
43 | */ | |
44 | public static @Nullable <T extends IAnalysisModule> T getAnalysisModuleOfClass(ITmfTrace trace, | |
45 | Class<T> moduleClass, String id) { | |
46 | Iterable<T> modules = getAnalysisModulesOfClass(trace, moduleClass); | |
47 | for (T module : modules) { | |
48 | if (id.equals(module.getId())) { | |
49 | return module; | |
50 | } | |
51 | } | |
52 | return null; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Return the analysis modules that are of a given class. Module will be | |
57 | * casted to the requested class. | |
58 | * | |
59 | * @param trace | |
60 | * The trace for which you want the modules | |
61 | * @param moduleClass | |
62 | * Returned modules must extend this class | |
63 | * @return List of modules of class moduleClass | |
64 | */ | |
65 | public static @NonNull <T> Iterable<T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) { | |
66 | Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules(); | |
67 | Set<T> modules = new HashSet<>(); | |
68 | for (IAnalysisModule module : analysisModules) { | |
69 | if (moduleClass.isAssignableFrom(module.getClass())) { | |
70 | modules.add(moduleClass.cast(module)); | |
71 | } | |
72 | } | |
73 | return modules; | |
74 | } | |
75 | } |