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
.context
;
16 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
17 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
18 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
19 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
20 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
23 * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
24 * a boxed Long, a long and an int.
26 * @author Matthew Khouzam
30 public class CtfTmfContext
implements ITmfContext
{
32 // -------------------------------------------
34 // -------------------------------------------
36 private CtfLocation fCurLocation
;
37 private long fCurRank
;
39 private final CtfTmfTrace fTrace
;
41 // -------------------------------------------
43 // -------------------------------------------
52 public CtfTmfContext(CtfTmfTrace ctfTmfTrace
) {
54 fCurLocation
= new CtfLocation(new CtfLocationInfo(0, 0));
57 // -------------------------------------------
58 // TmfContext Overrides
59 // -------------------------------------------
62 public long getRank() {
70 public synchronized ITmfLocation
getLocation() {
75 public boolean hasValidRank() {
76 return fCurRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
83 public synchronized void setLocation(ITmfLocation location
) {
84 fCurLocation
= (CtfLocation
) location
;
85 if (fCurLocation
!= null) {
86 getIterator().seek(fCurLocation
.getLocationInfo());
91 public void setRank(long rank
) {
97 public void increaseRank() {
103 // -------------------------------------------
104 // CtfTmfTrace Helpers
105 // -------------------------------------------
108 * Gets the trace of this context.
110 * @return The trace of this context
112 public CtfTmfTrace
getTrace() {
117 * Gets the current event. Wrapper to help CtfTmfTrace
119 * @return The event or null
121 public synchronized CtfTmfEvent
getCurrentEvent() {
122 return getIterator().getCurrentEvent();
126 * Advances to a the next event. Wrapper to help CtfTmfTrace
128 * @return success or not
130 public synchronized boolean advance() {
131 final CtfLocationInfo curLocationData
= fCurLocation
.getLocationInfo();
132 CtfIterator iterator
= getIterator();
133 boolean retVal
= iterator
.advance();
134 CtfTmfEvent currentEvent
= iterator
.getCurrentEvent();
136 if (currentEvent
!= null) {
137 final long timestampValue
= iterator
.getCurrentTimestamp();
138 if (curLocationData
.getTimestamp() == timestampValue
) {
139 fCurLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
141 fCurLocation
= new CtfLocation(timestampValue
, 0L);
144 fCurLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
151 public void dispose() {
152 fTrace
.getIteratorManager().removeIterator(this);
156 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
160 * @return success or not
162 public synchronized boolean seek(final long timestamp
) {
163 fCurLocation
= new CtfLocation(timestamp
, 0);
164 return getIterator().seek(timestamp
);
168 * Seeks to a given location. Wrapper to help CtfTmfTrace
170 * unique location to find the event.
172 * @return success or not
175 public synchronized boolean seek(final CtfLocationInfo location
) {
176 fCurLocation
= new CtfLocation(location
);
177 return getIterator().seek(location
);
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 fTrace
.getIteratorManager().getIterator(this);