1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
17 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
19 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
20 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
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
28 public class CtfTmfContext
implements ITmfContext
{
30 // -------------------------------------------
32 // -------------------------------------------
34 private CtfLocation fCurLocation
;
35 private long fCurRank
;
37 private final CtfTmfTrace fTrace
;
39 // -------------------------------------------
41 // -------------------------------------------
49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace
) {
51 fCurLocation
= new CtfLocation(new CtfLocationInfo(0, 0));
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
59 public long getRank() {
64 public synchronized ITmfLocation
getLocation() {
69 public boolean hasValidRank() {
70 return fCurRank
!= CtfLocation
.INVALID_LOCATION
.getTimestamp();
74 public synchronized void setLocation(ITmfLocation location
) {
75 if (location
instanceof CtfLocation
) {
76 CtfIterator iterator
= getIterator();
77 iterator
.seek(((CtfLocation
) location
).getLocationInfo());
78 fCurLocation
= iterator
.getLocation();
85 public void setRank(long rank
) {
91 public void increaseRank() {
97 // -------------------------------------------
98 // CtfTmfTrace Helpers
99 // -------------------------------------------
102 * Gets the trace of this context.
104 * @return The trace of this context
106 public CtfTmfTrace
getTrace() {
111 * Gets the current event. Wrapper to help CtfTmfTrace
113 * @return The event or null
115 public synchronized CtfTmfEvent
getCurrentEvent() {
116 return getIterator().getCurrentEvent();
120 * Advances to a the next event. Wrapper to help CtfTmfTrace
122 * @return success or not
124 public synchronized boolean advance() {
125 final CtfLocationInfo curLocationData
= fCurLocation
.getLocationInfo();
126 CtfIterator iterator
= getIterator();
127 boolean retVal
= iterator
.advance();
128 CtfTmfEvent currentEvent
= iterator
.getCurrentEvent();
130 if (currentEvent
!= null) {
131 final long timestampValue
= iterator
.getCurrentTimestamp();
132 if (curLocationData
.getTimestamp() == timestampValue
) {
133 fCurLocation
= new CtfLocation(timestampValue
, curLocationData
.getIndex() + 1);
135 fCurLocation
= new CtfLocation(timestampValue
, 0L);
138 fCurLocation
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
145 public void dispose() {
146 fTrace
.disposeContext(this);
150 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
154 * @return success or not
156 public synchronized boolean seek(final long timestamp
) {
157 CtfIterator iterator
= getIterator();
158 boolean ret
= iterator
.seek(timestamp
);
159 fCurLocation
= iterator
.getLocation();
164 * Seeks to a given location. Wrapper to help CtfTmfTrace
166 * unique location to find the event.
168 * @return success or not
170 public synchronized boolean seek(final CtfLocationInfo location
) {
171 fCurLocation
= new CtfLocation(location
);
172 return getIterator().seek(location
);
175 // -------------------------------------------
177 // -------------------------------------------
180 * Get iterator, called every time to get an iterator, no local copy is
181 * stored so that there is no need to "update"
183 * @return an iterator
185 private CtfIterator
getIterator() {
186 return (CtfIterator
) fTrace
.createIteratorFromContext(this);