Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 2013 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 |
ea271da6 | 12 | * Patrick Tasse - Updated for removal of context clone |
8c8bf09f ASL |
13 | *******************************************************************************/ |
14 | ||
9e0640dc | 15 | package org.eclipse.linuxtools.internal.tmf.core.trace; |
8c8bf09f | 16 | |
07ef7847 AM |
17 | import java.util.ArrayList; |
18 | import java.util.List; | |
19 | ||
20 | import org.eclipse.jdt.annotation.Nullable; | |
72f1e62a | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
34ccf9a9 | 22 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b | 23 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
8c8bf09f ASL |
24 | |
25 | /** | |
0316808c | 26 | * The experiment context in TMF. |
8c8bf09f | 27 | * <p> |
cbdacf03 FC |
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. | |
8f50c396 | 30 | * <p> |
ce2388e0 | 31 | * This implies that the "next" event from each trace has already been |
ea271da6 | 32 | * read and that we at least know its timestamp. |
8f50c396 | 33 | * <p> |
cbdacf03 FC |
34 | * The last trace refers to the trace from which the last event was "consumed" |
35 | * at the experiment level. | |
8c8bf09f | 36 | */ |
07ef7847 | 37 | public final class TmfExperimentContext extends TmfContext { |
9f584e4c | 38 | |
cbdacf03 FC |
39 | // ------------------------------------------------------------------------ |
40 | // Constants | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
0316808c FC |
43 | /** |
44 | * No last trace read indicator | |
45 | */ | |
9e0640dc | 46 | public static final int NO_TRACE = -1; |
cbdacf03 FC |
47 | |
48 | // ------------------------------------------------------------------------ | |
49 | // Attributes | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
07ef7847 AM |
52 | private final List<ITmfContext> fContexts; |
53 | private final List<ITmfEvent> fEvents; | |
03648eab | 54 | private int fLastTraceRead; |
cbdacf03 FC |
55 | |
56 | // ------------------------------------------------------------------------ | |
57 | // Constructors | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
408e65d2 | 60 | /** |
063f0d27 AM |
61 | * Standard constructor |
62 | * | |
ea271da6 PT |
63 | * @param nbTraces |
64 | * The number of traces in the experiment | |
408e65d2 | 65 | */ |
ea271da6 | 66 | public TmfExperimentContext(final int nbTraces) { |
cbdacf03 | 67 | super(); |
03648eab | 68 | fLastTraceRead = NO_TRACE; |
07ef7847 AM |
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 | } | |
cbdacf03 FC |
78 | } |
79 | ||
4c9f2944 PT |
80 | @Override |
81 | public void dispose() { | |
82 | for (ITmfContext context : fContexts) { | |
83 | context.dispose(); | |
84 | } | |
85 | super.dispose(); | |
86 | } | |
87 | ||
cbdacf03 FC |
88 | // ------------------------------------------------------------------------ |
89 | // Accessors | |
90 | // ------------------------------------------------------------------------ | |
91 | ||
063f0d27 | 92 | /** |
07ef7847 AM |
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 | |
063f0d27 | 132 | * |
063f0d27 | 133 | */ |
07ef7847 AM |
134 | @Nullable |
135 | public ITmfEvent getEvent(int traceIndex) { | |
136 | return fEvents.get(traceIndex); | |
cbdacf03 FC |
137 | } |
138 | ||
063f0d27 | 139 | /** |
07ef7847 | 140 | * Set the context's event for a specific trace |
063f0d27 | 141 | * |
07ef7847 AM |
142 | * @param traceIndex |
143 | * The index of the trace in the experiment | |
144 | * @param event | |
145 | * The event at the context in the trace | |
063f0d27 | 146 | */ |
07ef7847 AM |
147 | public void setEvent(int traceIndex, ITmfEvent event) { |
148 | fEvents.set(traceIndex, event); | |
cbdacf03 FC |
149 | } |
150 | ||
063f0d27 AM |
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 | */ | |
cbdacf03 | 157 | public int getLastTrace() { |
03648eab | 158 | return fLastTraceRead; |
cbdacf03 FC |
159 | } |
160 | ||
063f0d27 AM |
161 | /** |
162 | * Set the last trace read index | |
163 | * | |
164 | * @param newIndex | |
165 | * The new value to assign | |
166 | */ | |
cbdacf03 | 167 | public void setLastTrace(final int newIndex) { |
03648eab | 168 | fLastTraceRead = newIndex; |
cbdacf03 FC |
169 | } |
170 | ||
171 | // ------------------------------------------------------------------------ | |
172 | // Object | |
173 | // ------------------------------------------------------------------------ | |
550d787e FC |
174 | |
175 | @Override | |
176 | public int hashCode() { | |
cbdacf03 | 177 | int result = 17; |
07ef7847 AM |
178 | for (int i = 0; i < fContexts.size(); i++) { |
179 | result = 37 * result + fContexts.get(i).hashCode(); | |
cbdacf03 FC |
180 | } |
181 | return result; | |
550d787e | 182 | } |
cbdacf03 | 183 | |
550d787e | 184 | @Override |
cbdacf03 | 185 | public boolean equals(final Object other) { |
9b749023 | 186 | if (this == other) { |
6e85c58d | 187 | return true; |
9b749023 AM |
188 | } |
189 | if (!super.equals(other)) { | |
6e85c58d | 190 | return false; |
9b749023 AM |
191 | } |
192 | if (!(other instanceof TmfExperimentContext)) { | |
cbdacf03 | 193 | return false; |
9b749023 | 194 | } |
cbdacf03 FC |
195 | final TmfExperimentContext o = (TmfExperimentContext) other; |
196 | boolean isEqual = true; | |
197 | int i = 0; | |
07ef7847 AM |
198 | while (isEqual && (i < fContexts.size())) { |
199 | isEqual &= fContexts.get(i).equals(o.fContexts.get(i)); | |
cbdacf03 FC |
200 | i++; |
201 | } | |
202 | return isEqual; | |
550d787e | 203 | } |
cbdacf03 | 204 | |
3bd44ac8 FC |
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=["); | |
07ef7847 AM |
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")); | |
3bd44ac8 FC |
213 | } |
214 | sb.append("\tfEvents=["); | |
07ef7847 AM |
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")); | |
3bd44ac8 FC |
218 | } |
219 | sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n"); | |
220 | sb.append("]"); | |
221 | return sb.toString(); | |
222 | } | |
223 | ||
8c8bf09f | 224 | } |