fix seek with offsetted timestamps.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfIterator.java
1 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
2
3 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
4 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
5 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
6 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
7
8 public class CtfIterator extends CTFTraceReader implements ITmfContext,
9 Comparable<CtfIterator> {
10
11 private final CtfTmfTrace ctfTmfTrace;
12
13 private CtfLocation curLocation;
14 private long curRank;
15
16 /**
17 * Create a new CTF trace iterator, which initially points at the first
18 * event in the trace.
19 *
20 * @param trace
21 */
22 public CtfIterator(CtfTmfTrace trace) {
23 super(trace.getCTFTrace());
24 this.ctfTmfTrace = trace;
25
26 // FIXME put the real stuff here...
27 this.curLocation = new CtfLocation(trace.getStartTime());
28 this.curRank = 0;
29 }
30
31 public CtfIterator(CtfTmfTrace trace, long timestampValue, long rank) {
32 super(trace.getCTFTrace());
33 this.ctfTmfTrace = trace;
34 this.curLocation = (new CtfLocation(
35 this.getCurrentEvent().getTimestampValue()));
36 if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
37 this.seek(timestampValue);
38 }
39
40 this.curRank = rank;
41 }
42
43 public CtfTmfTrace getCtfTmfTrace() {
44 return ctfTmfTrace;
45 }
46
47 public CtfTmfEvent getCurrentEvent() {
48 StreamInputReader top = super.prio.peek();
49 if (top != null) {
50 return new CtfTmfEvent(top.getCurrentEvent(), top, ctfTmfTrace);
51 }
52 return null;
53 }
54
55 @Override
56 public boolean seek(long timestamp) {
57 boolean ret = false;
58 long offsetTimestamp = timestamp - this.getCtfTmfTrace().getCTFTrace().getOffset();
59 if( offsetTimestamp < 0 ) {
60 ret = super.seek(timestamp);
61 } else {
62 ret = super.seek(offsetTimestamp);
63 }
64
65 if (ret) {
66 curLocation.setLocation(getCurrentEvent().getTimestampValue());
67 }
68 return ret;
69 }
70
71 @Override
72 public long getRank() {
73 final CtfTmfEvent current = getCurrentEvent();
74 if (current != null) {
75 return getCurrentEvent().getRank();
76 }
77 return 0;
78 }
79
80 @Override
81 public void setRank(long rank) {
82 // FIXME NYI
83 }
84
85 /*
86 * (non-Javadoc)
87 *
88 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
89 */
90 @Override
91 public CtfIterator clone() {
92 CtfIterator clone = null;
93 clone = new CtfIterator(ctfTmfTrace,
94 this.getCurrentEvent().getTimestampValue(), curRank);
95 return clone;
96 }
97
98 @Override
99 public void dispose() {
100 // FIXME add dispose() stuff to CTFTrace and call it here...
101
102 }
103
104 @Override
105 public void setLocation(ITmfLocation<?> location) {
106 // FIXME alex: isn't there a cleaner way than a cast here?
107 this.curLocation = (CtfLocation) location;
108 seek(((CtfLocation)location).getLocation());
109 }
110
111 @Override
112 public CtfLocation getLocation() {
113 return curLocation;
114 }
115
116 @Override
117 public void updateRank(int rank) {
118 curRank = rank;
119 }
120
121 @Override
122 public boolean isValidRank() {
123 return true;
124 }
125
126 @Override
127 public boolean advance() {
128 return super.advance();
129 }
130
131 @Override
132 public int compareTo(CtfIterator o) {
133 if (this.getRank() < o.getRank()) {
134 return -1;
135 } else if (this.getRank() > o.getRank()) {
136 return 1;
137 }
138 return 0;
139 }
140
141 }
This page took 0.03485 seconds and 6 git commands to generate.