Rationalize ITmfTrace.initTrace()
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / experiment / TmfExperimentContext.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.experiment;
14
15 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
16 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
17 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
18 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
19 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
20
21 /**
22 * <b><u>TmfExperimentContext</u></b>
23 * <p>
24 * The experiment keeps track of the next event from each of its traces so
25 * it can pick the next one in chronological order.
26 * <p>
27 * This implies that the "next" event from each trace has already been
28 * read and that we at least know its timestamp. This doesn't imply that a
29 * full parse of the event content was performed (read: LTTng works like
30 * this).
31 * <p>
32 * The last trace refers to the trace from which the last event was
33 * "consumed" at the experiment level.
34 */
35 public class TmfExperimentContext extends TmfContext {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
41 public static final int NO_TRACE = -1;
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 private ITmfTrace<?>[] fTraces = new ITmfTrace[0];
48 private ITmfContext[] fContexts;
49 private ITmfEvent[] fEvents;
50 private int lastTraceRead;
51
52 // ------------------------------------------------------------------------
53 // Constructors
54 // ------------------------------------------------------------------------
55
56 public TmfExperimentContext(ITmfTrace<?>[] traces, ITmfContext[] contexts) {
57 super();
58 fTraces = traces;
59 fContexts = contexts;
60 fEvents = new ITmfEvent[fTraces.length];
61
62 ITmfLocation<?>[] locations = new ITmfLocation[fTraces.length];
63 long[] ranks = new long[fTraces.length];
64 long rank = 0;
65 for (int i = 0; i < fTraces.length; i++) {
66 if (contexts[i] != null) {
67 locations[i] = contexts[i].getLocation();
68 ranks[i] = contexts[i].getRank();
69 rank += contexts[i].getRank();
70 }
71 }
72
73 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations), ranks));
74 setRank(rank);
75 lastTraceRead = NO_TRACE;
76 }
77
78 public TmfExperimentContext(ITmfTrace<?>[] traces) {
79 this(traces, new TmfContext[traces.length]);
80 }
81
82 public TmfExperimentContext(TmfExperimentContext other) {
83 this(other.fTraces, other.cloneContexts());
84 fEvents = other.fEvents;
85 if (other.getLocation() != null)
86 setLocation(other.getLocation().clone());
87 setRank(other.getRank());
88 setLastTrace(other.lastTraceRead);
89 }
90
91 private ITmfContext[] cloneContexts() {
92 ITmfContext[] contexts = new TmfContext[fContexts.length];
93 for (int i = 0; i < fContexts.length; i++)
94 contexts[i] = fContexts[i].clone();
95 return contexts;
96 }
97
98 // ------------------------------------------------------------------------
99 // Accessors
100 // ------------------------------------------------------------------------
101
102 public ITmfTrace<?>[] getTraces() {
103 return fTraces;
104 }
105
106 public ITmfContext[] getContexts() {
107 return fContexts;
108 }
109
110 public ITmfEvent[] getEvents() {
111 return fEvents;
112 }
113
114 public int getLastTrace() {
115 return lastTraceRead;
116 }
117
118 public void setLastTrace(int newIndex) {
119 lastTraceRead = newIndex;
120 }
121
122 // ------------------------------------------------------------------------
123 // Object
124 // ------------------------------------------------------------------------
125
126 @Override
127 public int hashCode() {
128 int result = 17;
129 for (int i = 0; i < fTraces.length; i++) {
130 result = 37 * result + fTraces[i].hashCode();
131 result = 37 * result + fContexts[i].hashCode();
132 }
133 return result;
134 }
135
136 @Override
137 public boolean equals(Object other) {
138 if (this == other)
139 return true;
140 if (!super.equals(other))
141 return false;
142 if (!(other instanceof TmfExperimentContext)) {
143 return false;
144 }
145 TmfExperimentContext o = (TmfExperimentContext) other;
146 boolean isEqual = true;
147 int i = 0;
148 while (isEqual && i < fTraces.length) {
149 isEqual &= fTraces[i].equals(o.fTraces[i]);
150 isEqual &= fContexts[i].equals(o.fContexts[i]);
151 i++;
152 }
153 return isEqual;
154 }
155
156 }
This page took 0.035761 seconds and 5 git commands to generate.