Fix for bug 381411: Implement ranked location in experiment.
[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.TmfContext;
19
20 /**
21 * The experiment context in TMF.
22 * <p>
23 * The experiment keeps track of the next event from each of its traces so it
24 * can pick the next one in chronological order.
25 * <p>
26 * This implies that the "next" event from each trace has already been
27 * read and that we at least know its timestamp. This doesn't imply that a
28 * full parse of the event content was performed (read: the legacy LTTng works
29 * like this...).
30 * <p>
31 * The last trace refers to the trace from which the last event was "consumed"
32 * at the experiment level.
33 */
34 public class TmfExperimentContext extends TmfContext implements Cloneable {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39
40 /**
41 * No last trace read indicator
42 */
43 public static final int NO_TRACE = -1;
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
49 private ITmfContext[] fContexts;
50 private ITmfEvent[] fEvents;
51 private int fLastTraceRead;
52
53 // ------------------------------------------------------------------------
54 // Constructors
55 // ------------------------------------------------------------------------
56
57 /**
58 * @param contexts
59 */
60 public TmfExperimentContext(final ITmfContext[] contexts) {
61 super();
62 fContexts = contexts;
63 fEvents = new ITmfEvent[fContexts.length];
64 final TmfRankedLocation[] locations = new TmfRankedLocation[fContexts.length];
65
66 long rank = 0;
67 for (int i = 0; i < fContexts.length; i++) {
68 if (contexts[i] != null) {
69 locations[i] = new TmfRankedLocation(contexts[i]);
70 rank += contexts[i].getRank();
71 }
72 }
73
74 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
75 setRank(rank);
76 fLastTraceRead = NO_TRACE;
77 }
78
79 /**
80 * @param other
81 */
82 public TmfExperimentContext(final TmfExperimentContext other) {
83 this(other.cloneContexts());
84 fEvents = other.fEvents;
85 if (other.getLocation() != null)
86 setLocation(other.getLocation().clone());
87 setRank(other.getRank());
88 setLastTrace(other.fLastTraceRead);
89 }
90
91 /* (non-Javadoc)
92 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
93 */
94 @Override
95 public TmfExperimentContext clone() {
96 TmfExperimentContext clone = null;
97 clone = (TmfExperimentContext) super.clone();
98 clone.fContexts = cloneContexts();
99 clone.fEvents = cloneEvents();
100 clone.fLastTraceRead = fLastTraceRead;
101 return clone;
102 }
103
104 private ITmfContext[] cloneContexts() {
105 final ITmfContext[] contexts = new ITmfContext[fContexts.length];
106 for (int i = 0; i < fContexts.length; i++)
107 contexts[i] = (fContexts[i] != null) ? fContexts[i].clone() : null;
108 return contexts;
109 }
110
111 private ITmfEvent[] cloneEvents() {
112 final ITmfEvent[] events = new ITmfEvent[fEvents.length];
113 for (int i = 0; i < fEvents.length; i++)
114 events[i] = (fEvents[i] != null) ? fEvents[i].clone() : null;
115 return events;
116 }
117
118 // ------------------------------------------------------------------------
119 // Accessors
120 // ------------------------------------------------------------------------
121
122 public ITmfContext[] getContexts() {
123 return fContexts;
124 }
125
126 public ITmfEvent[] getEvents() {
127 return fEvents;
128 }
129
130 public int getLastTrace() {
131 return fLastTraceRead;
132 }
133
134 public void setLastTrace(final int newIndex) {
135 fLastTraceRead = newIndex;
136 }
137
138 // ------------------------------------------------------------------------
139 // Object
140 // ------------------------------------------------------------------------
141
142 @Override
143 public int hashCode() {
144 int result = 17;
145 for (int i = 0; i < fContexts.length; i++) {
146 result = 37 * result + fContexts[i].hashCode();
147 }
148 return result;
149 }
150
151 @Override
152 public boolean equals(final Object other) {
153 if (this == other)
154 return true;
155 if (!super.equals(other))
156 return false;
157 if (!(other instanceof TmfExperimentContext))
158 return false;
159 final TmfExperimentContext o = (TmfExperimentContext) other;
160 boolean isEqual = true;
161 int i = 0;
162 while (isEqual && (i < fContexts.length)) {
163 isEqual &= fContexts[i].equals(o.fContexts[i]);
164 i++;
165 }
166 return isEqual;
167 }
168
169 @Override
170 @SuppressWarnings("nls")
171 public String toString() {
172 StringBuilder sb = new StringBuilder("TmfExperimentContext [\n");
173 sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
174 sb.append("\tfContexts=[");
175 for (int i = 0; i < fContexts.length; i++) {
176 sb.append("(" + fContexts[i].getLocation() + "," + fContexts[i].getRank() + ((i < fContexts.length - 1) ? ")," : ")]\n"));
177 }
178 sb.append("\tfEvents=[");
179 for (int i = 0; i < fEvents.length; i++) {
180 ITmfEvent event = fEvents[i];
181 sb.append(((event != null) ? fEvents[i].getTimestamp() : "(null)") + ((i < fEvents.length - 1) ? "," : "]\n"));
182 }
183 sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n");
184 sb.append("]");
185 return sb.toString();
186 }
187
188 }
This page took 0.034232 seconds and 5 git commands to generate.