1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
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
;
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
23 public class CtfIterator
extends CTFTraceReader
implements ITmfContext
,
24 Comparable
<CtfIterator
> {
26 private final CtfTmfTrace ctfTmfTrace
;
28 final public static CtfLocation NULL_LOCATION
= new CtfLocation(
29 CtfLocation
.INVALID_LOCATION
);
30 private CtfLocation curLocation
;
34 * Create a new CTF trace iterator, which initially points at the first
37 * @param trace the trace to iterate over
39 public CtfIterator(final CtfTmfTrace trace
) {
40 super(trace
.getCTFTrace());
41 this.ctfTmfTrace
= trace
;
42 if (this.hasMoreEvents()) {
44 this.curLocation
= new CtfLocation(trace
.getStartTime());
54 private void setUnknownLocation() {
55 this.curLocation
= NULL_LOCATION
;
56 this.curRank
= UNKNOWN_RANK
;
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
65 public CtfIterator(final CtfTmfTrace trace
, final long timestampValue
,
67 super(trace
.getCTFTrace());
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
);
84 * Method getCtfTmfTrace. gets a CtfTmfTrace
87 public CtfTmfTrace
getCtfTmfTrace() {
92 * Method getCurrentEvent. gets the current event
95 public CtfTmfEvent
getCurrentEvent() {
96 final StreamInputReader top
= super.prio
.peek();
98 return new CtfTmfEvent(top
.getCurrentEvent(), top
.getFilename(),
105 * Method seek. Seeks to a given timestamp
106 * @param timestamp long the timestamp in ns (utc)
110 public boolean seek(final long timestamp
) {
112 final long offsetTimestamp
= timestamp
113 - this.getCtfTmfTrace().getCTFTrace().getOffset();
114 if (offsetTimestamp
< 0) {
115 ret
= super.seek(timestamp
);
117 ret
= super.seek(offsetTimestamp
);
121 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
123 curLocation
= NULL_LOCATION
;
129 * Method seekRank. seeks to a given rank
130 * @param rank long the rank to seek to
133 public boolean seekRank(final long rank
) {
135 ret
= super.seekIndex(rank
);
138 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
140 curLocation
= NULL_LOCATION
;
148 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
151 public long getRank() {
158 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
161 public void setRank(final long rank
) {
168 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
171 public CtfIterator
clone() {
172 CtfIterator clone
= null;
173 clone
= new CtfIterator(ctfTmfTrace
, this.getCurrentEvent()
174 .getTimestampValue(), curRank
);
180 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
183 public void dispose() {
184 // FIXME add dispose() stuff to CTFTrace and call it here...
189 * Method setLocation.
190 * @param location ITmfLocation<?>
191 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(ITmfLocation<?>)
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());
201 * Method getLocation.
202 * @return CtfLocation
203 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
206 public CtfLocation
getLocation() {
211 * Method increaseRank.
212 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
215 public void increaseRank() {
220 * Method hasValidRank, if the iterator is valid
222 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
225 public boolean hasValidRank() {
226 return (getRank() >= 0);
230 * Method advance go to the next event
231 * @return boolean successful or not
234 public boolean advance() {
235 boolean ret
= super.advance();
237 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
239 curLocation
= NULL_LOCATION
;
246 * @param o CtfIterator
247 * @return int -1, 0, 1
250 public int compareTo(final CtfIterator o
) {
251 if (this.getRank() < o
.getRank()) {
253 } else if (this.getRank() > o
.getRank()) {
259 * @see java.lang.Object#hashCode()
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));
274 * @see java.lang.Object#equals(java.lang.Object)
277 public boolean equals(Object obj
) {
281 if (!super.equals(obj
)) {
284 if (!(obj
instanceof CtfIterator
)) {
287 CtfIterator other
= (CtfIterator
) obj
;
288 if (ctfTmfTrace
== null) {
289 if (other
.ctfTmfTrace
!= null) {
292 } else if (!ctfTmfTrace
.equals(other
.ctfTmfTrace
)) {
295 if (curLocation
== null) {
296 if (other
.curLocation
!= null) {
299 } else if (!curLocation
.equals(other
.curLocation
)) {
302 if (curRank
!= other
.curRank
) {