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
.tracecompass
.tmf
.ctf
.core
;
16 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
17 import org
.eclipse
.tracecompass
.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 synchronized ITmfLocation
getLocation() {
72 public boolean hasValidRank() {
73 return fCurRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
80 public synchronized 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 CtfIterator iterator
= getIterator();
130 boolean retVal
= iterator
.advance();
131 CtfTmfEvent currentEvent
= iterator
.getCurrentEvent();
133 if (currentEvent
!= null) {
134 final long timestampValue
= iterator
.getCurrentTimestamp();
135 if (curLocationData
.getTimestamp() == timestampValue
) {
136 fCurLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
138 fCurLocation
= new CtfLocation(timestampValue
, 0L);
141 fCurLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
148 public void dispose() {
149 fTrace
.getIteratorManager().removeIterator(this);
153 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
157 * @return success or not
159 public synchronized boolean seek(final long timestamp
) {
160 fCurLocation
= new CtfLocation(timestamp
, 0);
161 return getIterator().seek(timestamp
);
165 * Seeks to a given location. Wrapper to help CtfTmfTrace
167 * unique location to find the event.
169 * @return success or not
172 public synchronized boolean seek(final CtfLocationInfo location
) {
173 fCurLocation
= new CtfLocation(location
);
174 return getIterator().seek(location
);
177 // -------------------------------------------
179 // -------------------------------------------
182 * Get iterator, called every time to get an iterator, no local copy is
183 * stored so that there is no need to "update"
185 * @return an iterator
187 private CtfIterator
getIterator() {
188 return fTrace
.getIteratorManager().getIterator(this);