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