Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfIterator.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
12
13 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
14 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
15 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
16 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
17
18 /**
19 * The CTF trace reader iterator.
20 *
21 * It doesn't reserve a file handle, so many iterators can be used without worries
22 * of I/O errors or resource exhaustion.
23 *
24 * @version 1.0
25 * @author Matthew Khouzam
26 */
27 public class CtfIterator extends CTFTraceReader implements ITmfContext,
28 Comparable<CtfIterator>, Cloneable {
29
30 private final CtfTmfTrace ctfTmfTrace;
31
32 /**
33 * An invalid location
34 */
35 final public static CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION);
36
37 private CtfLocation curLocation;
38 private long curRank;
39
40 /**
41 * Create a new CTF trace iterator, which initially points at the first
42 * event in the trace.
43 *
44 * @param trace
45 * the trace to iterate over
46 */
47 public CtfIterator(final CtfTmfTrace trace) {
48 super(trace.getCTFTrace());
49 this.ctfTmfTrace = trace;
50 if (this.hasMoreEvents()) {
51 this.curLocation = new CtfLocation(trace.getStartTime());
52 this.curRank = 0;
53 } else {
54 setUnknownLocation();
55 }
56 }
57
58 private void setUnknownLocation() {
59 this.curLocation = NULL_LOCATION;
60 this.curRank = UNKNOWN_RANK;
61 }
62
63 /**
64 * Constructor for CtfIterator.
65 *
66 * @param trace
67 * CtfTmfTrace the trace
68 * @param ctfLocationData
69 * long the timestamp in ns of the trace for positioning
70 * @param rank
71 * long the index of the trace for positioning
72 * @since 2.0
73 */
74 public CtfIterator(final CtfTmfTrace trace,
75 final CtfLocationData ctfLocationData, final long rank) {
76 super(trace.getCTFTrace());
77
78 this.ctfTmfTrace = trace;
79 if (this.hasMoreEvents()) {
80 this.curLocation = new CtfLocation(ctfLocationData);
81 if (this.getCurrentEvent().getTimestampValue() != ctfLocationData.getTimestamp()) {
82 this.seek(ctfLocationData);
83 this.curRank = rank;
84 }
85 } else {
86 setUnknownLocation();
87 }
88
89 }
90
91 /**
92 * Method getCtfTmfTrace. gets a CtfTmfTrace
93 * @return CtfTmfTrace
94 */
95 public CtfTmfTrace getCtfTmfTrace() {
96 return ctfTmfTrace;
97 }
98
99 /**
100 * Method getCurrentEvent. gets the current event
101 * @return CtfTmfEvent
102 */
103 public CtfTmfEvent getCurrentEvent() {
104 final StreamInputReader top = super.prio.peek();
105 if (top != null) {
106 return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(),
107 ctfTmfTrace);
108 }
109 return null;
110 }
111
112 /**
113 * Seek this iterator to a given location.
114 *
115 * @param ctfLocationData
116 * The LocationData representing the position to seek to
117 * @return boolean
118 * @since 2.0
119 */
120 public boolean seek(final CtfLocationData ctfLocationData) {
121 boolean ret = false;
122
123 /* Adjust the timestamp depending on the trace's offset */
124 long currTimestamp = ctfLocationData.getTimestamp();
125 final long offsetTimestamp = currTimestamp - this.getTrace().getOffset();
126 if (offsetTimestamp < 0) {
127 ret = super.seek(0L);
128 } else {
129 ret = super.seek(offsetTimestamp);
130 }
131
132 /*
133 * Check if there is already one or more events for that timestamp, and
134 * assign the location index correctly
135 */
136 currTimestamp = this.getCurrentEvent().getTimestampValue();
137 long index = 0;
138 for (long i = 0; i < ctfLocationData.getIndex(); i++) {
139 if (currTimestamp == this.getCurrentEvent().getTimestampValue()) {
140 index++;
141 } else {
142 index = 0;
143 }
144 this.advance();
145 }
146
147 /* Seek the current location accordingly */
148 if (ret) {
149 curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestampValue(), index));
150 } else {
151 curLocation = NULL_LOCATION;
152 }
153 return ret;
154 }
155
156 /**
157 * Method getRank.
158 * @return long
159 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
160 */
161 @Override
162 public long getRank() {
163 return curRank;
164 }
165
166 /**
167 * Method setRank.
168 * @param rank long
169 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
170 */
171 @Override
172 public void setRank(final long rank) {
173 curRank = rank;
174 }
175
176 /*
177 * (non-Javadoc)
178 *
179 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
180 */
181 @Override
182 public CtfIterator clone() {
183 CtfIterator clone = null;
184 clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocation(), curRank);
185 return clone;
186 }
187
188 /**
189 * Method dispose.
190 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
191 */
192 @Override
193 public void dispose() {
194 // FIXME add dispose() stuff to CTFTrace and call it here...
195
196 }
197
198 /**
199 * Method setLocation.
200 * @param location ITmfLocation<?>
201 */
202 @Override
203 public void setLocation(final ITmfLocation<?> location) {
204 // FIXME alex: isn't there a cleaner way than a cast here?
205 this.curLocation = (CtfLocation) location;
206 seek(((CtfLocation) location).getLocation());
207 }
208
209 /**
210 * Method getLocation.
211 * @return CtfLocation
212 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
213 */
214 @Override
215 public CtfLocation getLocation() {
216 return curLocation;
217 }
218
219 /**
220 * Method increaseRank.
221 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
222 */
223 @Override
224 public void increaseRank() {
225 /* Only increase the rank if it's valid */
226 if(hasValidRank()) {
227 curRank++;
228 }
229 }
230
231 /**
232 * Method hasValidRank, if the iterator is valid
233 * @return boolean
234 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
235 */
236 @Override
237 public boolean hasValidRank() {
238 return (getRank() >= 0);
239 }
240
241 /**
242 * Method advance go to the next event
243 * @return boolean successful or not
244 */
245 @Override
246 public boolean advance() {
247 long index = curLocation.getLocation().getIndex();
248 long timestamp = curLocation.getLocation().getTimestamp();
249 boolean ret = super.advance();
250
251 if (ret) {
252 final long timestampValue = getCurrentEvent().getTimestampValue();
253 if (timestamp == timestampValue) {
254 curLocation.setLocation(timestampValue, index + 1);
255 } else {
256 curLocation.setLocation(timestampValue, 0L);
257 }
258 } else {
259 curLocation = NULL_LOCATION;
260 }
261 return ret;
262 }
263
264 /**
265 * Method compareTo.
266 * @param o CtfIterator
267 * @return int -1, 0, 1
268 */
269 @Override
270 public int compareTo(final CtfIterator o) {
271 if (this.getRank() < o.getRank()) {
272 return -1;
273 } else if (this.getRank() > o.getRank()) {
274 return 1;
275 }
276 return 0;
277 }
278
279 /* (non-Javadoc)
280 * @see java.lang.Object#hashCode()
281 */
282 @Override
283 public int hashCode() {
284 final int prime = 31;
285 int result = super.hashCode();
286 result = (prime * result)
287 + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode());
288 result = (prime * result)
289 + ((curLocation == null) ? 0 : curLocation.hashCode());
290 result = (prime * result) + (int) (curRank ^ (curRank >>> 32));
291 return result;
292 }
293
294 /* (non-Javadoc)
295 * @see java.lang.Object#equals(java.lang.Object)
296 */
297 @Override
298 public boolean equals(Object obj) {
299 if (this == obj) {
300 return true;
301 }
302 if (!super.equals(obj)) {
303 return false;
304 }
305 if (!(obj instanceof CtfIterator)) {
306 return false;
307 }
308 CtfIterator other = (CtfIterator) obj;
309 if (ctfTmfTrace == null) {
310 if (other.ctfTmfTrace != null) {
311 return false;
312 }
313 } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) {
314 return false;
315 }
316 if (curLocation == null) {
317 if (other.curLocation != null) {
318 return false;
319 }
320 } else if (!curLocation.equals(other.curLocation)) {
321 return false;
322 }
323 if (curRank != other.curRank) {
324 return false;
325 }
326 return true;
327 }
328 }
This page took 0.038787 seconds and 6 git commands to generate.