Commit | Line | Data |
---|---|---|
788ddcbc 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 | *******************************************************************************/ | |
11 | ||
12 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.ListIterator; | |
16 | ||
17 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; | |
19 | ||
20 | /** | |
21 | * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to | |
22 | * a boxed Long, a long and an int. | |
23 | * | |
132a02b0 | 24 | * @version 1.0 |
788ddcbc | 25 | * @author Matthew Khouzam |
788ddcbc MK |
26 | */ |
27 | public class CtfTmfLightweightContext implements ITmfContext { | |
28 | ||
788ddcbc MK |
29 | // ------------------------------------------- |
30 | // Fields | |
31 | // ------------------------------------------- | |
32 | private CtfLocation curLocation; | |
33 | private long curRank; | |
788ddcbc | 34 | |
53b235e1 | 35 | private final CtfTmfTrace fTrace; |
788ddcbc MK |
36 | |
37 | // ------------------------------------------- | |
38 | // Constructor | |
39 | // ------------------------------------------- | |
40 | /** | |
53b235e1 | 41 | * Deprecated, use CtfTmfLightweightContext( CtfTmfTrace please ) |
788ddcbc MK |
42 | * |
43 | * @param iters | |
44 | * the shared iterator pool. | |
45 | * @param pos | |
46 | * the iterator position. | |
47 | */ | |
53b235e1 | 48 | @Deprecated |
788ddcbc MK |
49 | public CtfTmfLightweightContext(ArrayList<CtfIterator> iters, |
50 | ListIterator<CtfIterator> pos) { | |
53b235e1 | 51 | fTrace = iters.get(0).getCtfTmfTrace(); |
132a02b0 | 52 | curLocation = new CtfLocation(new CtfLocationData(0, 0)); |
53b235e1 MK |
53 | } |
54 | ||
55 | /** | |
56 | * | |
57 | * @param ctfTmfTrace | |
58 | * the parent trace | |
59 | * @since 1.1 | |
60 | */ | |
61 | public CtfTmfLightweightContext(CtfTmfTrace ctfTmfTrace) { | |
62 | fTrace = ctfTmfTrace; | |
132a02b0 | 63 | curLocation = new CtfLocation(new CtfLocationData(0, 0)); |
788ddcbc MK |
64 | } |
65 | ||
66 | // ------------------------------------------- | |
67 | // TmfContext Overrides | |
68 | // ------------------------------------------- | |
69 | ||
70 | @Override | |
71 | public long getRank() { | |
72 | return curRank; | |
73 | } | |
74 | ||
75 | @Override | |
1e1bef82 | 76 | public ITmfLocation getLocation() { |
788ddcbc MK |
77 | return curLocation; |
78 | } | |
79 | ||
80 | @Override | |
81 | public boolean hasValidRank() { | |
132a02b0 | 82 | return curRank != CtfLocation.INVALID_LOCATION.getTimestamp(); |
788ddcbc MK |
83 | } |
84 | ||
85 | @Override | |
1e1bef82 | 86 | public void setLocation(ITmfLocation location) { |
788ddcbc | 87 | curLocation = (CtfLocation) location; |
5976d44a | 88 | getIterator().seek(curLocation.getLocationInfo()); |
788ddcbc MK |
89 | } |
90 | ||
91 | @Override | |
92 | public void setRank(long rank) { | |
93 | curRank = rank; | |
94 | ||
95 | } | |
96 | ||
97 | @Override | |
98 | public void increaseRank() { | |
99 | if (hasValidRank()) { | |
100 | curRank++; | |
101 | } | |
102 | } | |
103 | ||
104 | // ------------------------------------------- | |
105 | // CtfTmfTrace Helpers | |
106 | // ------------------------------------------- | |
107 | ||
108 | /** | |
109 | * Gets the current event. Wrapper to help CtfTmfTrace | |
53b235e1 | 110 | * |
788ddcbc MK |
111 | * @return The event or null |
112 | */ | |
113 | public synchronized CtfTmfEvent getCurrentEvent() { | |
53b235e1 | 114 | return getIterator().getCurrentEvent(); |
788ddcbc MK |
115 | } |
116 | ||
117 | /** | |
118 | * Advances to a the next event. Wrapper to help CtfTmfTrace | |
53b235e1 | 119 | * |
788ddcbc MK |
120 | * @return success or not |
121 | */ | |
122 | public synchronized boolean advance() { | |
5976d44a | 123 | final CtfLocationData curLocationData = this.curLocation.getLocationInfo(); |
53b235e1 MK |
124 | boolean retVal = getIterator().advance(); |
125 | CtfTmfEvent currentEvent = getIterator().getCurrentEvent(); | |
132a02b0 | 126 | |
788ddcbc | 127 | if (currentEvent != null) { |
58f3bc52 | 128 | final long timestampValue = currentEvent.getTimestamp().getValue(); |
132a02b0 MK |
129 | if (curLocationData.getTimestamp() == timestampValue) { |
130 | curLocation.setLocation(timestampValue, curLocationData.getIndex() + 1); | |
131 | } else { | |
132 | curLocation.setLocation(timestampValue, 0L); | |
133 | } | |
788ddcbc MK |
134 | } else { |
135 | curLocation.setLocation(CtfLocation.INVALID_LOCATION); | |
136 | } | |
137 | ||
138 | return retVal; | |
139 | } | |
140 | ||
141 | @Override | |
142 | public void dispose() { | |
143 | // do nothing | |
144 | } | |
145 | ||
146 | /** | |
147 | * Seeks to a given timestamp. Wrapper to help CtfTmfTrace | |
53b235e1 MK |
148 | * |
149 | * @param timestamp | |
150 | * desired timestamp | |
788ddcbc MK |
151 | * @return success or not |
152 | */ | |
153 | public synchronized boolean seek(final long timestamp) { | |
132a02b0 | 154 | curLocation.setLocation(timestamp, 0); |
53b235e1 | 155 | return getIterator().seek(timestamp); |
788ddcbc MK |
156 | } |
157 | ||
132a02b0 MK |
158 | /** |
159 | * Seeks to a given location. Wrapper to help CtfTmfTrace | |
160 | * @param location | |
161 | * unique location to find the event. | |
162 | * | |
163 | * @return success or not | |
164 | * @since 2.0 | |
165 | */ | |
166 | public synchronized boolean seek(final CtfLocationData location) { | |
167 | curLocation.setLocation(location); | |
168 | return getIterator().seek(location); | |
169 | } | |
170 | ||
788ddcbc MK |
171 | /* |
172 | * (non-Javadoc) | |
173 | * | |
174 | * @see java.lang.Object#clone() | |
175 | */ | |
176 | @Override | |
177 | public CtfTmfLightweightContext clone() { | |
53b235e1 | 178 | CtfTmfLightweightContext ret = new CtfTmfLightweightContext(fTrace); |
788ddcbc MK |
179 | ret.curLocation = curLocation.clone(); |
180 | ret.curRank = curRank; | |
181 | return ret; | |
182 | } | |
183 | ||
184 | // ------------------------------------------- | |
185 | // Private helpers | |
186 | // ------------------------------------------- | |
187 | /** | |
53b235e1 MK |
188 | * Get iterator, called every time to get an iterator, no local copy is |
189 | * stored so that there is no need to "update" | |
788ddcbc | 190 | * |
53b235e1 | 191 | * @return an iterator |
788ddcbc MK |
192 | */ |
193 | private CtfIterator getIterator() { | |
53b235e1 | 194 | return CtfIteratorManager.getIterator(fTrace, this); |
788ddcbc | 195 | } |
788ddcbc | 196 | } |