ctf: Move CTF plugins to Java 7 and fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / EventDefinition.java
index 9ca6fee7584bd2c75fc73c608059e6403a9845d2..6008a4891e7e17b69567c027d3f29b464704a366 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * 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
 
 package org.eclipse.linuxtools.ctf.core.event;
 
-import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
 
 /**
- * <b><u>EventDefinition</u></b>
- * <p>
- * Represents an instance of an event.
+ * Representation of a particular instance of an event.
  */
 public class EventDefinition implements IDefinitionScope {
 
@@ -34,7 +34,7 @@ public class EventDefinition implements IDefinitionScope {
     /**
      * The corresponding event declaration.
      */
-    private final EventDeclaration declaration;
+    private final IEventDeclaration declaration;
 
     /**
      * The timestamp of the current event.
@@ -64,9 +64,12 @@ public class EventDefinition implements IDefinitionScope {
      * Constructs an event definition.
      *
      * @param declaration
-     *            The corresponding event declaration.
+     *            The corresponding event declaration
+     * @param streamInputReader
+     *            The SIR from where this EventDef was read
+     * @since 2.0
      */
-    public EventDefinition(EventDeclaration declaration,
+    public EventDefinition(IEventDeclaration declaration,
             StreamInputReader streamInputReader) {
         this.declaration = declaration;
         this.streamInputReader = streamInputReader;
@@ -85,8 +88,9 @@ public class EventDefinition implements IDefinitionScope {
      * Gets the declaration (the form) of the data
      *
      * @return the event declaration
+     * @since 2.0
      */
-    public EventDeclaration getDeclaration() {
+    public IEventDeclaration getDeclaration() {
         return declaration;
     }
 
@@ -100,14 +104,66 @@ public class EventDefinition implements IDefinitionScope {
     }
 
     /**
-     * Gets the context of this event
+     * Gets the context of this event without the context of the stream
      *
      * @return the context in struct form
+     * @since 1.2
      */
-    public StructDefinition getContext() {
+    public StructDefinition getEventContext() {
         return context;
     }
 
+    /**
+     * Gets the context of this event within a stream
+     *
+     * @return the context in struct form
+     */
+    public StructDefinition getContext() {
+        final StructDefinition streamContext =
+                streamInputReader.getPacketReader().getStreamEventContextDef();
+
+        /* Most common case so far */
+        if (streamContext == null) {
+            return context;
+        }
+
+        /* streamContext is not null, but the context of the event is null */
+        if (context == null) {
+            return streamContext;
+        }
+
+        /* The stream context and event context are assigned. */
+        StructDeclaration mergedDeclaration = new StructDeclaration(1);
+
+        /* Add fields from the stream */
+        Map<String, Definition> defs = streamContext.getDefinitions();
+        for (Entry<String, Definition> entry : defs.entrySet()) {
+            mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
+        }
+
+        /* Add fields from the event context, overwrite the stream ones if needed. */
+        for (Entry<String, Definition> entry : context.getDefinitions().entrySet()) {
+            mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
+        }
+
+        StructDefinition mergedContext = mergedDeclaration.createDefinition(null, "context"); //$NON-NLS-1$
+        for (String key : mergedContext.getDefinitions().keySet()) {
+            final Definition lookupDefinition = context.lookupDefinition(key);
+            /*
+             * If the key is in the event context, add it from there, if it is
+             * not, then it's in the stream. There is a priority with scoping so
+             * if there is a field like "context" in both stream and context,
+             * you display the context.
+             */
+            if (lookupDefinition != null) {
+                mergedContext.getDefinitions().put(key, lookupDefinition);
+            } else {
+                mergedContext.getDefinitions().put(key, streamContext.lookupDefinition(key));
+            }
+        }
+        return mergedContext;
+    }
+
     /**
      * Gets the stream input reader that this event was made by
      *
@@ -183,7 +239,7 @@ public class EventDefinition implements IDefinitionScope {
 
     @Override
     public String toString() {
-        HashMap<String, Definition> definitions;
+        Map<String, Definition> definitions;
         List<String> list;
         StringBuilder retString = new StringBuilder();
         final String cr = System.getProperty("line.separator");//$NON-NLS-1$
This page took 0.025821 seconds and 5 git commands to generate.