f688f9d7a7d0aa60252e4f3fedb454cf8c138ac4
[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.getTrace().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 getRank.
129 * @return long
130 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
131 */
132 @Override
133 public long getRank() {
134 return curRank;
135 }
136
137 /**
138 * Method setRank.
139 * @param rank long
140 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
141 */
142 @Override
143 public void setRank(final long rank) {
144 curRank = rank;
145 }
146
147 /*
148 * (non-Javadoc)
149 *
150 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
151 */
152 @Override
153 public CtfIterator clone() {
154 CtfIterator clone = null;
155 clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent()
156 .getTimestampValue(), curRank);
157 return clone;
158 }
159
160 /* (non-Javadoc)
161 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#clone2()
162 */
163 @Override
164 public CtfIterator clone2() {
165 return clone();
166 }
167
168 /**
169 * Method dispose.
170 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
171 */
172 @Override
173 public void dispose() {
174 // FIXME add dispose() stuff to CTFTrace and call it here...
175
176 }
177
178 /**
179 * Method setLocation.
180 * @param location ITmfLocation<?>
181 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(ITmfLocation<?>)
182 */
183 @Override
184 public void setLocation(final ITmfLocation<?> location) {
185 // FIXME alex: isn't there a cleaner way than a cast here?
186 this.curLocation = (CtfLocation) location;
187 seek(((CtfLocation) location).getLocation());
188 }
189
190 /**
191 * Method getLocation.
192 * @return CtfLocation
193 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
194 */
195 @Override
196 public CtfLocation getLocation() {
197 return curLocation;
198 }
199
200 /**
201 * Method increaseRank.
202 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
203 */
204 @Override
205 public void increaseRank() {
206 /* Only increase the rank if it's valid */
207 if(hasValidRank()) {
208 curRank++;
209 }
210 }
211
212 /**
213 * Method hasValidRank, if the iterator is valid
214 * @return boolean
215 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
216 */
217 @Override
218 public boolean hasValidRank() {
219 return (getRank() >= 0);
220 }
221
222 /**
223 * Method advance go to the next event
224 * @return boolean successful or not
225 */
226 @Override
227 public boolean advance() {
228 boolean ret = super.advance();
229 if (ret) {
230 curLocation.setLocation(getCurrentEvent().getTimestampValue());
231 } else {
232 curLocation = NULL_LOCATION;
233 }
234 return ret;
235 }
236
237 /**
238 * Method compareTo.
239 * @param o CtfIterator
240 * @return int -1, 0, 1
241 */
242 @Override
243 public int compareTo(final CtfIterator o) {
244 if (this.getRank() < o.getRank()) {
245 return -1;
246 } else if (this.getRank() > o.getRank()) {
247 return 1;
248 }
249 return 0;
250 }
251 /* (non-Javadoc)
252 * @see java.lang.Object#hashCode()
253 */
254 @Override
255 public int hashCode() {
256 final int prime = 31;
257 int result = super.hashCode();
258 result = (prime * result)
259 + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode());
260 result = (prime * result)
261 + ((curLocation == null) ? 0 : curLocation.hashCode());
262 result = (prime * result) + (int) (curRank ^ (curRank >>> 32));
263 return result;
264 }
265
266 /* (non-Javadoc)
267 * @see java.lang.Object#equals(java.lang.Object)
268 */
269 @Override
270 public boolean equals(Object obj) {
271 if (this == obj) {
272 return true;
273 }
274 if (!super.equals(obj)) {
275 return false;
276 }
277 if (!(obj instanceof CtfIterator)) {
278 return false;
279 }
280 CtfIterator other = (CtfIterator) obj;
281 if (ctfTmfTrace == null) {
282 if (other.ctfTmfTrace != null) {
283 return false;
284 }
285 } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) {
286 return false;
287 }
288 if (curLocation == null) {
289 if (other.curLocation != null) {
290 return false;
291 }
292 } else if (!curLocation.equals(other.curLocation)) {
293 return false;
294 }
295 if (curRank != other.curRank) {
296 return false;
297 }
298 return true;
299 }
300 }
This page took 0.03729 seconds and 5 git commands to generate.