tmf.ctf: Move each CtfIteratorManager into its own trace object
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / CtfTmfContext.java
CommitLineData
788ddcbc 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 Ericsson
788ddcbc
MK
3 *
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
8 *
f0414df8
SD
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Delisle - Remove the iterator in dispose()
788ddcbc
MK
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.ctf.core;
788ddcbc 15
2bdf0193
AM
16import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
17import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
788ddcbc
MK
18
19/**
20 * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
21 * a boxed Long, a long and an int.
22 *
23 * @author Matthew Khouzam
81a2d02e
AM
24 * @version 1.0
25 * @since 2.0
788ddcbc 26 */
81a2d02e 27public class CtfTmfContext implements ITmfContext {
788ddcbc 28
788ddcbc
MK
29 // -------------------------------------------
30 // Fields
31 // -------------------------------------------
81a2d02e 32
b94865c3
MK
33 private CtfLocation fCurLocation;
34 private long fCurRank;
788ddcbc 35
53b235e1 36 private final CtfTmfTrace fTrace;
788ddcbc
MK
37
38 // -------------------------------------------
39 // Constructor
40 // -------------------------------------------
53b235e1
MK
41
42 /**
81a2d02e 43 * Constructor
53b235e1
MK
44 *
45 * @param ctfTmfTrace
46 * the parent trace
47 * @since 1.1
48 */
81a2d02e 49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace) {
53b235e1 50 fTrace = ctfTmfTrace;
b94865c3 51 fCurLocation = new CtfLocation(new CtfLocationInfo(0, 0));
788ddcbc
MK
52 }
53
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
57
58 @Override
59 public long getRank() {
b94865c3 60 return fCurRank;
788ddcbc
MK
61 }
62
a3db8436
AM
63 /**
64 * @since 3.0
65 */
788ddcbc 66 @Override
51a7e2f6 67 public synchronized ITmfLocation getLocation() {
b94865c3 68 return fCurLocation;
788ddcbc
MK
69 }
70
71 @Override
72 public boolean hasValidRank() {
b94865c3 73 return fCurRank != CtfLocation.INVALID_LOCATION.getTimestamp();
788ddcbc
MK
74 }
75
a3db8436
AM
76 /**
77 * @since 3.0
78 */
788ddcbc 79 @Override
51a7e2f6 80 public synchronized void setLocation(ITmfLocation location) {
b94865c3
MK
81 fCurLocation = (CtfLocation) location;
82 if (fCurLocation != null) {
83 getIterator().seek(fCurLocation.getLocationInfo());
575beffc 84 }
788ddcbc
MK
85 }
86
87 @Override
88 public void setRank(long rank) {
b94865c3 89 fCurRank = rank;
788ddcbc
MK
90
91 }
92
93 @Override
94 public void increaseRank() {
95 if (hasValidRank()) {
b94865c3 96 fCurRank++;
788ddcbc
MK
97 }
98 }
99
100 // -------------------------------------------
101 // CtfTmfTrace Helpers
102 // -------------------------------------------
103
81a2d02e
AM
104 /**
105 * Gets the trace of this context.
106 *
107 * @return The trace of this context
108 */
109 public CtfTmfTrace getTrace() {
110 return fTrace;
111 }
112
788ddcbc
MK
113 /**
114 * Gets the current event. Wrapper to help CtfTmfTrace
53b235e1 115 *
788ddcbc
MK
116 * @return The event or null
117 */
118 public synchronized CtfTmfEvent getCurrentEvent() {
53b235e1 119 return getIterator().getCurrentEvent();
788ddcbc
MK
120 }
121
122 /**
123 * Advances to a the next event. Wrapper to help CtfTmfTrace
53b235e1 124 *
788ddcbc
MK
125 * @return success or not
126 */
127 public synchronized boolean advance() {
b94865c3 128 final CtfLocationInfo curLocationData = fCurLocation.getLocationInfo();
962fb72f
PT
129 CtfIterator iterator = getIterator();
130 boolean retVal = iterator.advance();
131 CtfTmfEvent currentEvent = iterator.getCurrentEvent();
132a02b0 132
788ddcbc 133 if (currentEvent != null) {
962fb72f 134 final long timestampValue = iterator.getCurrentTimestamp();
132a02b0 135 if (curLocationData.getTimestamp() == timestampValue) {
b94865c3 136 fCurLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
132a02b0 137 } else {
b94865c3 138 fCurLocation = new CtfLocation(timestampValue, 0L);
132a02b0 139 }
788ddcbc 140 } else {
b94865c3 141 fCurLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
788ddcbc
MK
142 }
143
144 return retVal;
145 }
146
147 @Override
148 public void dispose() {
6a0ec7bc 149 fTrace.getIteratorManager().removeIterator(this);
788ddcbc
MK
150 }
151
152 /**
153 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
53b235e1
MK
154 *
155 * @param timestamp
156 * desired timestamp
788ddcbc
MK
157 * @return success or not
158 */
159 public synchronized boolean seek(final long timestamp) {
b94865c3 160 fCurLocation = new CtfLocation(timestamp, 0);
53b235e1 161 return getIterator().seek(timestamp);
788ddcbc
MK
162 }
163
132a02b0
MK
164 /**
165 * Seeks to a given location. Wrapper to help CtfTmfTrace
166 * @param location
167 * unique location to find the event.
168 *
169 * @return success or not
170 * @since 2.0
171 */
f5df94f8 172 public synchronized boolean seek(final CtfLocationInfo location) {
b94865c3 173 fCurLocation = new CtfLocation(location);
132a02b0
MK
174 return getIterator().seek(location);
175 }
176
788ddcbc
MK
177 // -------------------------------------------
178 // Private helpers
179 // -------------------------------------------
81a2d02e 180
788ddcbc 181 /**
53b235e1
MK
182 * Get iterator, called every time to get an iterator, no local copy is
183 * stored so that there is no need to "update"
788ddcbc 184 *
53b235e1 185 * @return an iterator
788ddcbc
MK
186 */
187 private CtfIterator getIterator() {
6a0ec7bc 188 return fTrace.getIteratorManager().getIterator(this);
788ddcbc 189 }
788ddcbc 190}
This page took 0.063323 seconds and 5 git commands to generate.