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