1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
14 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
15 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
18 * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
19 * a boxed Long, a long and an int.
21 * @author Matthew Khouzam
25 public class CtfTmfContext
implements ITmfContext
{
27 // -------------------------------------------
29 // -------------------------------------------
31 private CtfLocation curLocation
;
34 private final CtfTmfTrace fTrace
;
36 // -------------------------------------------
38 // -------------------------------------------
47 public CtfTmfContext(CtfTmfTrace ctfTmfTrace
) {
49 curLocation
= new CtfLocation(new CtfLocationInfo(0, 0));
52 // -------------------------------------------
53 // TmfContext Overrides
54 // -------------------------------------------
57 public long getRank() {
65 public ITmfLocation
getLocation() {
70 public boolean hasValidRank() {
71 return curRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
78 public void setLocation(ITmfLocation location
) {
79 curLocation
= (CtfLocation
) location
;
80 if (curLocation
!= null) {
81 getIterator().seek(curLocation
.getLocationInfo());
86 public void setRank(long rank
) {
92 public void increaseRank() {
98 // -------------------------------------------
99 // CtfTmfTrace Helpers
100 // -------------------------------------------
103 * Gets the trace of this context.
105 * @return The trace of this context
107 public CtfTmfTrace
getTrace() {
112 * Gets the current event. Wrapper to help CtfTmfTrace
114 * @return The event or null
116 public synchronized CtfTmfEvent
getCurrentEvent() {
117 return getIterator().getCurrentEvent();
121 * Advances to a the next event. Wrapper to help CtfTmfTrace
123 * @return success or not
125 public synchronized boolean advance() {
126 final CtfLocationInfo curLocationData
= this.curLocation
.getLocationInfo();
127 boolean retVal
= getIterator().advance();
128 CtfTmfEvent currentEvent
= getIterator().getCurrentEvent();
130 if (currentEvent
!= null) {
131 final long timestampValue
= currentEvent
.getTimestamp().getValue();
132 if (curLocationData
.getTimestamp() == timestampValue
) {
133 curLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
135 curLocation
= new CtfLocation(timestampValue
, 0L);
138 curLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
145 public void dispose() {
150 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
154 * @return success or not
156 public synchronized boolean seek(final long timestamp
) {
157 curLocation
= new CtfLocation(timestamp
, 0);
158 return getIterator().seek(timestamp
);
162 * Seeks to a given location. Wrapper to help CtfTmfTrace
164 * unique location to find the event.
166 * @return success or not
169 public synchronized boolean seek(final CtfLocationInfo location
) {
170 curLocation
= new CtfLocation(location
);
171 return getIterator().seek(location
);
175 public CtfTmfContext
clone() {
176 CtfTmfContext ret
= null;
178 ret
= (CtfTmfContext
) super.clone();
179 /* Fields are immutable, no need to deep-copy them */
180 } catch (CloneNotSupportedException e
) {
181 /* Should not happen, we're calling Object.clone() */
186 // -------------------------------------------
188 // -------------------------------------------
191 * Get iterator, called every time to get an iterator, no local copy is
192 * stored so that there is no need to "update"
194 * @return an iterator
196 private CtfIterator
getIterator() {
197 return CtfIteratorManager
.getIterator(fTrace
, this);