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
;
30 final public static CtfLocation NULL_LOCATION
= new CtfLocation(
31 CtfLocation
.INVALID_LOCATION
);
32 private CtfLocation curLocation
;
36 * Create a new CTF trace iterator, which initially points at the first
39 * @param trace the trace to iterate over
41 public CtfIterator(final CtfTmfTrace trace
) {
42 super(trace
.getCTFTrace());
43 this.ctfTmfTrace
= trace
;
44 if (this.hasMoreEvents()) {
46 this.curLocation
= new CtfLocation(trace
.getStartTime());
56 private void setUnknownLocation() {
57 this.curLocation
= NULL_LOCATION
;
58 this.curRank
= UNKNOWN_RANK
;
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
67 public CtfIterator(final CtfTmfTrace trace
, final long timestampValue
,
69 super(trace
.getCTFTrace());
71 this.ctfTmfTrace
= trace
;
72 if (this.hasMoreEvents()) {
73 this.curLocation
= (new CtfLocation(this.getCurrentEvent()
74 .getTimestampValue()));
75 if (this.getCurrentEvent().getTimestampValue() != timestampValue
) {
76 this.seek(timestampValue
);
86 * Method getCtfTmfTrace. gets a CtfTmfTrace
89 public CtfTmfTrace
getCtfTmfTrace() {
94 * Method getCurrentEvent. gets the current event
97 public CtfTmfEvent
getCurrentEvent() {
98 final StreamInputReader top
= super.prio
.peek();
100 return new CtfTmfEvent(top
.getCurrentEvent(), top
.getFilename(),
107 * Method seek. Seeks to a given timestamp
108 * @param timestamp long the timestamp in ns (utc)
112 public boolean seek(final long timestamp
) {
114 final long offsetTimestamp
= timestamp
115 - this.getTrace().getOffset();
116 if (offsetTimestamp
< 0) {
117 ret
= super.seek(timestamp
);
119 ret
= super.seek(offsetTimestamp
);
123 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
125 curLocation
= NULL_LOCATION
;
133 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
136 public long getRank() {
143 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
146 public void setRank(final long rank
) {
153 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
156 public CtfIterator
clone() {
157 CtfIterator clone
= null;
158 clone
= new CtfIterator(ctfTmfTrace
, this.getCurrentEvent().getTimestampValue(), curRank
);
164 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
167 public void dispose() {
168 // FIXME add dispose() stuff to CTFTrace and call it here...
173 * Method setLocation.
174 * @param location ITmfLocation<?>
177 public void setLocation(final ITmfLocation
<?
> location
) {
178 // FIXME alex: isn't there a cleaner way than a cast here?
179 this.curLocation
= (CtfLocation
) location
;
180 seek(((CtfLocation
) location
).getLocation());
184 * Method getLocation.
185 * @return CtfLocation
186 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
189 public CtfLocation
getLocation() {
194 * Method increaseRank.
195 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
198 public void increaseRank() {
199 /* Only increase the rank if it's valid */
206 * Method hasValidRank, if the iterator is valid
208 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
211 public boolean hasValidRank() {
212 return (getRank() >= 0);
216 * Method advance go to the next event
217 * @return boolean successful or not
220 public boolean advance() {
221 boolean ret
= super.advance();
223 curLocation
.setLocation(getCurrentEvent().getTimestampValue());
225 curLocation
= NULL_LOCATION
;
232 * @param o CtfIterator
233 * @return int -1, 0, 1
236 public int compareTo(final CtfIterator o
) {
237 if (this.getRank() < o
.getRank()) {
239 } else if (this.getRank() > o
.getRank()) {
246 * @see java.lang.Object#hashCode()
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));
261 * @see java.lang.Object#equals(java.lang.Object)
264 public boolean equals(Object obj
) {
268 if (!super.equals(obj
)) {
271 if (!(obj
instanceof CtfIterator
)) {
274 CtfIterator other
= (CtfIterator
) obj
;
275 if (ctfTmfTrace
== null) {
276 if (other
.ctfTmfTrace
!= null) {
279 } else if (!ctfTmfTrace
.equals(other
.ctfTmfTrace
)) {
282 if (curLocation
== null) {
283 if (other
.curLocation
!= null) {
286 } else if (!curLocation
.equals(other
.curLocation
)) {
289 if (curRank
!= other
.curRank
) {