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
CommitLineData
8c8bf09f 1/*******************************************************************************
0316808c 2 * Copyright (c) 2009, 2010, 2012 Ericsson
ce2388e0 3 *
8c8bf09f
ASL
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
ce2388e0 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
3bd44ac8 11 * Francois Chouinard - Put in shape for 1.0
8c8bf09f
ASL
12 *******************************************************************************/
13
9e0640dc 14package org.eclipse.linuxtools.internal.tmf.core.trace;
8c8bf09f 15
72f1e62a 16import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
34ccf9a9 17import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
5cc97265 18import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
6c13869b 19import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
8c8bf09f
ASL
20
21/**
0316808c 22 * The experiment context in TMF.
8c8bf09f 23 * <p>
cbdacf03
FC
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.
8f50c396 26 * <p>
ce2388e0
FC
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
0316808c
FC
29 * full parse of the event content was performed (read: the legacy LTTng works
30 * like this...).
8f50c396 31 * <p>
cbdacf03
FC
32 * The last trace refers to the trace from which the last event was "consumed"
33 * at the experiment level.
8c8bf09f 34 */
9b749023 35public class TmfExperimentContext extends TmfContext {
9f584e4c 36
cbdacf03
FC
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
0316808c
FC
41 /**
42 * No last trace read indicator
43 */
9e0640dc 44 public static final int NO_TRACE = -1;
cbdacf03
FC
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
03648eab 50 private ITmfContext[] fContexts;
ce2388e0 51 private ITmfEvent[] fEvents;
03648eab 52 private int fLastTraceRead;
cbdacf03
FC
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
408e65d2 58 /**
063f0d27
AM
59 * Standard constructor
60 *
408e65d2 61 * @param contexts
063f0d27 62 * The matching context for each trace in the experiment
408e65d2 63 */
0316808c 64 public TmfExperimentContext(final ITmfContext[] contexts) {
cbdacf03 65 super();
cbdacf03 66 fContexts = contexts;
0316808c 67 fEvents = new ITmfEvent[fContexts.length];
1e1bef82 68 final ITmfLocation[] locations = new ITmfLocation[fContexts.length];
3bd44ac8 69
5cc97265 70 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations.clone())));
9b749023 71
5cc97265 72 final long[] ranks = new long[fContexts.length];
cbdacf03 73 long rank = 0;
9b749023 74 for (int i = 0; i < fContexts.length; i++) {
cbdacf03 75 if (contexts[i] != null) {
5cc97265
FC
76 locations[i] = contexts[i].getLocation();
77 ranks[i] = contexts[i].getRank();
cbdacf03
FC
78 rank += contexts[i].getRank();
79 }
9b749023 80 }
cbdacf03 81
5cc97265 82// setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
cbdacf03 83 setRank(rank);
03648eab 84 fLastTraceRead = NO_TRACE;
cbdacf03
FC
85 }
86
408e65d2 87 /**
063f0d27
AM
88 * Copy constructor
89 *
408e65d2 90 * @param other
063f0d27 91 * The experiment context to copy
408e65d2 92 */
ce2388e0 93 public TmfExperimentContext(final TmfExperimentContext other) {
0316808c 94 this(other.cloneContexts());
ce2388e0 95 fEvents = other.fEvents;
9b749023 96 if (other.getLocation() != null) {
ce2388e0 97 setLocation(other.getLocation().clone());
9b749023 98 }
ce2388e0 99 setRank(other.getRank());
03648eab 100 setLastTrace(other.fLastTraceRead);
ce2388e0
FC
101 }
102
408e65d2
FC
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
ce2388e0
FC
116 private ITmfContext[] cloneContexts() {
117 final ITmfContext[] contexts = new ITmfContext[fContexts.length];
9b749023 118 for (int i = 0; i < fContexts.length; i++) {
408e65d2 119 contexts[i] = (fContexts[i] != null) ? fContexts[i].clone() : null;
9b749023 120 }
ce2388e0
FC
121 return contexts;
122 }
123
03648eab
FC
124 private ITmfEvent[] cloneEvents() {
125 final ITmfEvent[] events = new ITmfEvent[fEvents.length];
9b749023 126 for (int i = 0; i < fEvents.length; i++) {
408e65d2 127 events[i] = (fEvents[i] != null) ? fEvents[i].clone() : null;
9b749023 128 }
03648eab
FC
129 return events;
130 }
ce2388e0 131
cbdacf03
FC
132 // ------------------------------------------------------------------------
133 // Accessors
134 // ------------------------------------------------------------------------
135
063f0d27
AM
136 /**
137 * Get the trace contexts composing this experiment context.
138 *
139 * @return The array of trace contexts
140 */
cbdacf03
FC
141 public ITmfContext[] getContexts() {
142 return fContexts;
143 }
144
063f0d27
AM
145 /**
146 * Get the trace events located at this experiment context's location.
147 *
148 * @return The array of trace events
149 */
cbdacf03
FC
150 public ITmfEvent[] getEvents() {
151 return fEvents;
152 }
153
063f0d27
AM
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 */
cbdacf03 160 public int getLastTrace() {
03648eab 161 return fLastTraceRead;
cbdacf03
FC
162 }
163
063f0d27
AM
164 /**
165 * Set the last trace read index
166 *
167 * @param newIndex
168 * The new value to assign
169 */
cbdacf03 170 public void setLastTrace(final int newIndex) {
03648eab 171 fLastTraceRead = newIndex;
cbdacf03
FC
172 }
173
174 // ------------------------------------------------------------------------
175 // Object
176 // ------------------------------------------------------------------------
550d787e
FC
177
178 @Override
179 public int hashCode() {
cbdacf03 180 int result = 17;
0316808c 181 for (int i = 0; i < fContexts.length; i++) {
cbdacf03
FC
182 result = 37 * result + fContexts[i].hashCode();
183 }
184 return result;
550d787e 185 }
cbdacf03 186
550d787e 187 @Override
cbdacf03 188 public boolean equals(final Object other) {
9b749023 189 if (this == other) {
6e85c58d 190 return true;
9b749023
AM
191 }
192 if (!super.equals(other)) {
6e85c58d 193 return false;
9b749023
AM
194 }
195 if (!(other instanceof TmfExperimentContext)) {
cbdacf03 196 return false;
9b749023 197 }
cbdacf03
FC
198 final TmfExperimentContext o = (TmfExperimentContext) other;
199 boolean isEqual = true;
200 int i = 0;
0316808c 201 while (isEqual && (i < fContexts.length)) {
cbdacf03
FC
202 isEqual &= fContexts[i].equals(o.fContexts[i]);
203 i++;
204 }
205 return isEqual;
550d787e 206 }
cbdacf03 207
3bd44ac8
FC
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
8c8bf09f 227}
This page took 0.210202 seconds and 5 git commands to generate.