ctf: Fix multiple coding style and typo issues while reading code.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTraceReader.java
index 08f9d181abd47ae95454cb1f7923544de96d338f..392900187b5197b79d3fc51559b7a134ae0479ee 100644 (file)
@@ -1,32 +1,35 @@
 /*******************************************************************************
- * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
- * Contributors: Matthew Khouzam - Initial API and implementation
- * Contributors: Alexandre Montplaisir - Initial API and implementation
+ * Contributors:
+ *     Matthew Khouzam - Initial API and implementation
+ *     Alexandre Montplaisir - Initial API and implementation
  *******************************************************************************/
 
 package org.eclipse.linuxtools.ctf.core.trace;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.PriorityQueue;
 import java.util.Set;
-import java.util.Vector;
 
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.internal.ctf.core.Activator;
-import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
-import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
 
 /**
- * Reads the events of a trace.
+ * A CTF trace reader. Reads the events of a trace.
+ *
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Alexandre Montplaisir
  */
-
 public class CTFTraceReader {
 
     // ------------------------------------------------------------------------
@@ -41,12 +44,12 @@ public class CTFTraceReader {
     /**
      * Vector of all the trace file readers.
      */
-    private final Vector<StreamInputReader> streamInputReaders = new Vector<StreamInputReader>();
+    private final List<StreamInputReader> streamInputReaders = new ArrayList<StreamInputReader>();
 
     /**
      * Priority queue to order the trace file readers by timestamp.
      */
-    protected PriorityQueue<StreamInputReader> prio;
+    private PriorityQueue<StreamInputReader> prio;
 
     /**
      * Array to count the number of event per trace file.
@@ -63,11 +66,6 @@ public class CTFTraceReader {
      */
     private long endTime;
 
-
-    protected void setEndTime(long endTime) {
-        this.endTime = endTime;
-    }
-
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -77,7 +75,6 @@ public class CTFTraceReader {
      *
      * @param trace
      *            The trace to read from.
-     * @throws CTFReaderException
      */
     public CTFTraceReader(CTFTrace trace) {
         this.trace = trace;
@@ -97,7 +94,7 @@ public class CTFTraceReader {
          * Get the start Time of this trace bear in mind that the trace could be
          * empty.
          */
-        this.startTime = 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
+        this.startTime = 0;
         if (hasMoreEvents()) {
             this.startTime = prio.peek().getCurrentEvent().getTimestamp();
             this.setEndTime(this.startTime);
@@ -106,6 +103,8 @@ public class CTFTraceReader {
 
     /**
      * Copy constructor
+     *
+     * @return The new CTFTraceReader
      */
     public CTFTraceReader copyFrom() {
         CTFTraceReader newReader = null;
@@ -116,6 +115,19 @@ public class CTFTraceReader {
         return newReader;
     }
 
+    /**
+     * Dispose the CTFTraceReader
+     * @since 2.0
+     */
+    public void dispose() {
+        for (StreamInputReader reader : streamInputReaders) {
+            if (reader != null) {
+                reader.dispose();
+            }
+        }
+        streamInputReaders.clear();
+    }
+
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
@@ -129,6 +141,27 @@ public class CTFTraceReader {
         return this.startTime;
     }
 
+    /**
+     * Set the trace's end time
+     *
+     * @param endTime
+     *            The end time to use
+     */
+    protected final void setEndTime(long endTime) {
+        this.endTime = endTime;
+    }
+
+    /**
+     * Get the priority queue of this trace reader.
+     *
+     * @return The priority queue of input readers
+     * @since 2.0
+     */
+    protected PriorityQueue<StreamInputReader> getPrio() {
+        return prio;
+    }
+
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
@@ -149,7 +182,6 @@ public class CTFTraceReader {
              * For each trace file of the stream.
              */
             for (StreamInput streamInput : streamInputs) {
-                streamInput.getIndex().getEntries().clear();
                 /*
                  * Create a reader.
                  */
@@ -174,6 +206,11 @@ public class CTFTraceReader {
      * lower next event timestamp.
      */
     private void populateStreamInputReaderHeap() {
+        if (this.streamInputReaders.isEmpty()) {
+            this.prio = new PriorityQueue<StreamInputReader>();
+            return;
+        }
+
         /*
          * Create the priority queue with a size twice as bigger as the number
          * of reader in order to avoid constant resizing.
@@ -220,9 +257,6 @@ public class CTFTraceReader {
      * @return True if an event was read.
      */
     public boolean advance() {
-        /*
-         * Index the
-         */
         /*
          * Remove the reader from the top of the priority queue.
          */
@@ -242,8 +276,8 @@ public class CTFTraceReader {
              * Add it back in the queue.
              */
             this.prio.add(top);
-            final long topEnd = top.getCurrentEvent().getTimestamp() + this.getTrace().getOffset();
-            this.setEndTime( Math.max(topEnd, this.getEndTime()));
+            final long topEnd = this.trace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
+            this.setEndTime(Math.max(topEnd, this.getEndTime()));
             this.eventCountPerTraceFile[top.getName()]++;
 
             if (top.getCurrentEvent() != null) {
@@ -260,8 +294,6 @@ public class CTFTraceReader {
 
     /**
      * Go to the last event in the trace.
-     *
-     * @throws CTFReaderException
      */
     public void goToLastEvent() {
         seek(this.getEndTime());
@@ -271,15 +303,16 @@ public class CTFTraceReader {
     }
 
     /**
-     * Seeks to a given timestamp It will go to the event just after the
-     * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
-     * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
-     * if you want 21, you'll get 30. You want -inf, you'll get the first
-     * element, you want +inf, you'll get the end of the file with no events.
+     * Seeks to a given timestamp. It will seek to the nearest event greater or
+     * equal to timestamp. If a trace is [10 20 30 40] and you are looking for
+     * 19, it will give you 20. If you want 20, you will get 20, if you want 21,
+     * you will get 30. The value -inf will seek to the first element and the
+     * value +inf will seek to the end of the file (past the last event).
      *
      * @param timestamp
      *            the timestamp to seek to
-     * @return true if the trace has more events following the timestamp
+     * @return true if there are events above or equal the seek timestamp,
+     *         false if seek at the end of the trace (no valid event).
      */
     public boolean seek(long timestamp) {
         /*
@@ -295,34 +328,18 @@ public class CTFTraceReader {
             /*
              * Add it to the priority queue if there is a current event.
              */
-
-        }
-        for (StreamInputReader streamInputReader : this.streamInputReaders) {
             if (streamInputReader.getCurrentEvent() != null) {
                 this.prio.add(streamInputReader);
-
             }
         }
         return hasMoreEvents();
     }
 
     /**
-     * Go to the first entry of a trace
+     * Gets the stream with the oldest event
      *
-     * @return 0, the first index.
+     * @return the stream with the oldest event
      */
-    private long goToZero() {
-        long tempIndex;
-        for (StreamInputReader streamInputReader : this.streamInputReaders) {
-            /*
-             * Seek the trace reader.
-             */
-            streamInputReader.seek(0);
-        }
-        tempIndex = 0;
-        return tempIndex;
-    }
-
     public StreamInputReader getTopStream() {
         return this.prio.peek();
     }
@@ -332,7 +349,7 @@ public class CTFTraceReader {
      *
      * @return true if yes.
      */
-    public boolean hasMoreEvents() {
+    public final boolean hasMoreEvents() {
         return this.prio.size() > 0;
     }
 
@@ -365,7 +382,8 @@ public class CTFTraceReader {
             long len = (width * this.eventCountPerTraceFile[se.getName()])
                     / numEvents;
 
-            StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$
+            StringBuilder sb = new StringBuilder(se.getFilename());
+            sb.append("\t["); //$NON-NLS-1$
 
             for (int i = 0; i < len; i++) {
                 sb.append('+');
@@ -376,10 +394,16 @@ public class CTFTraceReader {
             }
 
             sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
-            Activator.getDefault().log(sb.toString());
+            Activator.log(sb.toString());
         }
     }
 
+    /**
+     * Gets the last event timestamp that was read. This is NOT necessarily the
+     * last event in a trace, just the last one read so far.
+     *
+     * @return the last event
+     */
     public long getEndTime() {
         return this.endTime;
     }
@@ -389,9 +413,7 @@ public class CTFTraceReader {
         final int prime = 31;
         int result = 1;
         result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
-        result = (prime * result)
-                + ((streamInputReaders == null) ? 0 : streamInputReaders
-                        .hashCode());
+        result = (prime * result) + streamInputReaders.hashCode();
         result = (prime * result) + ((trace == null) ? 0 : trace.hashCode());
         return result;
     }
@@ -408,11 +430,7 @@ public class CTFTraceReader {
             return false;
         }
         CTFTraceReader other = (CTFTraceReader) obj;
-        if (streamInputReaders == null) {
-            if (other.streamInputReaders != null) {
-                return false;
-            }
-        } else if (!streamInputReaders.equals(other.streamInputReaders)) {
+        if (!streamInputReaders.equals(other.streamInputReaders)) {
             return false;
         }
         if (trace == null) {
@@ -425,17 +443,17 @@ public class CTFTraceReader {
         return true;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see java.lang.Object#toString()
-     */
     @Override
     public String toString() {
         /* Only for debugging, shouldn't be externalized */
         return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$
     }
 
+    /**
+     * Gets the parent trace
+     *
+     * @return the parent trace
+     */
     public CTFTrace getTrace() {
         return trace;
     }
This page took 0.02769 seconds and 5 git commands to generate.