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
.ctf
.core
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.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 fCurLocation
;
34 private long fCurRank
;
36 private final CtfTmfTrace fTrace
;
38 // -------------------------------------------
40 // -------------------------------------------
49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace
) {
51 fCurLocation
= new CtfLocation(new CtfLocationInfo(0, 0));
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
59 public long getRank() {
67 public ITmfLocation
getLocation() {
72 public boolean hasValidRank() {
73 return fCurRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
80 public void setLocation(ITmfLocation location
) {
81 fCurLocation
= (CtfLocation
) location
;
82 if (fCurLocation
!= null) {
83 getIterator().seek(fCurLocation
.getLocationInfo());
88 public void setRank(long rank
) {
94 public void increaseRank() {
100 // -------------------------------------------
101 // CtfTmfTrace Helpers
102 // -------------------------------------------
105 * Gets the trace of this context.
107 * @return The trace of this context
109 public CtfTmfTrace
getTrace() {
114 * Gets the current event. Wrapper to help CtfTmfTrace
116 * @return The event or null
118 public synchronized CtfTmfEvent
getCurrentEvent() {
119 return getIterator().getCurrentEvent();
123 * Advances to a the next event. Wrapper to help CtfTmfTrace
125 * @return success or not
127 public synchronized boolean advance() {
128 final CtfLocationInfo curLocationData
= fCurLocation
.getLocationInfo();
129 boolean retVal
= getIterator().advance();
130 CtfTmfEvent currentEvent
= getIterator().getCurrentEvent();
132 if (currentEvent
!= null) {
133 final long timestampValue
= currentEvent
.getTimestamp().getValue();
134 if (curLocationData
.getTimestamp() == timestampValue
) {
135 fCurLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
137 fCurLocation
= new CtfLocation(timestampValue
, 0L);
140 fCurLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
147 public void dispose() {
148 CtfIteratorManager
.removeIterator(fTrace
, this);
152 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
156 * @return success or not
158 public synchronized boolean seek(final long timestamp
) {
159 fCurLocation
= new CtfLocation(timestamp
, 0);
160 return getIterator().seek(timestamp
);
164 * Seeks to a given location. Wrapper to help CtfTmfTrace
166 * unique location to find the event.
168 * @return success or not
171 public synchronized boolean seek(final CtfLocationInfo location
) {
172 fCurLocation
= new CtfLocation(location
);
173 return getIterator().seek(location
);
177 public CtfTmfContext
clone() {
178 CtfTmfContext ret
= null;
180 ret
= (CtfTmfContext
) super.clone();
181 /* Fields are immutable, no need to deep-copy them */
182 } catch (CloneNotSupportedException e
) {
183 /* Should not happen, we're calling Object.clone() */
188 // -------------------------------------------
190 // -------------------------------------------
193 * Get iterator, called every time to get an iterator, no local copy is
194 * stored so that there is no need to "update"
196 * @return an iterator
198 private CtfIterator
getIterator() {
199 return CtfIteratorManager
.getIterator(fTrace
, this);