Fix #2 for bug381411
[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 implements Cloneable {
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 * @param contexts
60 */
61 public TmfExperimentContext(final ITmfContext[] contexts) {
62 super();
63 fContexts = contexts;
64 fEvents = new ITmfEvent[fContexts.length];
65 final ITmfLocation<?>[] locations = new ITmfLocation[fContexts.length];
66
67 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations.clone())));
68
69 final long[] ranks = new long[fContexts.length];
70 long rank = 0;
71 for (int i = 0; i < fContexts.length; i++)
72 if (contexts[i] != null) {
73 locations[i] = contexts[i].getLocation();
74 ranks[i] = contexts[i].getRank();
75 rank += contexts[i].getRank();
76 }
77
78 // setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
79 setRank(rank);
80 fLastTraceRead = NO_TRACE;
81 }
82
83 /**
84 * @param other
85 */
86 public TmfExperimentContext(final TmfExperimentContext other) {
87 this(other.cloneContexts());
88 fEvents = other.fEvents;
89 if (other.getLocation() != null)
90 setLocation(other.getLocation().clone());
91 setRank(other.getRank());
92 setLastTrace(other.fLastTraceRead);
93 }
94
95 /* (non-Javadoc)
96 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
97 */
98 @Override
99 public TmfExperimentContext clone() {
100 TmfExperimentContext clone = null;
101 clone = (TmfExperimentContext) super.clone();
102 clone.fContexts = cloneContexts();
103 clone.fEvents = cloneEvents();
104 clone.fLastTraceRead = fLastTraceRead;
105 return clone;
106 }
107
108 private ITmfContext[] cloneContexts() {
109 final ITmfContext[] contexts = new ITmfContext[fContexts.length];
110 for (int i = 0; i < fContexts.length; i++)
111 contexts[i] = (fContexts[i] != null) ? fContexts[i].clone() : null;
112 return contexts;
113 }
114
115 private ITmfEvent[] cloneEvents() {
116 final ITmfEvent[] events = new ITmfEvent[fEvents.length];
117 for (int i = 0; i < fEvents.length; i++)
118 events[i] = (fEvents[i] != null) ? fEvents[i].clone() : null;
119 return events;
120 }
121
122 // ------------------------------------------------------------------------
123 // Accessors
124 // ------------------------------------------------------------------------
125
126 public ITmfContext[] getContexts() {
127 return fContexts;
128 }
129
130 public ITmfEvent[] getEvents() {
131 return fEvents;
132 }
133
134 public int getLastTrace() {
135 return fLastTraceRead;
136 }
137
138 public void setLastTrace(final int newIndex) {
139 fLastTraceRead = newIndex;
140 }
141
142 // ------------------------------------------------------------------------
143 // Object
144 // ------------------------------------------------------------------------
145
146 @Override
147 public int hashCode() {
148 int result = 17;
149 for (int i = 0; i < fContexts.length; i++) {
150 result = 37 * result + fContexts[i].hashCode();
151 }
152 return result;
153 }
154
155 @Override
156 public boolean equals(final Object other) {
157 if (this == other)
158 return true;
159 if (!super.equals(other))
160 return false;
161 if (!(other instanceof TmfExperimentContext))
162 return false;
163 final TmfExperimentContext o = (TmfExperimentContext) other;
164 boolean isEqual = true;
165 int i = 0;
166 while (isEqual && (i < fContexts.length)) {
167 isEqual &= fContexts[i].equals(o.fContexts[i]);
168 i++;
169 }
170 return isEqual;
171 }
172
173 @Override
174 @SuppressWarnings("nls")
175 public String toString() {
176 StringBuilder sb = new StringBuilder("TmfExperimentContext [\n");
177 sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
178 sb.append("\tfContexts=[");
179 for (int i = 0; i < fContexts.length; i++) {
180 sb.append("(" + fContexts[i].getLocation() + "," + fContexts[i].getRank() + ((i < fContexts.length - 1) ? ")," : ")]\n"));
181 }
182 sb.append("\tfEvents=[");
183 for (int i = 0; i < fEvents.length; i++) {
184 ITmfEvent event = fEvents[i];
185 sb.append(((event != null) ? fEvents[i].getTimestamp() : "(null)") + ((i < fEvents.length - 1) ? "," : "]\n"));
186 }
187 sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n");
188 sb.append("]");
189 return sb.toString();
190 }
191
192 }
This page took 0.04157 seconds and 6 git commands to generate.