Fixed lost event bug.
[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 ctfIterator is the class that will act like a reader for the trace
20 * it does not have a file handle, so many iterators can be used without worries
21 * of io errors.
22 */
23 public class CtfIterator extends CTFTraceReader implements ITmfContext,
24 Comparable<CtfIterator> {
25
26 private final CtfTmfTrace ctfTmfTrace;
27
28 final public static CtfLocation NULL_LOCATION = new CtfLocation(
29 CtfLocation.INVALID_LOCATION);
30 private CtfLocation curLocation;
31 private long curRank;
32
33 /**
34 * Create a new CTF trace iterator, which initially points at the first
35 * event in the trace.
36 *
37 * @param trace the trace to iterate over
38 */
39 public CtfIterator(final CtfTmfTrace trace) {
40 super(trace.getCTFTrace());
41 this.ctfTmfTrace = trace;
42 if (this.hasMoreEvents()) {
43
44 this.curLocation = new CtfLocation(trace.getStartTime());
45 this.curRank = 0;
46 } else {
47 setUnknownLocation();
48 }
49 }
50
51 /**
52 *
53 */
54 private void setUnknownLocation() {
55 this.curLocation = NULL_LOCATION;
56 this.curRank = UNKNOWN_RANK;
57 }
58
59 /**
60 * Constructor for CtfIterator.
61 * @param trace CtfTmfTrace the trace
62 * @param timestampValue long the timestamp in ns of the trace for positioning
63 * @param rank long the index of the trace for positioning
64 */
65 public CtfIterator(final CtfTmfTrace trace, final long timestampValue,
66 final long rank) {
67 super(trace.getCTFTrace());
68
69 this.ctfTmfTrace = trace;
70 if (this.hasMoreEvents()) {
71 this.curLocation = (new CtfLocation(this.getCurrentEvent()
72 .getTimestampValue()));
73 if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
74 this.seek(timestampValue);
75 this.curRank = rank;
76 }
77 } else {
78 setUnknownLocation();
79 }
80
81 }
82
83 /**
84 * Method getCtfTmfTrace. gets a CtfTmfTrace
85 * @return CtfTmfTrace
86 */
87 public CtfTmfTrace getCtfTmfTrace() {
88 return ctfTmfTrace;
89 }
90
91 /**
92 * Method getCurrentEvent. gets the current event
93 * @return CtfTmfEvent
94 */
95 public CtfTmfEvent getCurrentEvent() {
96 final StreamInputReader top = super.prio.peek();
97 if (top != null) {
98 return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(),
99 ctfTmfTrace);
100 }
101 return null;
102 }
103
104 /**
105 * Method seek. Seeks to a given timestamp
106 * @param timestamp long the timestamp in ns (utc)
107 * @return boolean
108 */
109 @Override
110 public boolean seek(final long timestamp) {
111 boolean ret = false;
112 final long offsetTimestamp = timestamp
113 - this.getCtfTmfTrace().getCTFTrace().getOffset();
114 if (offsetTimestamp < 0) {
115 ret = super.seek(timestamp);
116 } else {
117 ret = super.seek(offsetTimestamp);
118 }
119
120 if (ret) {
121 curLocation.setLocation(getCurrentEvent().getTimestampValue());
122 } else {
123 curLocation = NULL_LOCATION;
124 }
125 return ret;
126 }
127
128 /**
129 * Method seekRank. seeks to a given rank
130 * @param rank long the rank to seek to
131 * @return boolean
132 */
133 public boolean seekRank(final long rank) {
134 boolean ret = false;
135 ret = super.seekIndex(rank);
136
137 if (ret) {
138 curLocation.setLocation(getCurrentEvent().getTimestampValue());
139 } else {
140 curLocation = NULL_LOCATION;
141 }
142 return ret;
143 }
144
145 /**
146 * Method getRank.
147 * @return long
148 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
149 */
150 @Override
151 public long getRank() {
152 return curRank;
153 }
154
155 /**
156 * Method setRank.
157 * @param rank long
158 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
159 */
160 @Override
161 public void setRank(final long rank) {
162 curRank = rank;
163 }
164
165 /*
166 * (non-Javadoc)
167 *
168 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
169 */
170 @Override
171 public CtfIterator clone() {
172 CtfIterator clone = null;
173 clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent()
174 .getTimestampValue(), curRank);
175 return clone;
176 }
177
178 /**
179 * Method dispose.
180 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
181 */
182 @Override
183 public void dispose() {
184 // FIXME add dispose() stuff to CTFTrace and call it here...
185
186 }
187
188 /**
189 * Method setLocation.
190 * @param location ITmfLocation<?>
191 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(ITmfLocation<?>)
192 */
193 @Override
194 public void setLocation(final ITmfLocation<?> location) {
195 // FIXME alex: isn't there a cleaner way than a cast here?
196 this.curLocation = (CtfLocation) location;
197 seek(((CtfLocation) location).getLocation());
198 }
199
200 /**
201 * Method getLocation.
202 * @return CtfLocation
203 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
204 */
205 @Override
206 public CtfLocation getLocation() {
207 return curLocation;
208 }
209
210 /**
211 * Method increaseRank.
212 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
213 */
214 @Override
215 public void increaseRank() {
216 curRank++;
217 }
218
219 /**
220 * Method hasValidRank, if the iterator is valid
221 * @return boolean
222 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
223 */
224 @Override
225 public boolean hasValidRank() {
226 return (getRank() >= 0);
227 }
228
229 /**
230 * Method advance go to the next event
231 * @return boolean successful or not
232 */
233 @Override
234 public boolean advance() {
235 boolean ret = super.advance();
236 if (ret) {
237 curLocation.setLocation(getCurrentEvent().getTimestampValue());
238 } else {
239 curLocation = NULL_LOCATION;
240 }
241 return ret;
242 }
243
244 /**
245 * Method compareTo.
246 * @param o CtfIterator
247 * @return int -1, 0, 1
248 */
249 @Override
250 public int compareTo(final CtfIterator o) {
251 if (this.getRank() < o.getRank()) {
252 return -1;
253 } else if (this.getRank() > o.getRank()) {
254 return 1;
255 }
256 return 0;
257 }
258 /* (non-Javadoc)
259 * @see java.lang.Object#hashCode()
260 */
261 @Override
262 public int hashCode() {
263 final int prime = 31;
264 int result = super.hashCode();
265 result = (prime * result)
266 + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode());
267 result = (prime * result)
268 + ((curLocation == null) ? 0 : curLocation.hashCode());
269 result = (prime * result) + (int) (curRank ^ (curRank >>> 32));
270 return result;
271 }
272
273 /* (non-Javadoc)
274 * @see java.lang.Object#equals(java.lang.Object)
275 */
276 @Override
277 public boolean equals(Object obj) {
278 if (this == obj) {
279 return true;
280 }
281 if (!super.equals(obj)) {
282 return false;
283 }
284 if (!(obj instanceof CtfIterator)) {
285 return false;
286 }
287 CtfIterator other = (CtfIterator) obj;
288 if (ctfTmfTrace == null) {
289 if (other.ctfTmfTrace != null) {
290 return false;
291 }
292 } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) {
293 return false;
294 }
295 if (curLocation == null) {
296 if (other.curLocation != null) {
297 return false;
298 }
299 } else if (!curLocation.equals(other.curLocation)) {
300 return false;
301 }
302 if (curRank != other.curRank) {
303 return false;
304 }
305 return true;
306 }
307 }
This page took 0.036397 seconds and 5 git commands to generate.