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