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