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