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
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Delisle - Remove the iterator in dispose()
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
20 * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
21 * a boxed Long, a long and an int.
23 * @author Matthew Khouzam
27 public class CtfTmfContext
implements ITmfContext
{
29 // -------------------------------------------
31 // -------------------------------------------
33 private CtfLocation curLocation
;
36 private final CtfTmfTrace fTrace
;
38 // -------------------------------------------
40 // -------------------------------------------
49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace
) {
51 curLocation
= new CtfLocation(new CtfLocationInfo(0, 0));
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
59 public long getRank() {
64 public ITmfLocation
getLocation() {
69 public boolean hasValidRank() {
70 return curRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
74 public void setLocation(ITmfLocation location
) {
75 curLocation
= (CtfLocation
) location
;
76 if (curLocation
!= null) {
77 getIterator().seek(curLocation
.getLocationInfo());
82 public void setRank(long rank
) {
88 public void increaseRank() {
94 // -------------------------------------------
95 // CtfTmfTrace Helpers
96 // -------------------------------------------
99 * Gets the trace of this context.
101 * @return The trace of this context
103 public CtfTmfTrace
getTrace() {
108 * Gets the current event. Wrapper to help CtfTmfTrace
110 * @return The event or null
112 public synchronized CtfTmfEvent
getCurrentEvent() {
113 return getIterator().getCurrentEvent();
117 * Advances to a the next event. Wrapper to help CtfTmfTrace
119 * @return success or not
121 public synchronized boolean advance() {
122 final CtfLocationInfo curLocationData
= this.curLocation
.getLocationInfo();
123 boolean retVal
= getIterator().advance();
124 CtfTmfEvent currentEvent
= getIterator().getCurrentEvent();
126 if (currentEvent
!= null) {
127 final long timestampValue
= currentEvent
.getTimestamp().getValue();
128 if (curLocationData
.getTimestamp() == timestampValue
) {
129 curLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
131 curLocation
= new CtfLocation(timestampValue
, 0L);
134 curLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
141 public void dispose() {
142 CtfIteratorManager
.removeIterator(fTrace
, this);
146 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
150 * @return success or not
152 public synchronized boolean seek(final long timestamp
) {
153 curLocation
= new CtfLocation(timestamp
, 0);
154 return getIterator().seek(timestamp
);
158 * Seeks to a given location. Wrapper to help CtfTmfTrace
160 * unique location to find the event.
162 * @return success or not
165 public synchronized boolean seek(final CtfLocationInfo location
) {
166 curLocation
= new CtfLocation(location
);
167 return getIterator().seek(location
);
171 public CtfTmfContext
clone() {
172 CtfTmfContext ret
= null;
174 ret
= (CtfTmfContext
) super.clone();
175 /* Fields are immutable, no need to deep-copy them */
176 } catch (CloneNotSupportedException e
) {
177 /* Should not happen, we're calling Object.clone() */
182 // -------------------------------------------
184 // -------------------------------------------
187 * Get iterator, called every time to get an iterator, no local copy is
188 * stored so that there is no need to "update"
190 * @return an iterator
192 private CtfIterator
getIterator() {
193 return CtfIteratorManager
.getIterator(fTrace
, this);