fix empty trace bug
[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 final public static CtfLocation nullLocation = new CtfLocation(
14 CtfLocation.INVALID_LOCATION);
15 private CtfLocation curLocation;
16 private long curRank;
17
18 /**
19 * Create a new CTF trace iterator, which initially points at the first
20 * event in the trace.
21 *
22 * @param trace
23 */
24 public CtfIterator(final CtfTmfTrace trace) {
25 super(trace.getCTFTrace());
26 this.ctfTmfTrace = trace;
27 if (this.hasMoreEvents()) {
28
29 this.curLocation = new CtfLocation(trace.getStartTime());
30 this.curRank = 0;
31 } else {
32 setUnknownLocation();
33 }
34 }
35
36 /**
37 *
38 */
39 private void setUnknownLocation() {
40 this.curLocation = nullLocation;
41 this.curRank = UNKNOWN_RANK;
42 }
43
44 public CtfIterator(final CtfTmfTrace trace, final long timestampValue,
45 final long rank) {
46 super(trace.getCTFTrace());
47
48 this.ctfTmfTrace = trace;
49 if (this.hasMoreEvents()) {
50 this.curLocation = (new CtfLocation(this.getCurrentEvent()
51 .getTimestampValue()));
52 if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
53 this.seek(timestampValue);
54 this.curRank = rank;
55 }
56 } else {
57 setUnknownLocation();
58 }
59
60 }
61
62 public CtfTmfTrace getCtfTmfTrace() {
63 return ctfTmfTrace;
64 }
65
66 public CtfTmfEvent getCurrentEvent() {
67 final StreamInputReader top = super.prio.peek();
68 if (top != null) {
69 return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(),
70 ctfTmfTrace);
71 }
72 return null;
73 }
74
75 @Override
76 public boolean seek(final long timestamp) {
77 boolean ret = false;
78 final long offsetTimestamp = timestamp
79 - this.getCtfTmfTrace().getCTFTrace().getOffset();
80 if (offsetTimestamp < 0) {
81 ret = super.seek(timestamp);
82 } else {
83 ret = super.seek(offsetTimestamp);
84 }
85
86 if (ret) {
87 curLocation.setLocation(getCurrentEvent().getTimestampValue());
88 }
89 return ret;
90 }
91
92 public boolean seekRank(final long rank) {
93 boolean ret = false;
94 ret = super.seekIndex(rank);
95
96 if (ret) {
97 curLocation.setLocation(getCurrentEvent().getTimestampValue());
98 }
99 return ret;
100 }
101
102 @Override
103 public long getRank() {
104 return super.getIndex();
105 }
106
107 @Override
108 public void setRank(final long rank) {
109 if(!this.curLocation.equals(nullLocation)) {
110 seekRank(rank);
111 }
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
118 */
119 @Override
120 public CtfIterator clone() {
121 CtfIterator clone = null;
122 clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent()
123 .getTimestampValue(), curRank);
124 return clone;
125 }
126
127 @Override
128 public void dispose() {
129 // FIXME add dispose() stuff to CTFTrace and call it here...
130
131 }
132
133 @Override
134 public void setLocation(final ITmfLocation<?> location) {
135 // FIXME alex: isn't there a cleaner way than a cast here?
136 this.curLocation = (CtfLocation) location;
137 seek(((CtfLocation) location).getLocation());
138 }
139
140 @Override
141 public CtfLocation getLocation() {
142 return curLocation;
143 }
144
145 @Override
146 public void increaseRank() {
147 curRank++;
148 }
149
150 @Override
151 public boolean hasValidRank() {
152 return (getRank() >= 0);
153 }
154
155 @Override
156 public boolean advance() {
157 return super.advance();
158 }
159
160 @Override
161 public int compareTo(final CtfIterator o) {
162 if (this.getRank() < o.getRank()) {
163 return -1;
164 } else if (this.getRank() > o.getRank()) {
165 return 1;
166 }
167 return 0;
168 }
169
170 }
This page took 0.050063 seconds and 6 git commands to generate.