requirements: Implement all level for event names and fields
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceUtils.java
CommitLineData
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
13package org.eclipse.tracecompass.tmf.core.trace;
14
aa353506
AM
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
d26d67f5
BH
17import java.io.BufferedInputStream;
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.IOException;
b8585c7c
AM
21import java.util.HashSet;
22import java.util.Set;
23
df2597e0 24import org.eclipse.jdt.annotation.NonNull;
1d83ed07 25import org.eclipse.jdt.annotation.NonNullByDefault;
b8585c7c 26import org.eclipse.jdt.annotation.Nullable;
c15e897d 27import org.eclipse.tracecompass.common.core.StreamUtils;
b8585c7c 28import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
b1aad44e 29import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
35f39420 30import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
b8585c7c
AM
31
32/**
33 * Utility methods for ITmfTrace's.
34 *
35 * @author Alexandre Montplaisir
36 */
1d83ed07 37@NonNullByDefault
b8585c7c
AM
38public final class TmfTraceUtils {
39
d26d67f5
BH
40 private static final int MAX_NB_BINARY_BYTES = 2048;
41
b1aad44e
GB
42 private TmfTraceUtils() {
43 }
b8585c7c
AM
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 */
df2597e0 79 public static <T> Iterable<@NonNull T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) {
b8585c7c 80 Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules();
df2597e0 81 Set<@NonNull T> modules = new HashSet<>();
b1aad44e 82 for (IAnalysisModule module : analysisModules) {
b8585c7c 83 if (moduleClass.isAssignableFrom(module.getClass())) {
aa353506 84 modules.add(checkNotNull(moduleClass.cast(module)));
b8585c7c
AM
85 }
86 }
87 return modules;
88 }
35f39420
AM
89
90 /**
b1aad44e
GB
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.
35f39420
AM
94 *
95 * @param trace
96 * The trace for which you want the event aspects
97 * @param aspectClass
b1aad44e
GB
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
35f39420 104 */
c15e897d 105 public static <T extends ITmfEventAspect<?>> @Nullable Object resolveEventAspectOfClassForEvent(
1d83ed07 106 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
c15e897d
AM
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);
35f39420 112 }
d26d67f5 113
b3867ecc 114 /**
c15e897d
AM
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.
b3867ecc
MAL
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 */
c15e897d 130 public static <T extends ITmfEventAspect<Integer>> @Nullable Integer resolveIntEventAspectOfClassForEvent(
b3867ecc 131 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
c15e897d
AM
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);
b3867ecc
MAL
138 }
139
d26d67f5
BH
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 }
b8585c7c 167}
This page took 0.058574 seconds and 5 git commands to generate.