1 /*******************************************************************************
2 * Copyright (c) 2012 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
.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() {
62 public ITmfLocation
getLocation() {
67 public boolean hasValidRank() {
68 return curRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
72 public void setLocation(ITmfLocation location
) {
73 curLocation
= (CtfLocation
) location
;
74 if (curLocation
!= null) {
75 getIterator().seek(curLocation
.getLocationInfo());
80 public void setRank(long rank
) {
86 public void increaseRank() {
92 // -------------------------------------------
93 // CtfTmfTrace Helpers
94 // -------------------------------------------
97 * Gets the trace of this context.
99 * @return The trace of this context
101 public CtfTmfTrace
getTrace() {
106 * Gets the current event. Wrapper to help CtfTmfTrace
108 * @return The event or null
110 public synchronized CtfTmfEvent
getCurrentEvent() {
111 return getIterator().getCurrentEvent();
115 * Advances to a the next event. Wrapper to help CtfTmfTrace
117 * @return success or not
119 public synchronized boolean advance() {
120 final CtfLocationInfo curLocationData
= this.curLocation
.getLocationInfo();
121 boolean retVal
= getIterator().advance();
122 CtfTmfEvent currentEvent
= getIterator().getCurrentEvent();
124 if (currentEvent
!= null) {
125 final long timestampValue
= currentEvent
.getTimestamp().getValue();
126 if (curLocationData
.getTimestamp() == timestampValue
) {
127 curLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
129 curLocation
= new CtfLocation(timestampValue
, 0L);
132 curLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
139 public void dispose() {
144 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
148 * @return success or not
150 public synchronized boolean seek(final long timestamp
) {
151 curLocation
= new CtfLocation(timestamp
, 0);
152 return getIterator().seek(timestamp
);
156 * Seeks to a given location. Wrapper to help CtfTmfTrace
158 * unique location to find the event.
160 * @return success or not
163 public synchronized boolean seek(final CtfLocationInfo location
) {
164 curLocation
= new CtfLocation(location
);
165 return getIterator().seek(location
);
169 public CtfTmfContext
clone() {
170 CtfTmfContext ret
= null;
172 ret
= (CtfTmfContext
) super.clone();
173 /* Fields are immutable, no need to deep-copy them */
174 } catch (CloneNotSupportedException e
) {
175 /* Should not happen, we're calling Object.clone() */
180 // -------------------------------------------
182 // -------------------------------------------
185 * Get iterator, called every time to get an iterator, no local copy is
186 * stored so that there is no need to "update"
188 * @return an iterator
190 private CtfIterator
getIterator() {
191 return CtfIteratorManager
.getIterator(fTrace
, this);