ctf: simplify search
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFStreamInputReader.java
index c4d36a9d1df6587f5552a60b1d19eb47b8a7e45c..8faf38c45f58c256a0f4b16a9eb7c6100f0aa1ba 100644 (file)
 
 package org.eclipse.tracecompass.ctf.core.trace;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.io.File;
 import java.io.IOException;
-import java.nio.ByteOrder;
 import java.nio.channels.FileChannel;
 import java.nio.file.StandardOpenOption;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.ctf.core.CTFException;
 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
@@ -32,7 +35,9 @@ import com.google.common.collect.ImmutableList;
  *
  * @author Matthew Khouzam
  * @author Simon Marchi
+ * @since 2.0
  */
+@NonNullByDefault
 public class CTFStreamInputReader implements AutoCloseable {
 
     // ------------------------------------------------------------------------
@@ -42,11 +47,11 @@ public class CTFStreamInputReader implements AutoCloseable {
     /**
      * The StreamInput we are reading.
      */
-    private final @NonNull File fFile;
+    private final File fFile;
 
-    private final @NonNull CTFStreamInput fStreamInput;
+    private final CTFStreamInput fStreamInput;
 
-    private final FileChannel fFileChannel;
+    private final @Nullable FileChannel fFileChannel;
 
     /**
      * The packet reader used to read packets from this trace file.
@@ -62,12 +67,10 @@ public class CTFStreamInputReader implements AutoCloseable {
      * Reference to the current event of this trace file (iow, the last on that
      * was read, the next one to be returned)
      */
-    private EventDefinition fCurrentEvent = null;
+    private @Nullable EventDefinition fCurrentEvent = null;
 
     private int fId;
 
-    private CTFTraceReader fParent;
-
     /**
      * Live trace reading
      */
@@ -85,9 +88,6 @@ public class CTFStreamInputReader implements AutoCloseable {
      *             If the file cannot be opened
      */
     public CTFStreamInputReader(CTFStreamInput streamInput) throws CTFException {
-        if (streamInput == null) {
-            throw new IllegalArgumentException("stream cannot be null"); //$NON-NLS-1$
-        }
         fStreamInput = streamInput;
         fFile = fStreamInput.getFile();
         try {
@@ -127,9 +127,7 @@ public class CTFStreamInputReader implements AutoCloseable {
         if (fFileChannel != null) {
             fFileChannel.close();
         }
-        if (fPacketReader != null) {
-            fPacketReader.close();
-        }
+        fPacketReader.close();
     }
 
     // ------------------------------------------------------------------------
@@ -142,19 +140,10 @@ public class CTFStreamInputReader implements AutoCloseable {
      * @return the current event in the stream, null if the stream is
      *         finished/empty/malformed
      */
-    public EventDefinition getCurrentEvent() {
+    public @Nullable EventDefinition getCurrentEvent() {
         return fCurrentEvent;
     }
 
-    /**
-     * Gets the byte order for a trace
-     *
-     * @return the trace byte order
-     */
-    public ByteOrder getByteOrder() {
-        return fStreamInput.getStream().getTrace().getByteOrder();
-    }
-
     /**
      * Gets the name of the stream (it's an id and a number)
      *
@@ -206,7 +195,7 @@ public class CTFStreamInputReader implements AutoCloseable {
      * @return Unmodifiable set with the event definitions
      */
     public Iterable<IEventDeclaration> getEventDeclarations() {
-        return ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations());
+        return checkNotNull(ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations()));
     }
 
     /**
@@ -233,7 +222,7 @@ public class CTFStreamInputReader implements AutoCloseable {
      *
      * @return the event context declaration of the stream
      */
-    public StructDeclaration getStreamEventContextDecl() {
+    public @Nullable StructDeclaration getStreamEventContextDecl() {
         return getStreamInput().getStream().getEventContextDecl();
     }
 
@@ -253,8 +242,7 @@ public class CTFStreamInputReader implements AutoCloseable {
          * Change packet if needed
          */
         if (!fPacketReader.hasMoreEvents()) {
-            final ICTFPacketDescriptor prevPacket = fPacketReader
-                    .getCurrentPacket();
+            final ICTFPacketDescriptor prevPacket = fPacketReader.getCurrentPacket();
             if (prevPacket != null || fLive) {
                 goToNextPacket();
             }
@@ -344,10 +332,10 @@ public class CTFStreamInputReader implements AutoCloseable {
          * timestamp.
          */
         readNextEvent();
-        boolean done = (this.getCurrentEvent() == null);
-        while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
+        EventDefinition currentEvent = getCurrentEvent();
+        while (currentEvent != null && (currentEvent.getTimestamp() < timestamp)) {
             readNextEvent();
-            done = (this.getCurrentEvent() == null);
+            currentEvent = getCurrentEvent();
             offset++;
         }
         return offset;
@@ -360,8 +348,7 @@ public class CTFStreamInputReader implements AutoCloseable {
      *             if an error occurs
      */
     private void gotoPacket(long timestamp) throws CTFException {
-        fPacketIndex = fStreamInput.getIndex().search(timestamp)
-                .previousIndex();
+        fPacketIndex = fStreamInput.getIndex().search(timestamp) - 1;
         /*
          * Switch to this packet.
          */
@@ -431,28 +418,13 @@ public class CTFStreamInputReader implements AutoCloseable {
         this.setCurrentEvent(prevEvent);
     }
 
-    /**
-     * @return the parent
-     */
-    public CTFTraceReader getParent() {
-        return fParent;
-    }
-
-    /**
-     * @param parent
-     *            the parent to set
-     */
-    public void setParent(CTFTraceReader parent) {
-        fParent = parent;
-    }
-
     /**
      * Sets the current event in a stream input reader
      *
      * @param currentEvent
      *            the event to set
      */
-    public void setCurrentEvent(EventDefinition currentEvent) {
+    public void setCurrentEvent(@Nullable EventDefinition currentEvent) {
         fCurrentEvent = currentEvent;
     }
 
@@ -463,7 +435,10 @@ public class CTFStreamInputReader implements AutoCloseable {
         return fPacketIndex;
     }
 
-    private ICTFPacketDescriptor getPacket() {
+    private @Nullable ICTFPacketDescriptor getPacket() {
+        if (getPacketIndex() >= fStreamInput.getIndex().size()) {
+            return null;
+        }
         return fStreamInput.getIndex().getElement(getPacketIndex());
     }
 
@@ -472,6 +447,7 @@ public class CTFStreamInputReader implements AutoCloseable {
      *
      * @return the file channel
      */
+    @Nullable
     FileChannel getFc() {
         return fFileChannel;
     }
@@ -494,7 +470,7 @@ public class CTFStreamInputReader implements AutoCloseable {
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(@Nullable Object obj) {
         if (this == obj) {
             return true;
         }
@@ -514,7 +490,7 @@ public class CTFStreamInputReader implements AutoCloseable {
     @Override
     public String toString() {
         // this helps debugging
-        return fId + ' ' + fCurrentEvent.toString();
+        return fId + ' ' + NonNullUtils.nullToEmptyString(fCurrentEvent);
     }
 
 }
This page took 0.031674 seconds and 5 git commands to generate.