ctf: Introduce IEventDefinition
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFTraceReader.java
index 73a153f6a300f9bdd95811bf82ba164d2f76ec79..9f2c45c7736a5d78f3d8ce129a43a591ffbdc8ad 100644 (file)
@@ -13,6 +13,8 @@
 
 package org.eclipse.tracecompass.ctf.core.trace;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -22,14 +24,11 @@ import java.util.PriorityQueue;
 import java.util.Set;
 
 import org.eclipse.tracecompass.ctf.core.CTFException;
-import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
+import org.eclipse.tracecompass.ctf.core.event.IEventDefinition;
 import org.eclipse.tracecompass.internal.ctf.core.Activator;
 import org.eclipse.tracecompass.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
 
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSet.Builder;
-
 /**
  * A CTF trace reader. Reads the events of a trace.
  *
@@ -55,8 +54,7 @@ public class CTFTraceReader implements AutoCloseable {
     /**
      * Vector of all the trace file readers.
      */
-    private final List<CTFStreamInputReader> fStreamInputReaders =
-            Collections.synchronizedList(new ArrayList<CTFStreamInputReader>());
+    private final List<CTFStreamInputReader> fStreamInputReaders = Collections.synchronizedList(new ArrayList<CTFStreamInputReader>());
 
     /**
      * Priority queue to order the trace file readers by timestamp.
@@ -115,7 +113,7 @@ public class CTFTraceReader implements AutoCloseable {
          */
         fStartTime = 0;
         if (hasMoreEvents()) {
-            fStartTime = getTopStream().getCurrentEvent().getTimestamp();
+            fStartTime = checkNotNull(getTopStream().getCurrentEvent()).getTimestamp();
             setEndTime(fStartTime);
         }
     }
@@ -203,7 +201,7 @@ public class CTFTraceReader implements AutoCloseable {
         /*
          * For each stream.
          */
-        for (CTFStream stream : fTrace.getStreams()) {
+        for (ICTFStream stream : fTrace.getStreams()) {
             Set<CTFStreamInput> streamInputs = stream.getStreamInputs();
 
             /*
@@ -214,7 +212,7 @@ public class CTFTraceReader implements AutoCloseable {
                 /*
                  * Create a reader and add it to the group.
                  */
-                fStreamInputReaders.add(new CTFStreamInputReader(streamInput));
+                fStreamInputReaders.add(new CTFStreamInputReader(checkNotNull(streamInput)));
             }
         }
 
@@ -228,7 +226,7 @@ public class CTFTraceReader implements AutoCloseable {
      * Returns whether or not this CTFTraceReader has been closed
      *
      * @return true if it has been closed, false else
-     * @since 2.0
+     * @since 1.1
      */
     public boolean isClosed() {
         return fClosed;
@@ -242,22 +240,21 @@ public class CTFTraceReader implements AutoCloseable {
      */
     public void update() throws CTFException {
         Set<CTFStreamInputReader> readers = new HashSet<>();
-        for (CTFStream stream : fTrace.getStreams()) {
+        for (ICTFStream stream : fTrace.getStreams()) {
             Set<CTFStreamInput> streamInputs = stream.getStreamInputs();
             for (CTFStreamInput streamInput : streamInputs) {
                 /*
-                 * Create a reader.
-                 */
-                CTFStreamInputReader streamInputReader = new CTFStreamInputReader(
-                        streamInput);
-
-                /*
-                 * Add it to the group.
+                 * Create a reader to check if it already exists in the list. If it doesn't, add it.
                  */
-                if (!fStreamInputReaders.contains(streamInputReader)) {
-                    streamInputReader.readNextEvent();
-                    fStreamInputReaders.add(streamInputReader);
-                    readers.add(streamInputReader);
+                try (CTFStreamInputReader streamInputReader = new CTFStreamInputReader(checkNotNull(streamInput))) {
+                    if (!fStreamInputReaders.contains(streamInputReader)) {
+                        CTFStreamInputReader streamInputReaderToAdd = new CTFStreamInputReader(checkNotNull(streamInput));
+                        streamInputReaderToAdd.readNextEvent();
+                        fStreamInputReaders.add(streamInputReaderToAdd);
+                        readers.add(streamInputReaderToAdd);
+                    }
+                } catch (IOException e) {
+                    Activator.logError(e.getMessage(), e);
                 }
             }
         }
@@ -277,11 +274,12 @@ public class CTFTraceReader implements AutoCloseable {
      * @return the iterable of the stream input readers
      */
     public Iterable<IEventDeclaration> getEventDeclarations() {
-        ImmutableSet.Builder<IEventDeclaration> builder = new Builder<>();
+        Set<IEventDeclaration> retSet = new HashSet<>();
         for (CTFStreamInputReader sir : fStreamInputReaders) {
-            builder.addAll(sir.getEventDeclarations());
+            retSet.addAll(sir.getEventDeclarations());
         }
-        return builder.build();
+        retSet.remove(null);
+        return retSet;
     }
 
     /**
@@ -331,8 +329,9 @@ public class CTFTraceReader implements AutoCloseable {
      *
      * @return An event definition, or null of the trace reader reached the end
      *         of the trace.
+     * @since 2.0
      */
-    public EventDefinition getCurrentEventDef() {
+    public IEventDefinition getCurrentEventDef() {
         CTFStreamInputReader top = getTopStream();
         return (top != null) ? top.getCurrentEvent() : null;
     }
@@ -365,14 +364,15 @@ public class CTFTraceReader implements AutoCloseable {
              * Add it back in the queue.
              */
             fPrio.add(top);
-            final long topEnd = fTrace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
+            /*
+             * We're in OK, there's a guaranteed top#getCurrentEvent() unless another
+             * thread does something bad.
+             */
+            IEventDefinition currentEvent = checkNotNull(top.getCurrentEvent());
+            final long topEnd = fTrace.timestampCyclesToNanos(currentEvent.getTimestamp());
             setEndTime(Math.max(topEnd, getEndTime()));
             fEventCountPerTraceFile[top.getName()]++;
-
-            if (top.getCurrentEvent() != null) {
-                fEndTime = Math.max(top.getCurrentEvent().getTimestamp(),
-                        fEndTime);
-            }
+            fEndTime = Math.max(currentEvent.getTimestamp(), fEndTime);
             break;
         }
         case WAIT: {
This page took 0.027047 seconds and 5 git commands to generate.