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