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