Commit | Line | Data |
---|---|---|
b6cfa2bb FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.trace; | |
14 | ||
15 | import java.util.Iterator; | |
16 | ||
17 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
18 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; | |
19 | ||
20 | /** | |
21 | * A simple class to iterate over a TMF trace and return ITmfEvent:s. Its main | |
22 | * purpose is to encapsulate the ITmfContext. | |
23 | * | |
24 | * @author Francois Chouinard | |
25 | * @version 1.0 | |
26 | * @since 2.0 | |
27 | */ | |
28 | public class TmfTraceIterator implements Iterator<ITmfEvent> { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | private ITmfTrace fTrace; // The trace to iterate over | |
35 | private ITmfContext fContext; // The trace reading context | |
36 | private ITmfEvent fNextEvent; // The buffered next event | |
37 | ||
38 | // ------------------------------------------------------------------------ | |
39 | // Constructors | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | /** | |
43 | * The standard constructor. Returns an iterator pointing to the start of | |
44 | * the trace. | |
45 | * | |
46 | * @param trace the trace to iterate over | |
47 | */ | |
48 | public TmfTraceIterator(ITmfTrace trace) { | |
49 | this(trace, 0); | |
50 | } | |
51 | ||
52 | /** | |
53 | * The rank constructor. Returns an iterator pointing to the event | |
54 | * at the requested rank. | |
55 | * | |
56 | * @param trace the trace to iterate over | |
57 | * @param rank the starting event rank | |
58 | */ | |
59 | public TmfTraceIterator(ITmfTrace trace, long rank) { | |
60 | fTrace = trace; | |
61 | fContext = fTrace.seekEvent(rank); | |
62 | } | |
63 | ||
64 | /** | |
65 | * The timestamp constructor. Returns an iterator pointing to the event | |
66 | * at the requested timestamp. | |
67 | * | |
68 | * @param trace the trace to iterate over | |
69 | * @param timestamp the starting event timestamp | |
70 | */ | |
71 | public TmfTraceIterator(ITmfTrace trace, ITmfTimestamp timestamp) { | |
72 | fTrace = trace; | |
73 | fContext = fTrace.seekEvent(timestamp); | |
74 | } | |
75 | ||
76 | /** | |
77 | * The location constructor. Returns an iterator pointing to the event | |
78 | * at the requested location. | |
79 | * | |
80 | * @param trace the trace to iterate over | |
81 | * @param location the starting event location | |
82 | */ | |
83 | public TmfTraceIterator(ITmfTrace trace, ITmfLocation location) { | |
84 | fTrace = trace; | |
85 | fContext = fTrace.seekEvent(location); | |
86 | } | |
87 | ||
88 | /** | |
89 | * The ratio constructor. Returns an iterator pointing to the event | |
90 | * at the requested ratio. | |
91 | * | |
92 | * @param trace the trace to iterate over | |
93 | * @param ratio the starting event ratio | |
94 | */ | |
95 | public TmfTraceIterator(ITmfTrace trace, double ratio) { | |
96 | fTrace = trace; | |
97 | fContext = fTrace.seekEvent(ratio); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Copy constructor | |
102 | * | |
103 | * @param other the other iterator | |
104 | */ | |
105 | public TmfTraceIterator(TmfTraceIterator other) { | |
106 | fTrace = other.fTrace; | |
107 | fContext = other.fContext.clone(); | |
108 | } | |
109 | ||
110 | // ------------------------------------------------------------------------ | |
111 | // Iterator | |
112 | // ------------------------------------------------------------------------ | |
113 | ||
114 | /* (non-Javadoc) | |
115 | * @see java.util.Iterator#hasNext() | |
116 | */ | |
117 | @Override | |
118 | public boolean hasNext() { | |
119 | if (fNextEvent == null) { | |
120 | fNextEvent = fTrace.getNext(fContext); | |
121 | } | |
122 | return fNextEvent != null; | |
123 | } | |
124 | ||
125 | /* (non-Javadoc) | |
126 | * @see java.util.Iterator#next() | |
127 | */ | |
128 | @Override | |
129 | public ITmfEvent next() { | |
130 | ITmfEvent event; | |
131 | if (fNextEvent != null) { | |
132 | event = fNextEvent; | |
133 | fNextEvent = null; | |
134 | } else { | |
135 | event = fTrace.getNext(fContext); | |
136 | } | |
137 | return event; | |
138 | } | |
139 | ||
140 | /* (non-Javadoc) | |
141 | * @see java.util.Iterator#remove() | |
142 | */ | |
143 | @Override | |
144 | public void remove() { | |
145 | throw new UnsupportedOperationException(); | |
146 | } | |
147 | ||
148 | } |