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 | |
72f1e62a | 17 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
34ccf9a9 | 18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b | 19 | import 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 | 27 | * This implies that the "next" event from each trace has already been |
ea271da6 | 28 | * read and that we at least know its timestamp. |
8f50c396 | 29 | * <p> |
cbdacf03 FC |
30 | * The last trace refers to the trace from which the last event was "consumed" |
31 | * at the experiment level. | |
8c8bf09f | 32 | */ |
9b749023 | 33 | public class TmfExperimentContext extends TmfContext { |
9f584e4c | 34 | |
cbdacf03 FC |
35 | // ------------------------------------------------------------------------ |
36 | // Constants | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
0316808c FC |
39 | /** |
40 | * No last trace read indicator | |
41 | */ | |
9e0640dc | 42 | public static final int NO_TRACE = -1; |
cbdacf03 FC |
43 | |
44 | // ------------------------------------------------------------------------ | |
45 | // Attributes | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
03648eab | 48 | private ITmfContext[] fContexts; |
ce2388e0 | 49 | private ITmfEvent[] fEvents; |
03648eab | 50 | private int fLastTraceRead; |
cbdacf03 FC |
51 | |
52 | // ------------------------------------------------------------------------ | |
53 | // Constructors | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
408e65d2 | 56 | /** |
063f0d27 AM |
57 | * Standard constructor |
58 | * | |
ea271da6 PT |
59 | * @param nbTraces |
60 | * The number of traces in the experiment | |
408e65d2 | 61 | */ |
ea271da6 | 62 | public TmfExperimentContext(final int nbTraces) { |
cbdacf03 | 63 | super(); |
ea271da6 PT |
64 | fContexts = new ITmfContext[nbTraces]; |
65 | fEvents = new ITmfEvent[nbTraces]; | |
03648eab | 66 | fLastTraceRead = NO_TRACE; |
cbdacf03 FC |
67 | } |
68 | ||
4c9f2944 PT |
69 | @Override |
70 | public void dispose() { | |
71 | for (ITmfContext context : fContexts) { | |
72 | context.dispose(); | |
73 | } | |
74 | super.dispose(); | |
75 | } | |
76 | ||
cbdacf03 FC |
77 | // ------------------------------------------------------------------------ |
78 | // Accessors | |
79 | // ------------------------------------------------------------------------ | |
80 | ||
063f0d27 AM |
81 | /** |
82 | * Get the trace contexts composing this experiment context. | |
83 | * | |
84 | * @return The array of trace contexts | |
85 | */ | |
cbdacf03 FC |
86 | public ITmfContext[] getContexts() { |
87 | return fContexts; | |
88 | } | |
89 | ||
063f0d27 AM |
90 | /** |
91 | * Get the trace events located at this experiment context's location. | |
92 | * | |
93 | * @return The array of trace events | |
94 | */ | |
cbdacf03 FC |
95 | public ITmfEvent[] getEvents() { |
96 | return fEvents; | |
97 | } | |
98 | ||
063f0d27 AM |
99 | /** |
100 | * Get the index of the trace that was last read (so the trace whose | |
101 | * current context will match this experiment's). | |
102 | * | |
103 | * @return The index of the trace | |
104 | */ | |
cbdacf03 | 105 | public int getLastTrace() { |
03648eab | 106 | return fLastTraceRead; |
cbdacf03 FC |
107 | } |
108 | ||
063f0d27 AM |
109 | /** |
110 | * Set the last trace read index | |
111 | * | |
112 | * @param newIndex | |
113 | * The new value to assign | |
114 | */ | |
cbdacf03 | 115 | public void setLastTrace(final int newIndex) { |
03648eab | 116 | fLastTraceRead = newIndex; |
cbdacf03 FC |
117 | } |
118 | ||
119 | // ------------------------------------------------------------------------ | |
120 | // Object | |
121 | // ------------------------------------------------------------------------ | |
550d787e FC |
122 | |
123 | @Override | |
124 | public int hashCode() { | |
cbdacf03 | 125 | int result = 17; |
0316808c | 126 | for (int i = 0; i < fContexts.length; i++) { |
cbdacf03 FC |
127 | result = 37 * result + fContexts[i].hashCode(); |
128 | } | |
129 | return result; | |
550d787e | 130 | } |
cbdacf03 | 131 | |
550d787e | 132 | @Override |
cbdacf03 | 133 | public boolean equals(final Object other) { |
9b749023 | 134 | if (this == other) { |
6e85c58d | 135 | return true; |
9b749023 AM |
136 | } |
137 | if (!super.equals(other)) { | |
6e85c58d | 138 | return false; |
9b749023 AM |
139 | } |
140 | if (!(other instanceof TmfExperimentContext)) { | |
cbdacf03 | 141 | return false; |
9b749023 | 142 | } |
cbdacf03 FC |
143 | final TmfExperimentContext o = (TmfExperimentContext) other; |
144 | boolean isEqual = true; | |
145 | int i = 0; | |
0316808c | 146 | while (isEqual && (i < fContexts.length)) { |
cbdacf03 FC |
147 | isEqual &= fContexts[i].equals(o.fContexts[i]); |
148 | i++; | |
149 | } | |
150 | return isEqual; | |
550d787e | 151 | } |
cbdacf03 | 152 | |
3bd44ac8 FC |
153 | @Override |
154 | @SuppressWarnings("nls") | |
155 | public String toString() { | |
156 | StringBuilder sb = new StringBuilder("TmfExperimentContext [\n"); | |
157 | sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n"); | |
158 | sb.append("\tfContexts=["); | |
159 | for (int i = 0; i < fContexts.length; i++) { | |
160 | sb.append("(" + fContexts[i].getLocation() + "," + fContexts[i].getRank() + ((i < fContexts.length - 1) ? ")," : ")]\n")); | |
161 | } | |
162 | sb.append("\tfEvents=["); | |
163 | for (int i = 0; i < fEvents.length; i++) { | |
164 | ITmfEvent event = fEvents[i]; | |
165 | sb.append(((event != null) ? fEvents[i].getTimestamp() : "(null)") + ((i < fEvents.length - 1) ? "," : "]\n")); | |
166 | } | |
167 | sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n"); | |
168 | sb.append("]"); | |
169 | return sb.toString(); | |
170 | } | |
171 | ||
8c8bf09f | 172 | } |