Commit | Line | Data |
---|---|---|
a3fc8213 AM |
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, | |
ce2388e0 | 9 | Comparable<CtfIterator> { |
a3fc8213 AM |
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 | */ | |
ce2388e0 | 22 | public CtfIterator(final CtfTmfTrace trace) { |
a3fc8213 AM |
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 | ||
ce2388e0 | 31 | public CtfIterator(final CtfTmfTrace trace, final long timestampValue, final long rank) { |
a3fc8213 AM |
32 | super(trace.getCTFTrace()); |
33 | this.ctfTmfTrace = trace; | |
ce2388e0 FC |
34 | this.curLocation = (new CtfLocation(this.getCurrentEvent() |
35 | .getTimestampValue())); | |
36 | if (this.getCurrentEvent().getTimestampValue() != timestampValue) | |
a3fc8213 | 37 | this.seek(timestampValue); |
a3fc8213 AM |
38 | |
39 | this.curRank = rank; | |
40 | } | |
41 | ||
42 | public CtfTmfTrace getCtfTmfTrace() { | |
43 | return ctfTmfTrace; | |
44 | } | |
45 | ||
46 | public CtfTmfEvent getCurrentEvent() { | |
ce2388e0 FC |
47 | final StreamInputReader top = super.prio.peek(); |
48 | if (top != null) | |
49 | return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(), ctfTmfTrace); | |
a3fc8213 AM |
50 | return null; |
51 | } | |
52 | ||
53 | @Override | |
ce2388e0 | 54 | public boolean seek(final long timestamp) { |
a3fc8213 | 55 | boolean ret = false; |
ce2388e0 FC |
56 | final long offsetTimestamp = timestamp - this.getCtfTmfTrace().getCTFTrace().getOffset(); |
57 | if( offsetTimestamp < 0 ) | |
58 | ret = super.seek(timestamp); | |
59 | else | |
60 | ret = super.seek(offsetTimestamp); | |
a3fc8213 | 61 | |
ce2388e0 FC |
62 | if (ret) |
63 | curLocation.setLocation(getCurrentEvent().getTimestampValue()); | |
64 | return ret; | |
65 | } | |
66 | ||
67 | public boolean seekRank(final long rank) { | |
68 | boolean ret = false; | |
69 | ret = super.seekIndex(rank); | |
70 | ||
71 | if (ret) | |
a3fc8213 | 72 | curLocation.setLocation(getCurrentEvent().getTimestampValue()); |
a3fc8213 AM |
73 | return ret; |
74 | } | |
75 | ||
76 | @Override | |
77 | public long getRank() { | |
ce2388e0 | 78 | return super.getIndex(); |
a3fc8213 AM |
79 | } |
80 | ||
81 | @Override | |
ce2388e0 FC |
82 | public void setRank(final long rank) { |
83 | seekRank(rank); | |
a3fc8213 AM |
84 | } |
85 | ||
86 | /* | |
87 | * (non-Javadoc) | |
88 | * | |
89 | * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone() | |
90 | */ | |
91 | @Override | |
92 | public CtfIterator clone() { | |
93 | CtfIterator clone = null; | |
ce2388e0 FC |
94 | clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent() |
95 | .getTimestampValue(), curRank); | |
a3fc8213 AM |
96 | return clone; |
97 | } | |
98 | ||
99 | @Override | |
100 | public void dispose() { | |
101 | // FIXME add dispose() stuff to CTFTrace and call it here... | |
102 | ||
103 | } | |
104 | ||
105 | @Override | |
ce2388e0 | 106 | public void setLocation(final ITmfLocation<?> location) { |
a3fc8213 AM |
107 | // FIXME alex: isn't there a cleaner way than a cast here? |
108 | this.curLocation = (CtfLocation) location; | |
ce2388e0 | 109 | seek(((CtfLocation)location).getLocation()); |
a3fc8213 AM |
110 | } |
111 | ||
112 | @Override | |
113 | public CtfLocation getLocation() { | |
114 | return curLocation; | |
115 | } | |
116 | ||
ce2388e0 | 117 | @SuppressWarnings("unused") |
a3fc8213 | 118 | @Override |
cbdacf03 FC |
119 | public void increaseRank() { |
120 | curRank++; | |
a3fc8213 AM |
121 | } |
122 | ||
123 | @Override | |
cbdacf03 | 124 | public boolean hasValidRank() { |
ce2388e0 | 125 | return (getRank() > -1); |
a3fc8213 AM |
126 | } |
127 | ||
128 | @Override | |
129 | public boolean advance() { | |
130 | return super.advance(); | |
131 | } | |
132 | ||
133 | @Override | |
ce2388e0 FC |
134 | public int compareTo(final CtfIterator o) { |
135 | if (this.getRank() < o.getRank()) | |
a3fc8213 | 136 | return -1; |
ce2388e0 | 137 | else if (this.getRank() > o.getRank()) |
a3fc8213 | 138 | return 1; |
a3fc8213 AM |
139 | return 0; |
140 | } | |
141 | ||
ce2388e0 | 142 | } |