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
.HashSet
;
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
;
33 * Utility methods for ITmfTrace's.
35 * @author Alexandre Montplaisir
38 public final class TmfTraceUtils
{
40 private static final int MAX_NB_BINARY_BYTES
= 2048;
42 private TmfTraceUtils() {
46 * Get an analysis module belonging to this trace, with the specified ID and
50 * The trace for which you want the modules
52 * Returned modules must extend this class
54 * The ID of the analysis module
55 * @return The analysis module with specified class and ID, or null if no
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())) {
70 * Return the analysis modules that are of a given class. Module will be
71 * casted to the requested class.
74 * The trace for which you want the modules
76 * Returned modules must extend this class
77 * @return List of modules of class moduleClass
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
)));
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.
96 * The trace for which you want the event aspects
98 * The class of the aspect(s) to resolve
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
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);
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.
120 * The trace for which you want the event aspects
122 * The class of the aspect(s) to resolve
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
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);
141 * Checks for text file.
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.
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
153 public static boolean isText(File file
) throws IOException
{
154 try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream(file
))) {
156 int val
= bufferedInputStream
.read();
157 while ((count
< MAX_NB_BINARY_BYTES
) && (val
>= 0)) {
162 val
= bufferedInputStream
.read();