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