Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / trace / TmfExperimentContext.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Patrick Tasse - Updated for removal of context clone
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.internal.tmf.core.trace;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
23 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
24
25 /**
26 * The experiment context in TMF.
27 * <p>
28 * The experiment keeps track of the next event from each of its traces so it
29 * can pick the next one in chronological order.
30 * <p>
31 * This implies that the "next" event from each trace has already been
32 * read and that we at least know its timestamp.
33 * <p>
34 * The last trace refers to the trace from which the last event was "consumed"
35 * at the experiment level.
36 */
37 public final class TmfExperimentContext extends TmfContext {
38
39 // ------------------------------------------------------------------------
40 // Constants
41 // ------------------------------------------------------------------------
42
43 /**
44 * No last trace read indicator
45 */
46 public static final int NO_TRACE = -1;
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51
52 private final List<ITmfContext> fContexts;
53 private final List<ITmfEvent> fEvents;
54 private int fLastTraceRead;
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 /**
61 * Standard constructor
62 *
63 * @param nbTraces
64 * The number of traces in the experiment
65 */
66 public TmfExperimentContext(final int nbTraces) {
67 super();
68 fLastTraceRead = NO_TRACE;
69 fContexts = new ArrayList<>(nbTraces);
70 fEvents = new ArrayList<>(nbTraces);
71
72
73 /* Initialize the arrays to the requested size */
74 for (int i = 0; i < nbTraces; i++) {
75 fContexts.add(null);
76 fEvents.add(null);
77 }
78 }
79
80 @Override
81 public void dispose() {
82 for (ITmfContext context : fContexts) {
83 context.dispose();
84 }
85 super.dispose();
86 }
87
88 // ------------------------------------------------------------------------
89 // Accessors
90 // ------------------------------------------------------------------------
91
92 /**
93 * Return how many traces this experiment context tracks the contexts of
94 * (a.k.a., the number of traces in the experiment).
95 *
96 * @return The number of traces in the experiment
97 */
98 public int getNbTraces() {
99 return fContexts.size();
100 }
101
102 /**
103 * Get the current context of a specific trace
104 *
105 * @param traceIndex
106 * The index of the trace in the experiment
107 * @return The matching context object for that trace
108 */
109 @Nullable
110 public ITmfContext getContext(int traceIndex) {
111 return fContexts.get(traceIndex);
112 }
113
114 /**
115 * Set the context of a trace
116 *
117 * @param traceIndex
118 * The index of the trace in the experiment
119 * @param ctx
120 * The new context object for that trace
121 */
122 public void setContext(int traceIndex, ITmfContext ctx) {
123 fContexts.set(traceIndex, ctx);
124 }
125
126 /**
127 * Get the current event for a specific trace in the experiment.
128 *
129 * @param traceIndex
130 * The index of the trace in the experiment
131 * @return The event matching the trace/context
132 *
133 */
134 @Nullable
135 public ITmfEvent getEvent(int traceIndex) {
136 return fEvents.get(traceIndex);
137 }
138
139 /**
140 * Set the context's event for a specific trace
141 *
142 * @param traceIndex
143 * The index of the trace in the experiment
144 * @param event
145 * The event at the context in the trace
146 */
147 public void setEvent(int traceIndex, ITmfEvent event) {
148 fEvents.set(traceIndex, event);
149 }
150
151 /**
152 * Get the index of the trace that was last read (so the trace whose
153 * current context will match this experiment's).
154 *
155 * @return The index of the trace
156 */
157 public int getLastTrace() {
158 return fLastTraceRead;
159 }
160
161 /**
162 * Set the last trace read index
163 *
164 * @param newIndex
165 * The new value to assign
166 */
167 public void setLastTrace(final int newIndex) {
168 fLastTraceRead = newIndex;
169 }
170
171 // ------------------------------------------------------------------------
172 // Object
173 // ------------------------------------------------------------------------
174
175 @Override
176 public int hashCode() {
177 int result = 17;
178 for (int i = 0; i < fContexts.size(); i++) {
179 result = 37 * result + fContexts.get(i).hashCode();
180 }
181 return result;
182 }
183
184 @Override
185 public boolean equals(final Object other) {
186 if (this == other) {
187 return true;
188 }
189 if (!super.equals(other)) {
190 return false;
191 }
192 if (!(other instanceof TmfExperimentContext)) {
193 return false;
194 }
195 final TmfExperimentContext o = (TmfExperimentContext) other;
196 boolean isEqual = true;
197 int i = 0;
198 while (isEqual && (i < fContexts.size())) {
199 isEqual &= fContexts.get(i).equals(o.fContexts.get(i));
200 i++;
201 }
202 return isEqual;
203 }
204
205 @Override
206 @SuppressWarnings("nls")
207 public String toString() {
208 StringBuilder sb = new StringBuilder("TmfExperimentContext [\n");
209 sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
210 sb.append("\tfContexts=[");
211 for (int i = 0; i < fContexts.size(); i++) {
212 sb.append("(" + fContexts.get(i).getLocation() + "," + fContexts.get(i).getRank() + ((i < fContexts.size() - 1) ? ")," : ")]\n"));
213 }
214 sb.append("\tfEvents=[");
215 for (int i = 0; i < fEvents.size(); i++) {
216 ITmfEvent event = fEvents.get(i);
217 sb.append(((event != null) ? fEvents.get(i).getTimestamp() : "(null)") + ((i < fEvents.size() - 1) ? "," : "]\n"));
218 }
219 sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n");
220 sb.append("]");
221 return sb.toString();
222 }
223
224 }
This page took 0.036385 seconds and 5 git commands to generate.