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