ctf: split up IOStructGen into 44 files
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFTrace.java
index 9d82709e13cd247e4180beeda48d6dafa2dc578a..5053c34dedd64bb64d07fda17cb1c2cb96c85e1c 100644 (file)
@@ -32,6 +32,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.ctf.core.CTFException;
 import org.eclipse.tracecompass.ctf.core.CTFStrings;
 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
@@ -48,9 +49,12 @@ import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
 import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer;
 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
-import org.eclipse.tracecompass.internal.ctf.core.event.metadata.exceptions.ParseException;
+import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
+import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream;
 import org.eclipse.tracecompass.internal.ctf.core.trace.Utils;
 
+import com.google.common.collect.ImmutableMap;
+
 /**
  * A CTF trace on the file system.
  *
@@ -119,12 +123,12 @@ public class CTFTrace implements IDefinitionScope {
     /**
      * Collection of streams contained in the trace.
      */
-    private final Map<Long, CTFStream> fStreams = new HashMap<>();
+    private final Map<Long, ICTFStream> fStreams = new HashMap<>();
 
     /**
      * Collection of environment variables set by the tracer
      */
-    private final Map<String, String> fEnvironment = new HashMap<>();
+    private Map<String, String> fEnvironment = new HashMap<>();
 
     /**
      * Collection of all the clocks in a system.
@@ -201,7 +205,7 @@ public class CTFTrace implements IDefinitionScope {
         }
 
         /* Create their index */
-        for (CTFStream stream : getStreams()) {
+        for (ICTFStream stream : getStreams()) {
             Set<CTFStreamInput> inputs = stream.getStreamInputs();
             for (CTFStreamInput s : inputs) {
                 addStream(s);
@@ -221,7 +225,11 @@ public class CTFTrace implements IDefinitionScope {
      * @return The list of event declarations
      */
     public Collection<IEventDeclaration> getEventDeclarations(Long streamId) {
-        return fStreams.get(streamId).getEventDeclarations();
+        ICTFStream stream = fStreams.get(streamId);
+        if (stream == null) {
+            return null;
+        }
+        return stream.getEventDeclarations();
     }
 
     /**
@@ -230,8 +238,9 @@ public class CTFTrace implements IDefinitionScope {
      * @param id
      *            Long the id of the stream
      * @return Stream the stream that we need
+     * @since 2.0
      */
-    public CTFStream getStream(Long id) {
+    public ICTFStream getStream(Long id) {
         if (id == null) {
             return fStreams.get(0L);
         }
@@ -401,7 +410,7 @@ public class CTFTrace implements IDefinitionScope {
      *
      * @return Iterable&lt;Stream&gt; an iterable over streams.
      */
-    public Iterable<CTFStream> getStreams() {
+    public Iterable<ICTFStream> getStreams() {
         return fStreams.values();
     }
 
@@ -424,7 +433,7 @@ public class CTFTrace implements IDefinitionScope {
         /*
          * add the stream
          */
-        CTFStream stream = s.getStream();
+        ICTFStream stream = s.getStream();
         fStreams.put(stream.getId(), stream);
 
         /*
@@ -445,10 +454,10 @@ public class CTFTrace implements IDefinitionScope {
      * @throws CTFException
      *             if there is a file error
      */
-    private CTFStream openStreamInput(File streamFile) throws CTFException {
+    private ICTFStream openStreamInput(File streamFile) throws CTFException {
         ByteBuffer byteBuffer;
         BitBuffer streamBitBuffer;
-        CTFStream stream;
+        ICTFStream stream;
 
         if (!streamFile.canRead()) {
             throw new CTFException("Unreadable file : " //$NON-NLS-1$
@@ -473,13 +482,14 @@ public class CTFTrace implements IDefinitionScope {
             /* Shouldn't happen at this stage if every other check passed */
             throw new CTFException(e);
         }
-        if (fPacketHeaderDef != null) {
-            validateMagicNumber(fPacketHeaderDef);
+        final StructDefinition packetHeaderDef = getPacketHeaderDef();
+        if (packetHeaderDef != null) {
+            validateMagicNumber(packetHeaderDef);
 
-            validateUUID(fPacketHeaderDef);
+            validateUUID(packetHeaderDef);
 
             /* Read the stream ID */
-            IDefinition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
+            IDefinition streamIDDef = packetHeaderDef.lookupDefinition(MetadataStrings.STREAM_ID);
 
             if (streamIDDef instanceof IntegerDefinition) {
                 /* This doubles as a null check */
@@ -499,13 +509,16 @@ public class CTFTrace implements IDefinitionScope {
             throw new CTFException("Unexpected end of stream"); //$NON-NLS-1$
         }
 
+        if (!(stream instanceof CTFStream)) {
+            throw new CTFException("Stream is not a CTFStream, but rather a " + stream.getClass().getCanonicalName()); //$NON-NLS-1$
+        }
+        CTFStream ctfStream = (CTFStream) stream;
         /*
          * Create the stream input and add a reference to the streamInput in the
          * stream.
          */
-        stream.addInput(new CTFStreamInput(stream, streamFile));
-
-        return stream;
+        ctfStream.addInput(new CTFStreamInput(ctfStream, streamFile));
+        return ctfStream;
     }
 
     private void validateUUID(StructDefinition packetHeaderDef) throws CTFException {
@@ -552,7 +565,7 @@ public class CTFTrace implements IDefinitionScope {
     @Override
     public Definition lookupDefinition(String lookupPath) {
         if (lookupPath.equals(ILexicalScope.TRACE_PACKET_HEADER.getPath())) {
-            return fPacketHeaderDef;
+            return getPacketHeaderDef();
         }
         return null;
     }
@@ -581,8 +594,9 @@ public class CTFTrace implements IDefinitionScope {
      *            A stream object.
      * @throws ParseException
      *             If there was some problem reading the metadata
+     * @since 2.0
      */
-    public void addStream(CTFStream stream) throws ParseException {
+    public void addStream(ICTFStream stream) throws ParseException {
         /*
          * If there is already a stream without id (the null key), it must be
          * the only one
@@ -602,7 +616,7 @@ public class CTFTrace implements IDefinitionScope {
         /*
          * If a stream with the same ID already exists, it is not valid.
          */
-        CTFStream existingStream = fStreams.get(stream.getId());
+        ICTFStream existingStream = fStreams.get(stream.getId());
         if (existingStream != null) {
             throw new ParseException("Stream id already exists"); //$NON-NLS-1$
         }
@@ -621,18 +635,6 @@ public class CTFTrace implements IDefinitionScope {
         return Collections.unmodifiableMap(fEnvironment);
     }
 
-    /**
-     * 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) {
-        fEnvironment.put(varName, varValue);
-    }
-
     /**
      * Add a clock to the clock list
      *
@@ -704,7 +706,7 @@ public class CTFTrace implements IDefinitionScope {
      */
     public long getCurrentStartTime() {
         long currentStart = Long.MAX_VALUE;
-        for (CTFStream stream : fStreams.values()) {
+        for (ICTFStream stream : fStreams.values()) {
             for (CTFStreamInput si : stream.getStreamInputs()) {
                 currentStart = Math.min(currentStart, si.getIndex().getElement(0).getTimestampBegin());
             }
@@ -719,7 +721,7 @@ public class CTFTrace implements IDefinitionScope {
      */
     public long getCurrentEndTime() {
         long currentEnd = Long.MIN_VALUE;
-        for (CTFStream stream : fStreams.values()) {
+        for (ICTFStream stream : fStreams.values()) {
             for (CTFStreamInput si : stream.getStreamInputs()) {
                 currentEnd = Math.max(currentEnd, si.getTimestampEnd());
             }
@@ -798,18 +800,21 @@ public class CTFTrace implements IDefinitionScope {
      *             The file must exist
      */
     public void addStream(long id, File streamFile) throws CTFException {
-        CTFStream stream = null;
         final File file = streamFile;
         if (file == null) {
             throw new CTFException("cannot create a stream with no file"); //$NON-NLS-1$
         }
-        if (fStreams.containsKey(id)) {
-            stream = fStreams.get(id);
-        } else {
+        ICTFStream stream = fStreams.get(id);
+        if (stream == null) {
             stream = new CTFStream(this);
             fStreams.put(id, stream);
         }
-        stream.addInput(new CTFStreamInput(stream, file));
+        if (stream instanceof CTFStream) {
+            CTFStream ctfStream = (CTFStream) stream;
+            ctfStream.addInput(new CTFStreamInput(stream, file));
+        } else {
+            throw new CTFException("Stream does not support adding input"); //$NON-NLS-1$
+        }
     }
 
     /**
@@ -822,6 +827,28 @@ public class CTFTrace implements IDefinitionScope {
     public DeclarationScope getScope() {
         return fScope;
     }
+
+    /**
+     * Gets the packet header definition (UUID, magic number and such)
+     *
+     * @return the packet header definition
+     *
+     * @since 2.0
+     */
+    public StructDefinition getPacketHeaderDef() {
+        return fPacketHeaderDef;
+    }
+
+    /**
+     * Sets the environment map
+     *
+     * @param parseEnvironment
+     *            The environment map
+     * @since 2.0
+     */
+    public void setEnvironment(@NonNull Map<String, String> parseEnvironment) {
+        fEnvironment = ImmutableMap.copyOf(parseEnvironment);
+    }
 }
 
 class MetadataFileFilter implements FileFilter {
This page took 0.027361 seconds and 5 git commands to generate.