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
.tmf
.core
.analysis
.IAnalysisModule
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
32 * Utility methods for ITmfTrace's.
34 * @author Alexandre Montplaisir
37 public final class TmfTraceUtils
{
39 private static final int MAX_NB_BINARY_BYTES
= 2048;
41 private TmfTraceUtils() {
45 * Get an analysis module belonging to this trace, with the specified ID and
49 * The trace for which you want the modules
51 * Returned modules must extend this class
53 * The ID of the analysis module
54 * @return The analysis module with specified class and ID, or null if no
57 public static @Nullable <T
extends IAnalysisModule
> T
getAnalysisModuleOfClass(ITmfTrace trace
,
58 Class
<T
> moduleClass
, String id
) {
59 Iterable
<T
> modules
= getAnalysisModulesOfClass(trace
, moduleClass
);
60 for (T module
: modules
) {
61 if (id
.equals(module
.getId())) {
69 * Return the analysis modules that are of a given class. Module will be
70 * casted to the requested class.
73 * The trace for which you want the modules
75 * Returned modules must extend this class
76 * @return List of modules of class moduleClass
78 public static <T
> Iterable
<@NonNull T
> getAnalysisModulesOfClass(ITmfTrace trace
, Class
<T
> moduleClass
) {
79 Iterable
<IAnalysisModule
> analysisModules
= trace
.getAnalysisModules();
80 Set
<@NonNull T
> modules
= new HashSet
<>();
81 for (IAnalysisModule module
: analysisModules
) {
82 if (moduleClass
.isAssignableFrom(module
.getClass())) {
83 modules
.add(checkNotNull(moduleClass
.cast(module
)));
90 * Return the first result of the first aspect that resolves as non null for
91 * the event received in parameter. If the returned value is not null, it
92 * can be safely cast to the aspect's class proper return type.
95 * The trace for which you want the event aspects
97 * The class of the aspect(s) to resolve
99 * The event for which to get the aspect
100 * @return The first result of the
101 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
102 * for the event or {@code null} otherwise
104 public static @Nullable <T
extends ITmfEventAspect
<?
>> Object
resolveEventAspectOfClassForEvent(
105 ITmfTrace trace
, Class
<T
> aspectClass
, ITmfEvent event
) {
106 Iterable
<ITmfEventAspect
<?
>> aspects
= trace
.getEventAspects();
107 for (ITmfEventAspect
<?
> aspect
: aspects
) {
108 if (aspectClass
.isAssignableFrom(aspect
.getClass())) {
109 Object obj
= aspect
.resolve(event
);
119 * Return the first result of the first aspect that resolves as non null for
120 * the event received in parameter. The result is cast to an Integer if
121 * possible, otherwise null is returned.
124 * The trace for which you want the event aspects
126 * The class of the aspect(s) to resolve
128 * The event for which to get the aspect
129 * @return Integer of the first result of the
130 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
131 * for the event or {@code null} otherwise
134 public static @Nullable <T
extends ITmfEventAspect
<Integer
>> Integer
resolveIntEventAspectOfClassForEvent(
135 ITmfTrace trace
, Class
<T
> aspectClass
, ITmfEvent event
) {
136 Object result
= resolveEventAspectOfClassForEvent(trace
, aspectClass
, event
);
137 if (result
instanceof Integer
) {
138 return (Integer
) result
;
144 * Checks for text file.
146 * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
147 * bytes to determine if the file is text.
150 * the file to check. Caller has to make sure that file exists.
151 * @return true if it is binary else false
152 * @throws IOException
153 * if IOException occurs
156 public static boolean isText(File file
) throws IOException
{
157 try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream(file
))) {
159 int val
= bufferedInputStream
.read();
160 while ((count
< MAX_NB_BINARY_BYTES
) && (val
>= 0)) {
165 val
= bufferedInputStream
.read();