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 | ||
aa353506 AM |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
d26d67f5 BH |
17 | import java.io.BufferedInputStream; |
18 | import java.io.File; | |
19 | import java.io.FileInputStream; | |
20 | import java.io.IOException; | |
b8585c7c AM |
21 | import java.util.HashSet; |
22 | import java.util.Set; | |
23 | ||
df2597e0 | 24 | import org.eclipse.jdt.annotation.NonNull; |
1d83ed07 | 25 | import org.eclipse.jdt.annotation.NonNullByDefault; |
b8585c7c AM |
26 | import org.eclipse.jdt.annotation.Nullable; |
27 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; | |
b1aad44e | 28 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
35f39420 | 29 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
b8585c7c AM |
30 | |
31 | /** | |
32 | * Utility methods for ITmfTrace's. | |
33 | * | |
34 | * @author Alexandre Montplaisir | |
35 | */ | |
1d83ed07 | 36 | @NonNullByDefault |
b8585c7c AM |
37 | public final class TmfTraceUtils { |
38 | ||
d26d67f5 BH |
39 | private static final int MAX_NB_BINARY_BYTES = 2048; |
40 | ||
b1aad44e GB |
41 | private TmfTraceUtils() { |
42 | } | |
b8585c7c AM |
43 | |
44 | /** | |
45 | * Get an analysis module belonging to this trace, with the specified ID and | |
46 | * class. | |
47 | * | |
48 | * @param trace | |
49 | * The trace for which you want the modules | |
50 | * @param moduleClass | |
51 | * Returned modules must extend this class | |
52 | * @param id | |
53 | * The ID of the analysis module | |
54 | * @return The analysis module with specified class and ID, or null if no | |
55 | * such module exists. | |
56 | */ | |
57 | public static @Nullable <T extends IAnalysisModule> T getAnalysisModuleOfClass(ITmfTrace trace, | |
58 | Class<T> moduleClass, String id) { | |
59 | Iterable<T> modules = getAnalysisModulesOfClass(trace, moduleClass); | |
60 | for (T module : modules) { | |
61 | if (id.equals(module.getId())) { | |
62 | return module; | |
63 | } | |
64 | } | |
65 | return null; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Return the analysis modules that are of a given class. Module will be | |
70 | * casted to the requested class. | |
71 | * | |
72 | * @param trace | |
73 | * The trace for which you want the modules | |
74 | * @param moduleClass | |
75 | * Returned modules must extend this class | |
76 | * @return List of modules of class moduleClass | |
77 | */ | |
df2597e0 | 78 | public static <T> Iterable<@NonNull T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) { |
b8585c7c | 79 | Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules(); |
df2597e0 | 80 | Set<@NonNull T> modules = new HashSet<>(); |
b1aad44e | 81 | for (IAnalysisModule module : analysisModules) { |
b8585c7c | 82 | if (moduleClass.isAssignableFrom(module.getClass())) { |
aa353506 | 83 | modules.add(checkNotNull(moduleClass.cast(module))); |
b8585c7c AM |
84 | } |
85 | } | |
86 | return modules; | |
87 | } | |
35f39420 AM |
88 | |
89 | /** | |
b1aad44e GB |
90 | * Return the first result of the first aspect that resolves as non null for |
91 | * the event received in parameter. If the returned value is not null, it | |
92 | * can be safely cast to the aspect's class proper return type. | |
35f39420 AM |
93 | * |
94 | * @param trace | |
95 | * The trace for which you want the event aspects | |
96 | * @param aspectClass | |
b1aad44e GB |
97 | * The class of the aspect(s) to resolve |
98 | * @param event | |
99 | * The event for which to get the aspect | |
100 | * @return The first result of the | |
101 | * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null | |
102 | * for the event or {@code null} otherwise | |
35f39420 | 103 | */ |
1d83ed07 AM |
104 | public static @Nullable <T extends ITmfEventAspect> Object resolveEventAspectOfClassForEvent( |
105 | ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) { | |
35f39420 | 106 | Iterable<ITmfEventAspect> aspects = trace.getEventAspects(); |
35f39420 AM |
107 | for (ITmfEventAspect aspect : aspects) { |
108 | if (aspectClass.isAssignableFrom(aspect.getClass())) { | |
b1aad44e GB |
109 | Object obj = aspect.resolve(event); |
110 | if (obj != null) { | |
111 | return obj; | |
112 | } | |
35f39420 AM |
113 | } |
114 | } | |
b1aad44e | 115 | return null; |
35f39420 | 116 | } |
d26d67f5 | 117 | |
b3867ecc MAL |
118 | /** |
119 | * Return the first result of the first aspect that resolves as non null for | |
120 | * the event received in parameter. The result is cast to an Integer if | |
121 | * possible, otherwise null is returned. | |
122 | * | |
123 | * @param trace | |
124 | * The trace for which you want the event aspects | |
125 | * @param aspectClass | |
126 | * The class of the aspect(s) to resolve | |
127 | * @param event | |
128 | * The event for which to get the aspect | |
129 | * @return Integer of the first result of the | |
130 | * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null | |
131 | * for the event or {@code null} otherwise | |
132 | * @since 2.0 | |
133 | */ | |
134 | public static @Nullable <T extends ITmfEventAspect> Integer resolveIntEventAspectOfClassForEvent( | |
135 | ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) { | |
136 | Object result = resolveEventAspectOfClassForEvent(trace, aspectClass, event); | |
137 | if (result instanceof Integer) { | |
138 | return (Integer) result; | |
139 | } | |
140 | return null; | |
141 | } | |
142 | ||
d26d67f5 BH |
143 | /** |
144 | * Checks for text file. | |
145 | * | |
146 | * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES | |
147 | * bytes to determine if the file is text. | |
148 | * | |
149 | * @param file | |
150 | * the file to check. Caller has to make sure that file exists. | |
151 | * @return true if it is binary else false | |
152 | * @throws IOException | |
153 | * if IOException occurs | |
154 | * @since 2.0 | |
155 | */ | |
156 | public static boolean isText(File file) throws IOException { | |
157 | try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) { | |
158 | int count = 0; | |
159 | int val = bufferedInputStream.read(); | |
160 | while ((count < MAX_NB_BINARY_BYTES) && (val >= 0)) { | |
161 | if (val == 0) { | |
162 | return false; | |
163 | } | |
164 | count++; | |
165 | val = bufferedInputStream.read(); | |
166 | } | |
167 | } | |
168 | return true; | |
169 | } | |
b8585c7c | 170 | } |