Remove the generic location (replace by Comparable)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / trace / TmfExperimentContext.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 * Francois Chouinard - Put in shape for 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.core.trace;
15
16 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
17 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
18 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
19 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
20
21 /**
22 * The experiment context in TMF.
23 * <p>
24 * The experiment keeps track of the next event from each of its traces so it
25 * 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: the legacy LTTng works
30 * like this...).
31 * <p>
32 * The last trace refers to the trace from which the last event was "consumed"
33 * at the experiment level.
34 */
35 public class TmfExperimentContext extends TmfContext {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
41 /**
42 * No last trace read indicator
43 */
44 public static final int NO_TRACE = -1;
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
50 private ITmfContext[] fContexts;
51 private ITmfEvent[] fEvents;
52 private int fLastTraceRead;
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
58 /**
59 * Standard constructor
60 *
61 * @param contexts
62 * The matching context for each trace in the experiment
63 */
64 public TmfExperimentContext(final ITmfContext[] contexts) {
65 super();
66 fContexts = contexts;
67 fEvents = new ITmfEvent[fContexts.length];
68 final ITmfLocation[] locations = new ITmfLocation[fContexts.length];
69
70 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations.clone())));
71
72 final long[] ranks = new long[fContexts.length];
73 long rank = 0;
74 for (int i = 0; i < fContexts.length; i++) {
75 if (contexts[i] != null) {
76 locations[i] = contexts[i].getLocation();
77 ranks[i] = contexts[i].getRank();
78 rank += contexts[i].getRank();
79 }
80 }
81
82 // setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
83 setRank(rank);
84 fLastTraceRead = NO_TRACE;
85 }
86
87 /**
88 * Copy constructor
89 *
90 * @param other
91 * The experiment context to copy
92 */
93 public TmfExperimentContext(final TmfExperimentContext other) {
94 this(other.cloneContexts());
95 fEvents = other.fEvents;
96 if (other.getLocation() != null) {
97 setLocation(other.getLocation().clone());
98 }
99 setRank(other.getRank());
100 setLastTrace(other.fLastTraceRead);
101 }
102
103 /* (non-Javadoc)
104 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
105 */
106 @Override
107 public TmfExperimentContext clone() {
108 TmfExperimentContext clone = null;
109 clone = (TmfExperimentContext) super.clone();
110 clone.fContexts = cloneContexts();
111 clone.fEvents = cloneEvents();
112 clone.fLastTraceRead = fLastTraceRead;
113 return clone;
114 }
115
116 private ITmfContext[] cloneContexts() {
117 final ITmfContext[] contexts = new ITmfContext[fContexts.length];
118 for (int i = 0; i < fContexts.length; i++) {
119 contexts[i] = (fContexts[i] != null) ? fContexts[i].clone() : null;
120 }
121 return contexts;
122 }
123
124 private ITmfEvent[] cloneEvents() {
125 final ITmfEvent[] events = new ITmfEvent[fEvents.length];
126 for (int i = 0; i < fEvents.length; i++) {
127 events[i] = (fEvents[i] != null) ? fEvents[i].clone() : null;
128 }
129 return events;
130 }
131
132 // ------------------------------------------------------------------------
133 // Accessors
134 // ------------------------------------------------------------------------
135
136 /**
137 * Get the trace contexts composing this experiment context.
138 *
139 * @return The array of trace contexts
140 */
141 public ITmfContext[] getContexts() {
142 return fContexts;
143 }
144
145 /**
146 * Get the trace events located at this experiment context's location.
147 *
148 * @return The array of trace events
149 */
150 public ITmfEvent[] getEvents() {
151 return fEvents;
152 }
153
154 /**
155 * Get the index of the trace that was last read (so the trace whose
156 * current context will match this experiment's).
157 *
158 * @return The index of the trace
159 */
160 public int getLastTrace() {
161 return fLastTraceRead;
162 }
163
164 /**
165 * Set the last trace read index
166 *
167 * @param newIndex
168 * The new value to assign
169 */
170 public void setLastTrace(final int newIndex) {
171 fLastTraceRead = newIndex;
172 }
173
174 // ------------------------------------------------------------------------
175 // Object
176 // ------------------------------------------------------------------------
177
178 @Override
179 public int hashCode() {
180 int result = 17;
181 for (int i = 0; i < fContexts.length; i++) {
182 result = 37 * result + fContexts[i].hashCode();
183 }
184 return result;
185 }
186
187 @Override
188 public boolean equals(final Object other) {
189 if (this == other) {
190 return true;
191 }
192 if (!super.equals(other)) {
193 return false;
194 }
195 if (!(other instanceof TmfExperimentContext)) {
196 return false;
197 }
198 final TmfExperimentContext o = (TmfExperimentContext) other;
199 boolean isEqual = true;
200 int i = 0;
201 while (isEqual && (i < fContexts.length)) {
202 isEqual &= fContexts[i].equals(o.fContexts[i]);
203 i++;
204 }
205 return isEqual;
206 }
207
208 @Override
209 @SuppressWarnings("nls")
210 public String toString() {
211 StringBuilder sb = new StringBuilder("TmfExperimentContext [\n");
212 sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
213 sb.append("\tfContexts=[");
214 for (int i = 0; i < fContexts.length; i++) {
215 sb.append("(" + fContexts[i].getLocation() + "," + fContexts[i].getRank() + ((i < fContexts.length - 1) ? ")," : ")]\n"));
216 }
217 sb.append("\tfEvents=[");
218 for (int i = 0; i < fEvents.length; i++) {
219 ITmfEvent event = fEvents[i];
220 sb.append(((event != null) ? fEvents[i].getTimestamp() : "(null)") + ((i < fEvents.length - 1) ? "," : "]\n"));
221 }
222 sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n");
223 sb.append("]");
224 return sb.toString();
225 }
226
227 }
This page took 0.037359 seconds and 6 git commands to generate.