tmf.ctf: Do not expose CtfIteratorManager publicly
[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.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;
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 */
28 public class CtfTmfContext implements ITmfContext {
29
30 // -------------------------------------------
31 // Fields
32 // -------------------------------------------
33
34 private CtfLocation fCurLocation;
35 private long fCurRank;
36
37 private final CtfTmfTrace fTrace;
38
39 // -------------------------------------------
40 // Constructor
41 // -------------------------------------------
42
43 /**
44 * Constructor
45 *
46 * @param ctfTmfTrace
47 * the parent trace
48 */
49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace) {
50 fTrace = ctfTmfTrace;
51 fCurLocation = new CtfLocation(new CtfLocationInfo(0, 0));
52 }
53
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
57
58 @Override
59 public long getRank() {
60 return fCurRank;
61 }
62
63 @Override
64 public synchronized ITmfLocation getLocation() {
65 return fCurLocation;
66 }
67
68 @Override
69 public boolean hasValidRank() {
70 return fCurRank != CtfLocation.INVALID_LOCATION.getTimestamp();
71 }
72
73 @Override
74 public synchronized void setLocation(ITmfLocation location) {
75 fCurLocation = (CtfLocation) location;
76 if (fCurLocation != null) {
77 getIterator().seek(fCurLocation.getLocationInfo());
78 }
79 }
80
81 @Override
82 public void setRank(long rank) {
83 fCurRank = rank;
84
85 }
86
87 @Override
88 public void increaseRank() {
89 if (hasValidRank()) {
90 fCurRank++;
91 }
92 }
93
94 // -------------------------------------------
95 // CtfTmfTrace Helpers
96 // -------------------------------------------
97
98 /**
99 * Gets the trace of this context.
100 *
101 * @return The trace of this context
102 */
103 public CtfTmfTrace getTrace() {
104 return fTrace;
105 }
106
107 /**
108 * Gets the current event. Wrapper to help CtfTmfTrace
109 *
110 * @return The event or null
111 */
112 public synchronized CtfTmfEvent getCurrentEvent() {
113 return getIterator().getCurrentEvent();
114 }
115
116 /**
117 * Advances to a the next event. Wrapper to help CtfTmfTrace
118 *
119 * @return success or not
120 */
121 public synchronized boolean advance() {
122 final CtfLocationInfo curLocationData = fCurLocation.getLocationInfo();
123 CtfIterator iterator = getIterator();
124 boolean retVal = iterator.advance();
125 CtfTmfEvent currentEvent = iterator.getCurrentEvent();
126
127 if (currentEvent != null) {
128 final long timestampValue = iterator.getCurrentTimestamp();
129 if (curLocationData.getTimestamp() == timestampValue) {
130 fCurLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
131 } else {
132 fCurLocation = new CtfLocation(timestampValue, 0L);
133 }
134 } else {
135 fCurLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
136 }
137
138 return retVal;
139 }
140
141 @Override
142 public void dispose() {
143 fTrace.disposeContext(this);
144 }
145
146 /**
147 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
148 *
149 * @param timestamp
150 * desired timestamp
151 * @return success or not
152 */
153 public synchronized boolean seek(final long timestamp) {
154 fCurLocation = new CtfLocation(timestamp, 0);
155 return getIterator().seek(timestamp);
156 }
157
158 /**
159 * Seeks to a given location. Wrapper to help CtfTmfTrace
160 * @param location
161 * unique location to find the event.
162 *
163 * @return success or not
164 */
165 public synchronized boolean seek(final CtfLocationInfo location) {
166 fCurLocation = new CtfLocation(location);
167 return getIterator().seek(location);
168 }
169
170 // -------------------------------------------
171 // Private helpers
172 // -------------------------------------------
173
174 /**
175 * Get iterator, called every time to get an iterator, no local copy is
176 * stored so that there is no need to "update"
177 *
178 * @return an iterator
179 */
180 private CtfIterator getIterator() {
181 return (CtfIterator) fTrace.createIteratorFromContext(this);
182 }
183 }
This page took 0.046022 seconds and 5 git commands to generate.