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