1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.io
.BufferedInputStream
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
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
;
33 import com
.google
.common
.collect
.Iterables
;
36 * Utility methods for ITmfTrace's.
38 * @author Alexandre Montplaisir
41 public final class TmfTraceUtils
{
43 private static final int MAX_NB_BINARY_BYTES
= 2048;
45 private TmfTraceUtils() {
49 * Return the first result of the first analysis module belonging to this trace or its children,
50 * with the specified ID and class.
53 * The trace for which you want the modules
55 * Returned modules must extend this class
57 * The ID of the analysis module
58 * @return The analysis module with specified class and ID, or null if no
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())) {
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
78 * The trace for which you want the modules, the children trace modules
81 * Returned modules must extend this class
82 * @return List of modules of class moduleClass
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
)));
92 for (ITmfEventProvider child
: trace
.getChildren()) {
93 if (child
instanceof ITmfTrace
) {
94 ITmfTrace childTrace
= (ITmfTrace
) child
;
95 Iterables
.addAll(modules
, getAnalysisModulesOfClass(childTrace
, moduleClass
));
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.
107 * The trace for which you want the event aspects
109 * The class of the aspect(s) to resolve
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
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);
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.
131 * The trace for which you want the event aspects
133 * The class of the aspect(s) to resolve
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
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);
152 * Checks for text file.
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.
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
164 public static boolean isText(File file
) throws IOException
{
165 try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream(file
))) {
167 int val
= bufferedInputStream
.read();
168 while ((count
< MAX_NB_BINARY_BYTES
) && (val
>= 0)) {
173 val
= bufferedInputStream
.read();