Improve test coverage.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfIterator.java
CommitLineData
b1baa808
MK
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
a3fc8213
AM
11package org.eclipse.linuxtools.tmf.core.ctfadaptor;
12
13import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
14import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
15import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
16import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
17
b1baa808
MK
18/**
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
21 * of io errors.
22 */
a3fc8213 23public class CtfIterator extends CTFTraceReader implements ITmfContext,
57c073c5 24 Comparable<CtfIterator> {
a3fc8213
AM
25
26 private final CtfTmfTrace ctfTmfTrace;
27
57c073c5
MK
28 final public static CtfLocation nullLocation = new CtfLocation(
29 CtfLocation.INVALID_LOCATION);
a3fc8213
AM
30 private CtfLocation curLocation;
31 private long curRank;
32
33 /**
34 * Create a new CTF trace iterator, which initially points at the first
35 * event in the trace.
36 *
b1baa808 37 * @param trace the trace to iterate over
a3fc8213 38 */
ce2388e0 39 public CtfIterator(final CtfTmfTrace trace) {
a3fc8213
AM
40 super(trace.getCTFTrace());
41 this.ctfTmfTrace = trace;
57c073c5 42 if (this.hasMoreEvents()) {
a3fc8213 43
57c073c5
MK
44 this.curLocation = new CtfLocation(trace.getStartTime());
45 this.curRank = 0;
46 } else {
47 setUnknownLocation();
48 }
a3fc8213
AM
49 }
50
57c073c5
MK
51 /**
52 *
53 */
54 private void setUnknownLocation() {
55 this.curLocation = nullLocation;
56 this.curRank = UNKNOWN_RANK;
57 }
58
b1baa808
MK
59 /**
60 * Constructor for CtfIterator.
61 * @param trace CtfTmfTrace the trace
62 * @param timestampValue long the timestamp in ns of the trace for positioning
63 * @param rank long the index of the trace for positioning
64 */
57c073c5
MK
65 public CtfIterator(final CtfTmfTrace trace, final long timestampValue,
66 final long rank) {
a3fc8213 67 super(trace.getCTFTrace());
57c073c5 68
a3fc8213 69 this.ctfTmfTrace = trace;
57c073c5
MK
70 if (this.hasMoreEvents()) {
71 this.curLocation = (new CtfLocation(this.getCurrentEvent()
72 .getTimestampValue()));
73 if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
74 this.seek(timestampValue);
75 this.curRank = rank;
76 }
77 } else {
78 setUnknownLocation();
79 }
a3fc8213 80
a3fc8213
AM
81 }
82
b1baa808
MK
83 /**
84 * Method getCtfTmfTrace. gets a CtfTmfTrace
85 * @return CtfTmfTrace
86 */
a3fc8213
AM
87 public CtfTmfTrace getCtfTmfTrace() {
88 return ctfTmfTrace;
89 }
90
b1baa808
MK
91 /**
92 * Method getCurrentEvent. gets the current event
93 * @return CtfTmfEvent
94 */
a3fc8213 95 public CtfTmfEvent getCurrentEvent() {
ce2388e0 96 final StreamInputReader top = super.prio.peek();
57c073c5
MK
97 if (top != null) {
98 return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(),
99 ctfTmfTrace);
100 }
a3fc8213
AM
101 return null;
102 }
103
b1baa808
MK
104 /**
105 * Method seek. Seeks to a given timestamp
106 * @param timestamp long the timestamp in ns (utc)
107 * @return boolean
108 */
a3fc8213 109 @Override
ce2388e0 110 public boolean seek(final long timestamp) {
a3fc8213 111 boolean ret = false;
57c073c5
MK
112 final long offsetTimestamp = timestamp
113 - this.getCtfTmfTrace().getCTFTrace().getOffset();
114 if (offsetTimestamp < 0) {
ce2388e0 115 ret = super.seek(timestamp);
57c073c5 116 } else {
ce2388e0 117 ret = super.seek(offsetTimestamp);
57c073c5 118 }
a3fc8213 119
57c073c5 120 if (ret) {
ce2388e0 121 curLocation.setLocation(getCurrentEvent().getTimestampValue());
57c073c5 122 }
ce2388e0
FC
123 return ret;
124 }
125
b1baa808
MK
126 /**
127 * Method seekRank. seeks to a given rank
128 * @param rank long the rank to seek to
129 * @return boolean
130 */
ce2388e0
FC
131 public boolean seekRank(final long rank) {
132 boolean ret = false;
133 ret = super.seekIndex(rank);
134
57c073c5 135 if (ret) {
a3fc8213 136 curLocation.setLocation(getCurrentEvent().getTimestampValue());
57c073c5 137 }
a3fc8213
AM
138 return ret;
139 }
140
b1baa808
MK
141 /**
142 * Method getRank.
143 * @return long
144 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
145 */
a3fc8213
AM
146 @Override
147 public long getRank() {
ce2388e0 148 return super.getIndex();
a3fc8213
AM
149 }
150
b1baa808
MK
151 /**
152 * Method setRank.
153 * @param rank long
154 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
155 */
a3fc8213 156 @Override
ce2388e0 157 public void setRank(final long rank) {
57c073c5
MK
158 if(!this.curLocation.equals(nullLocation)) {
159 seekRank(rank);
160 }
a3fc8213
AM
161 }
162
163 /*
164 * (non-Javadoc)
165 *
166 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
167 */
168 @Override
169 public CtfIterator clone() {
170 CtfIterator clone = null;
ce2388e0
FC
171 clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent()
172 .getTimestampValue(), curRank);
a3fc8213
AM
173 return clone;
174 }
175
b1baa808
MK
176 /**
177 * Method dispose.
178 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
179 */
a3fc8213
AM
180 @Override
181 public void dispose() {
182 // FIXME add dispose() stuff to CTFTrace and call it here...
183
184 }
185
b1baa808
MK
186 /**
187 * Method setLocation.
188 * @param location ITmfLocation<?>
189 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(ITmfLocation<?>)
190 */
a3fc8213 191 @Override
ce2388e0 192 public void setLocation(final ITmfLocation<?> location) {
a3fc8213
AM
193 // FIXME alex: isn't there a cleaner way than a cast here?
194 this.curLocation = (CtfLocation) location;
57c073c5 195 seek(((CtfLocation) location).getLocation());
a3fc8213
AM
196 }
197
b1baa808
MK
198 /**
199 * Method getLocation.
200 * @return CtfLocation
201 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
202 */
a3fc8213
AM
203 @Override
204 public CtfLocation getLocation() {
205 return curLocation;
206 }
207
b1baa808
MK
208 /**
209 * Method increaseRank.
210 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
211 */
a3fc8213 212 @Override
cbdacf03
FC
213 public void increaseRank() {
214 curRank++;
a3fc8213
AM
215 }
216
b1baa808
MK
217 /**
218 * Method hasValidRank, if the iterator is valid
219 * @return boolean
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
221 */
a3fc8213 222 @Override
cbdacf03 223 public boolean hasValidRank() {
bcbea6a6 224 return (getRank() >= 0);
a3fc8213
AM
225 }
226
b1baa808
MK
227 /**
228 * Method advance go to the next event
229 * @return boolean successful or not
230 */
a3fc8213
AM
231 @Override
232 public boolean advance() {
233 return super.advance();
234 }
235
b1baa808
MK
236 /**
237 * Method compareTo.
238 * @param o CtfIterator
239 * @return int -1, 0, 1
240 */
a3fc8213 241 @Override
ce2388e0 242 public int compareTo(final CtfIterator o) {
57c073c5 243 if (this.getRank() < o.getRank()) {
a3fc8213 244 return -1;
57c073c5 245 } else if (this.getRank() > o.getRank()) {
a3fc8213 246 return 1;
57c073c5 247 }
a3fc8213
AM
248 return 0;
249 }
b1baa808
MK
250 /* (non-Javadoc)
251 * @see java.lang.Object#hashCode()
252 */
253 @Override
254 public int hashCode() {
255 final int prime = 31;
256 int result = super.hashCode();
257 result = (prime * result)
258 + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode());
259 result = (prime * result)
260 + ((curLocation == null) ? 0 : curLocation.hashCode());
261 result = (prime * result) + (int) (curRank ^ (curRank >>> 32));
262 return result;
263 }
a3fc8213 264
b1baa808
MK
265 /* (non-Javadoc)
266 * @see java.lang.Object#equals(java.lang.Object)
267 */
268 @Override
269 public boolean equals(Object obj) {
270 if (this == obj) {
271 return true;
272 }
273 if (!super.equals(obj)) {
274 return false;
275 }
276 if (!(obj instanceof CtfIterator)) {
277 return false;
278 }
279 CtfIterator other = (CtfIterator) obj;
280 if (ctfTmfTrace == null) {
281 if (other.ctfTmfTrace != null) {
282 return false;
283 }
284 } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) {
285 return false;
286 }
287 if (curLocation == null) {
288 if (other.curLocation != null) {
289 return false;
290 }
291 } else if (!curLocation.equals(other.curLocation)) {
292 return false;
293 }
294 if (curRank != other.curRank) {
295 return false;
296 }
297 return true;
298 }
ce2388e0 299}
This page took 0.043141 seconds and 5 git commands to generate.