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