ctf: Fix Javadoc for all public methods
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTrace.java
index 9581988fdd6757fa88ac02e15e62c6646085368e..91fcd596ecb2e3ff7865a22a1b281487134b0718 100644 (file)
@@ -34,6 +34,7 @@ import java.util.UUID;
 
 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
 import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
@@ -45,6 +46,7 @@ import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
 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.StreamInputPacketIndex;
 
 /**
  * <b><u>CTFTrace</u></b>
@@ -139,10 +141,17 @@ public class CTFTrace implements IDefinitionScope {
 
     /** Handlers for the metadata files */
     private final static FileFilter metadataFileFilter = new MetadataFileFilter();
-    private final static Comparator<File> metadataComparator = new MetadataComparator();
+    private final static Comparator<File> metadataComparator = new MetadataComparator(); // $codepro.audit.disable
+                                                                                         // fieldJavadoc
 
     /** map of all the event types */
-    private final HashMap<Long, EventDeclaration> events;
+    private final HashMap<Long,HashMap<Long, EventDeclaration>> eventDecs;
+    /** map of all the event types */
+    private final HashMap<StreamInput,HashMap<Long, EventDefinition>> eventDefs;
+    /** map of all the indexes */
+    private final HashMap<StreamInput, StreamInputPacketIndex> indexes;
+
+
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -152,8 +161,9 @@ public class CTFTrace implements IDefinitionScope {
      * Trace constructor.
      *
      * @param path
-     *            Filesystem path of the trace directory.
-     * @throws IOException
+     *            Filesystem path of the trace directory
+     * @throws CTFReaderException
+     *             If no CTF trace was found at the path
      */
     public CTFTrace(String path) throws CTFReaderException {
         this(new File(path));
@@ -166,6 +176,7 @@ public class CTFTrace implements IDefinitionScope {
      * @param path
      *            Filesystem path of the trace directory.
      * @throws CTFReaderException
+     *             If no CTF trace was found at the path
      */
     public CTFTrace(File path) throws CTFReaderException {
         this.path = path;
@@ -176,6 +187,8 @@ public class CTFTrace implements IDefinitionScope {
         environment = new HashMap<String, String>();
         clocks = new HashMap<String, CTFClock>();
         streamFileChannels = new LinkedList<FileChannel>();
+        eventDecs = new HashMap<Long, HashMap<Long, EventDeclaration>>();
+        eventDefs = new HashMap<StreamInput, HashMap<Long, EventDefinition>>();
 
         if (!this.path.isDirectory()) {
             throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
@@ -198,12 +211,12 @@ public class CTFTrace implements IDefinitionScope {
         /* List files not called metadata and not hidden. */
         File[] files = path.listFiles(metadataFileFilter);
         Arrays.sort(files, metadataComparator);
-
+        indexes = new HashMap<StreamInput, StreamInputPacketIndex>();
         /* Try to open each file */
         for (File streamFile : files) {
             openStreamInput(streamFile);
         }
-        events = new HashMap<Long, EventDeclaration>();
+
         /* Create their index */
         for (Map.Entry<Long, Stream> stream : streams.entrySet()) {
             Set<StreamInput> inputs = stream.getValue().getStreamInputs();
@@ -217,19 +230,19 @@ public class CTFTrace implements IDefinitionScope {
                     Map.Entry<Long, EventDeclaration> pairs = it.next();
                     Long eventNum = pairs.getKey();
                     EventDeclaration eventDec = pairs.getValue();
-                    events.put(eventNum, eventDec);
+                    getEvents(s.getStream().getId()).put(eventNum, eventDec);
                 }
 
                 /*
                  * index the trace
                  */
-                s.createIndex();
+                s.setupIndex();
             }
         }
     }
 
     @Override
-    protected void finalize() {
+    protected void finalize() throws Throwable {
         /* If this trace gets closed, release the descriptors to the streams */
         for (FileChannel fc : streamFileChannels) {
             if (fc != null) {
@@ -240,6 +253,8 @@ public class CTFTrace implements IDefinitionScope {
                 }
             }
         }
+        super.finalize();
+
     }
 
     // ------------------------------------------------------------------------
@@ -247,23 +262,51 @@ public class CTFTrace implements IDefinitionScope {
     // ------------------------------------------------------------------------
 
     /**
-     * Get an event by it's ID
+     * Gets an event declaration hash map for a given streamID
      *
-     * @param id
-     *            the ID of the event
-     * @return the event declaration
+     * @param streamId
+     *            The ID of the stream from which to read
+     * @return The Hash map with the event declarations
      */
-    public EventDeclaration getEventType(long id) {
-        return events.get(id);
+    public HashMap<Long, EventDeclaration> getEvents(Long streamId) {
+        return eventDecs.get(streamId);
     }
 
     /**
-     * Get the number of events in the trace so far.
+     * Gets an index for a given StreamInput
+     * @param id the StreamInput
+     * @return The index
+     */
+    public StreamInputPacketIndex getIndex(StreamInput id){
+        if(! indexes.containsKey(id)){
+            indexes.put(id, new StreamInputPacketIndex());
+        }
+        return indexes.get(id);
+    }
+
+    /**
+     * Gets an event Declaration hashmap for a given StreamInput
+     * @param id the StreamInput
+     * @return the hashmap with the event definitions
+     */
+    public HashMap<Long, EventDefinition> getEventDefs(StreamInput id) {
+        if(! eventDefs.containsKey(id)){
+            eventDefs.put(id, new HashMap<Long, EventDefinition>());
+        }
+        return eventDefs.get(id);
+    }
+
+    /**
+     * Get an event by it's ID
      *
-     * @return the number of events in the trace
+     * @param streamId
+     *            The ID of the stream from which to read
+     * @param id
+     *            the ID of the event
+     * @return the event declaration
      */
-    public int getNbEventTypes() {
-        return events.size();
+    public EventDeclaration getEventType(long streamId, long id) {
+        return getEvents(streamId).get(id);
     }
 
     /**
@@ -511,7 +554,6 @@ public class CTFTrace implements IDefinitionScope {
             /* Check UUID */
             ArrayDefinition uuidDef = (ArrayDefinition) packetHeaderDef
                     .lookupDefinition("uuid"); //$NON-NLS-1$
-            assert ((uuidDef != null) && (uuidDef.getDeclaration().getLength() == Utils.UUID_LEN));
             if (uuidDef != null) {
                 byte[] uuidArray = new byte[Utils.UUID_LEN];
 
@@ -573,8 +615,8 @@ public class CTFTrace implements IDefinitionScope {
      *
      * @param stream
      *            A stream object.
-     *
      * @throws ParseException
+     *             If there was some problem reading the metadata
      */
     public void addStream(Stream stream) throws ParseException {
 
@@ -587,8 +629,7 @@ public class CTFTrace implements IDefinitionScope {
         }
 
         /*
-         * If the stream we try to add has the null key, it must be the only
-         * one. Thus, if the streams container is not empty, it is not valid.
+         * If the stream we try to add has the null key, it must be the onl         * one. Thus, if the streams container is not empty, it is not valid.
          */
         if ((stream.getId() == null) && (streams.size() != 0)) {
             throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
@@ -601,41 +642,103 @@ public class CTFTrace implements IDefinitionScope {
 
         /* It should be ok now. */
         streams.put(stream.getId(), stream);
+        eventDecs.put(stream.getId(), new HashMap<Long,EventDeclaration>());
     }
 
+    /**
+     * gets the Environment variables from the trace metadata (See CTF spec)
+     * @return the environment variables in a hashmap form (key value)
+     */
     public HashMap<String, String> getEnvironment() {
         return environment;
     }
 
+    /**
+     * Look up a specific environment variable
+     * @param key the key to look for
+     * @return the value of the variable, can be null.
+     */
     public String lookupEnvironment(String key) {
         return environment.get(key);
     }
 
+    /**
+     * Add a variable to the environment variables
+     * @param varName the name of the variable
+     * @param varValue the value of the variable
+     */
     public void addEnvironmentVar(String varName, String varValue) {
         environment.put(varName, varValue);
     }
 
+    /**
+     * Add a clock to the clock list
+     * @param nameValue the name of the clock (full name with scope)
+     * @param ctfClock the clock
+     */
     public void addClock(String nameValue, CTFClock ctfClock) {
         clocks.put(nameValue, ctfClock);
     }
 
+    /**
+     * gets the clock with a specific name
+     * @param name the name of the clock.
+     * @return the clock
+     */
     public CTFClock getClock(String name) {
         return clocks.get(name);
     }
 
-    public CTFClock getClock() {
+    private CTFClock singleClock;
+    private long singleOffset;
+
+    /**
+     * gets the clock if there is only one. (this is 100% of the use cases as of June 2012)
+     * @return the clock
+     */
+    public final CTFClock getClock() {
         if (clocks.size() == 1) {
-            String key = (String) clocks.keySet().toArray()[0];
-            return clocks.get(key);
+            if (singleClock == null) {
+                singleClock = clocks.get(clocks.keySet().toArray()[0]);
+                singleOffset = (Long) getClock().getProperty("offset"); //$NON-NLS-1$
+            }
+            return singleClock;
         }
         return null;
     }
 
-    public long getOffset() {
+    /**
+     * gets the time offset of a clock with respect to UTC in nanoseconds
+     * @return the time offset of a clock with respect to UTC in nanoseconds
+     */
+    public final long getOffset() {
         if (getClock() == null) {
             return 0;
         }
-        return (Long) getClock().getProperty("offset"); //$NON-NLS-1$
+        return singleOffset;
+    }
+
+    /**
+     * Does a given stream contain any events?
+     * @param id the stream ID
+     * @return true if the stream has events.
+     */
+    public boolean hasEvents(Long id){
+        return eventDecs.containsKey(id);
+    }
+
+    /**
+     * Add an event declaration map to the events map.
+     * @param id the id of a stream
+     * @return the hashmap containing events.
+     */
+    public HashMap<Long, EventDeclaration> createEvents(Long id){
+        HashMap<Long, EventDeclaration> value = eventDecs.get(id);
+        if( value == null ) {
+            value = new HashMap<Long, EventDeclaration>();
+            eventDecs.put(id, value);
+        }
+        return value;
     }
 
 }
@@ -660,6 +763,8 @@ class MetadataFileFilter implements FileFilter {
 
 class MetadataComparator implements Comparator<File>, Serializable {
 
+    private static final long serialVersionUID = 1L;
+
     @Override
     public int compare(File o1, File o2) {
         return o1.getName().compareTo(o2.getName());
This page took 0.029427 seconds and 5 git commands to generate.