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