31bb37d8b1c8f0a6fc78ee58bef1882f0f75a28a
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceUtils.java
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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.BufferedInputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.tracecompass.common.core.StreamUtils;
28 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
29 import org.eclipse.tracecompass.tmf.core.component.ITmfEventProvider;
30 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
31 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
32
33 import com.google.common.collect.Iterables;
34
35 /**
36 * Utility methods for ITmfTrace's.
37 *
38 * @author Alexandre Montplaisir
39 */
40 @NonNullByDefault
41 public final class TmfTraceUtils {
42
43 private static final int MAX_NB_BINARY_BYTES = 2048;
44
45 private TmfTraceUtils() {
46 }
47
48 /**
49 * Return the first result of the first analysis module belonging to this trace or its children,
50 * with the specified ID and class.
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 /**
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.
76 *
77 * @param trace
78 * The trace for which you want the modules, the children trace modules
79 * are added as well.
80 * @param moduleClass
81 * Returned modules must extend this class
82 * @return List of modules of class moduleClass
83 */
84 public static <T> Iterable<@NonNull T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) {
85 Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules();
86 List<@NonNull T> modules = new ArrayList<>();
87 for (IAnalysisModule module : analysisModules) {
88 if (moduleClass.isAssignableFrom(module.getClass())) {
89 modules.add(checkNotNull(moduleClass.cast(module)));
90 }
91 }
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 }
98 return modules;
99 }
100
101 /**
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.
105 *
106 * @param trace
107 * The trace for which you want the event aspects
108 * @param aspectClass
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
115 */
116 public static <T extends ITmfEventAspect<?>> @Nullable Object resolveEventAspectOfClassForEvent(
117 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
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);
123 }
124
125 /**
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.
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 */
141 public static <T extends ITmfEventAspect<Integer>> @Nullable Integer resolveIntEventAspectOfClassForEvent(
142 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
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);
149 }
150
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 1.2
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 }
178 }
This page took 0.034169 seconds and 4 git commands to generate.