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
, Comparable
<CtfIterator
>, Cloneable
{
25 private final CtfTmfTrace ctfTmfTrace
;
27 final public static CtfLocation NULL_LOCATION
= new CtfLocation(
28 CtfLocation
.INVALID_LOCATION
);
29 private CtfLocation curLocation
;
33 * Create a new CTF trace iterator, which initially points at the first
36 * @param trace the trace to iterate over
38 public CtfIterator(final CtfTmfTrace trace
) {
39 super(trace
.getCTFTrace());
40 this.ctfTmfTrace
= trace
;
41 if (this.hasMoreEvents()) {
43 this.curLocation
= new CtfLocation(trace
.getStartTime());
53 private void setUnknownLocation() {
54 this.curLocation
= NULL_LOCATION
;
55 this.curRank
= UNKNOWN_RANK
;
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
64 public CtfIterator(final CtfTmfTrace trace
, final long timestampValue
,
66 super(trace
.getCTFTrace());
68 this.ctfTmfTrace
= trace
;
69 if (this.hasMoreEvents()) {
70 this.curLocation
= (new CtfLocation(this.getCurrentEvent()
71 .getTimestampValue()));
72 if (this.getCurrentEvent().getTimestampValue() != timestampValue
) {
73 this.seek(timestampValue
);
83 * Method getCtfTmfTrace. gets a CtfTmfTrace
86 public CtfTmfTrace
getCtfTmfTrace() {
91 * Method getCurrentEvent. gets the current event
94 public CtfTmfEvent
getCurrentEvent() {
95 final StreamInputReader top
= super.prio
.peek();
97 return new CtfTmfEvent(top
.getCurrentEvent(), top
.getFilename(),
104 * Method seek. Seeks to a given timestamp
105 * @param timestamp long the timestamp in ns (utc)
109 public boolean seek(final long timestamp
) {
111 final long offsetTimestamp
= timestamp
112 - this.getCtfTmfTrace().getCTFTrace().getOffset();
113 if (offsetTimestamp
< 0) {
114 ret
= super.seek(timestamp
);
116 ret
= super.seek(offsetTimestamp
);
120 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
122 curLocation
= NULL_LOCATION
;
128 * Method seekRank. seeks to a given rank
129 * @param rank long the rank to seek to
132 public boolean seekRank(final long rank
) {
134 ret
= super.seekIndex(rank
);
137 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
139 curLocation
= NULL_LOCATION
;
147 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
150 public long getRank() {
157 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
160 public void setRank(final long rank
) {
167 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
170 public CtfIterator
clone() {
171 CtfIterator clone
= null;
172 clone
= new CtfIterator(ctfTmfTrace
, this.getCurrentEvent()
173 .getTimestampValue(), curRank
);
179 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
182 public void dispose() {
183 // FIXME add dispose() stuff to CTFTrace and call it here...
188 * Method setLocation.
189 * @param location ITmfLocation<?>
190 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(ITmfLocation<?>)
193 public void setLocation(final ITmfLocation
<?
> location
) {
194 // FIXME alex: isn't there a cleaner way than a cast here?
195 this.curLocation
= (CtfLocation
) location
;
196 seek(((CtfLocation
) location
).getLocation());
200 * Method getLocation.
201 * @return CtfLocation
202 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
205 public CtfLocation
getLocation() {
210 * Method increaseRank.
211 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
214 public void increaseRank() {
219 * Method hasValidRank, if the iterator is valid
221 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
224 public boolean hasValidRank() {
225 return (getRank() >= 0);
229 * Method advance go to the next event
230 * @return boolean successful or not
233 public boolean advance() {
234 boolean ret
= super.advance();
236 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
238 curLocation
= NULL_LOCATION
;
245 * @param o CtfIterator
246 * @return int -1, 0, 1
249 public int compareTo(final CtfIterator o
) {
250 if (this.getRank() < o
.getRank()) {
252 } else if (this.getRank() > o
.getRank()) {
258 * @see java.lang.Object#hashCode()
261 public int hashCode() {
262 final int prime
= 31;
263 int result
= super.hashCode();
264 result
= (prime
* result
)
265 + ((ctfTmfTrace
== null) ?
0 : ctfTmfTrace
.hashCode());
266 result
= (prime
* result
)
267 + ((curLocation
== null) ?
0 : curLocation
.hashCode());
268 result
= (prime
* result
) + (int) (curRank ^
(curRank
>>> 32));
273 * @see java.lang.Object#equals(java.lang.Object)
276 public boolean equals(Object obj
) {
280 if (!super.equals(obj
)) {
283 if (!(obj
instanceof CtfIterator
)) {
286 CtfIterator other
= (CtfIterator
) obj
;
287 if (ctfTmfTrace
== null) {
288 if (other
.ctfTmfTrace
!= null) {
291 } else if (!ctfTmfTrace
.equals(other
.ctfTmfTrace
)) {
294 if (curLocation
== null) {
295 if (other
.curLocation
!= null) {
298 } else if (!curLocation
.equals(other
.curLocation
)) {
301 if (curRank
!= other
.curRank
) {