Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / experiment / TmfExperimentContext.java
index c868d0bc16ee383bde8f5413e149c191aa44fc87..3d4981cbc118f04d045dc1f05e3a39f8e12df4b8 100644 (file)
 
 package org.eclipse.linuxtools.tmf.experiment;
 
-import java.util.Vector;
-
-import org.eclipse.linuxtools.tmf.component.ITmfContext;
 import org.eclipse.linuxtools.tmf.event.TmfEvent;
+import org.eclipse.linuxtools.tmf.trace.ITmfLocation;
 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
-import org.eclipse.linuxtools.tmf.trace.TmfTraceContext;
+import org.eclipse.linuxtools.tmf.trace.TmfContext;
 
 /**
  * <b><u>TmfExperimentContext</u></b>
  * <p>
- * Implement me. Please.
+ * The experiment keeps track of the next event from each of its traces so
+ * it can pick the next one in chronological order.
+ * <p>
+ * This implies that the "next" event from each trace has already been
+ * read and that we at least know its timestamp. This doesn't imply that a
+ * full parse of the event content was performed (read: LTTng works like 
+ * this).
+ * <p>
+ * The last trace refers to the trace from which the last event was
+ * "consumed" at the experiment level.
  */
-public class TmfExperimentContext implements ITmfContext, Cloneable {
+public class TmfExperimentContext extends TmfContext {
+
+       // ------------------------------------------------------------------------
+       // Constants
+       // ------------------------------------------------------------------------
+       
+        public static final int NO_TRACE = -1;
+
+       // ------------------------------------------------------------------------
+       // Attributes
+       // ------------------------------------------------------------------------
+
+       private ITmfTrace[]  fTraces = new ITmfTrace[0];
+       private TmfContext[] fContexts;
+       private TmfEvent[]   fEvents;
+       private int lastTraceRead;
 
-       private ITmfTrace[]       fTraces = new ITmfTrace[0];   // The set of traces
-       private TmfTraceContext[] fContexts;                                    // The set of trace contexts
-       private TmfEvent[]        fEvents;
+       // ------------------------------------------------------------------------
+       // Constructors
+       // ------------------------------------------------------------------------
 
-       public TmfExperimentContext(Vector<ITmfTrace> traces) {
-               fTraces   = traces.toArray(fTraces);
-               fContexts = new TmfTraceContext[fTraces.length];
+       public TmfExperimentContext(ITmfTrace[] traces, TmfContext[] contexts) {
+               super();
+               fTraces   = traces;
+               fContexts = contexts;
                fEvents   = new TmfEvent[fTraces.length];
-       }
 
-       public TmfExperimentContext clone() {
-               try {
-                       return (TmfExperimentContext) super.clone();
-               } catch (CloneNotSupportedException e) {
-                       e.printStackTrace();
+               ITmfLocation<?>[] locations = new ITmfLocation[fTraces.length];
+               long[] ranks = new long[fTraces.length];
+               long rank = 0;
+               for (int i = 0; i < fTraces.length; i++) {
+                       if (contexts[i] != null) {
+                               locations[i] = contexts[i].getLocation();
+                               ranks[i] = contexts[i].getRank();
+                               rank += contexts[i].getRank();
+                       }
                }
-               return null;
+               
+               setLocation(new TmfExperimentLocation(new TmfLocationArray(locations), ranks));
+               setRank(rank);
+               lastTraceRead = NO_TRACE;
        }
 
-       public ITmfTrace[] getTraces() {
-               return fTraces;
+       public TmfExperimentContext(ITmfTrace[] traces) {
+               this(traces, new TmfContext[traces.length]);
        }
 
-       public TmfTraceContext[] getContexts() {
-               return fContexts;
+       public TmfExperimentContext(TmfExperimentContext other) {
+               this(other.fTraces, other.cloneContexts());
+               fEvents = other.fEvents;
+               if (other.getLocation() != null)
+                       setLocation(other.getLocation().clone());
+               setRank(other.getRank());
+               setLastTrace(other.lastTraceRead);
        }
 
-       public TmfTraceContext[] cloneContexts() {
-               TmfTraceContext[] contexts = new TmfTraceContext[fContexts.length];
+       private TmfContext[] cloneContexts() {
+               TmfContext[] contexts = new TmfContext[fContexts.length];
                for (int i = 0; i < fContexts.length; i++)
                        contexts[i] = fContexts[i].clone();
                return contexts;
        }
 
+       // ------------------------------------------------------------------------
+       // Accessors
+       // ------------------------------------------------------------------------
+
+       public ITmfTrace[] getTraces() {
+               return fTraces;
+       }
+
+       public TmfContext[] getContexts() {
+               return fContexts;
+       }
+
        public TmfEvent[] getEvents() {
                return fEvents;
        }
 
+       public int getLastTrace() {
+               return lastTraceRead;
+       }
+
+       public void setLastTrace(int newIndex) {
+               lastTraceRead = newIndex;
+       }
+
+       // ------------------------------------------------------------------------
+       // Object
+       // ------------------------------------------------------------------------
+
+    @Override
+    public int hashCode() {
+               int result = 17;
+       for (int i = 0; i < fTraces.length; i++) {
+               result = 37 * result + fTraces[i].hashCode();
+               result = 37 * result + fContexts[i].hashCode();
+       }
+       return result;
+    }
+    @Override
+    public boolean equals(Object other) {
+       if (!(other instanceof TmfExperimentContext)) {
+               return false;
+       }
+       TmfExperimentContext o = (TmfExperimentContext) other;
+       boolean isEqual = true;
+       int i = 0;
+       while (isEqual && i < fTraces.length) {
+               isEqual &= fTraces[i].equals(o.fTraces[i]);
+               isEqual &= fContexts[i].equals(o.fContexts[i]);
+               i++;
+       }
+       return isEqual;
+    }
 }
This page took 0.025429 seconds and 5 git commands to generate.