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