tmf: Introduce resolveIntEventAspectOfClassForEvent
[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
d26d67f5
BH
15import java.io.BufferedInputStream;
16import java.io.File;
17import java.io.FileInputStream;
18import java.io.IOException;
b8585c7c
AM
19import java.util.HashSet;
20import java.util.Set;
21
df2597e0 22import org.eclipse.jdt.annotation.NonNull;
1d83ed07 23import org.eclipse.jdt.annotation.NonNullByDefault;
b8585c7c
AM
24import org.eclipse.jdt.annotation.Nullable;
25import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
b1aad44e 26import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
35f39420 27import 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
35public 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 115
b3867ecc
MAL
116 /**
117 * Return the first result of the first aspect that resolves as non null for
118 * the event received in parameter. The result is cast to an Integer if
119 * possible, otherwise null is returned.
120 *
121 * @param trace
122 * The trace for which you want the event aspects
123 * @param aspectClass
124 * The class of the aspect(s) to resolve
125 * @param event
126 * The event for which to get the aspect
127 * @return Integer of the first result of the
128 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
129 * for the event or {@code null} otherwise
130 * @since 2.0
131 */
132 public static @Nullable <T extends ITmfEventAspect> Integer resolveIntEventAspectOfClassForEvent(
133 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
134 Object result = resolveEventAspectOfClassForEvent(trace, aspectClass, event);
135 if (result instanceof Integer) {
136 return (Integer) result;
137 }
138 return null;
139 }
140
d26d67f5
BH
141 /**
142 * Checks for text file.
143 *
144 * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
145 * bytes to determine if the file is text.
146 *
147 * @param file
148 * the file to check. Caller has to make sure that file exists.
149 * @return true if it is binary else false
150 * @throws IOException
151 * if IOException occurs
152 * @since 2.0
153 */
154 public static boolean isText(File file) throws IOException {
155 try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
156 int count = 0;
157 int val = bufferedInputStream.read();
158 while ((count < MAX_NB_BINARY_BYTES) && (val >= 0)) {
159 if (val == 0) {
160 return false;
161 }
162 count++;
163 val = bufferedInputStream.read();
164 }
165 }
166 return true;
167 }
b8585c7c 168}
This page took 0.060433 seconds and 5 git commands to generate.