46b61a0e1723b23b64b9dba5648eceabf7e5df11
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfContext.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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.linuxtools.tmf.core.ctfadaptor;
15
16 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
17 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
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
24 * @version 1.0
25 * @since 2.0
26 */
27 public class CtfTmfContext implements ITmfContext {
28
29 // -------------------------------------------
30 // Fields
31 // -------------------------------------------
32
33 private CtfLocation fCurLocation;
34 private long fCurRank;
35
36 private final CtfTmfTrace fTrace;
37
38 // -------------------------------------------
39 // Constructor
40 // -------------------------------------------
41
42 /**
43 * Constructor
44 *
45 * @param ctfTmfTrace
46 * the parent trace
47 * @since 1.1
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 /**
64 * @since 3.0
65 */
66 @Override
67 public ITmfLocation getLocation() {
68 return fCurLocation;
69 }
70
71 @Override
72 public boolean hasValidRank() {
73 return fCurRank != CtfLocation.INVALID_LOCATION.getTimestamp();
74 }
75
76 /**
77 * @since 3.0
78 */
79 @Override
80 public void setLocation(ITmfLocation location) {
81 fCurLocation = (CtfLocation) location;
82 if (fCurLocation != null) {
83 getIterator().seek(fCurLocation.getLocationInfo());
84 }
85 }
86
87 @Override
88 public void setRank(long rank) {
89 fCurRank = rank;
90
91 }
92
93 @Override
94 public void increaseRank() {
95 if (hasValidRank()) {
96 fCurRank++;
97 }
98 }
99
100 // -------------------------------------------
101 // CtfTmfTrace Helpers
102 // -------------------------------------------
103
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
113 /**
114 * Gets the current event. Wrapper to help CtfTmfTrace
115 *
116 * @return The event or null
117 */
118 public synchronized CtfTmfEvent getCurrentEvent() {
119 return getIterator().getCurrentEvent();
120 }
121
122 /**
123 * Advances to a the next event. Wrapper to help CtfTmfTrace
124 *
125 * @return success or not
126 */
127 public synchronized boolean advance() {
128 final CtfLocationInfo curLocationData = fCurLocation.getLocationInfo();
129 boolean retVal = getIterator().advance();
130 CtfTmfEvent currentEvent = getIterator().getCurrentEvent();
131
132 if (currentEvent != null) {
133 final long timestampValue = currentEvent.getTimestamp().getValue();
134 if (curLocationData.getTimestamp() == timestampValue) {
135 fCurLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
136 } else {
137 fCurLocation = new CtfLocation(timestampValue, 0L);
138 }
139 } else {
140 fCurLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
141 }
142
143 return retVal;
144 }
145
146 @Override
147 public void dispose() {
148 CtfIteratorManager.removeIterator(fTrace, this);
149 }
150
151 /**
152 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
153 *
154 * @param timestamp
155 * desired timestamp
156 * @return success or not
157 */
158 public synchronized boolean seek(final long timestamp) {
159 fCurLocation = new CtfLocation(timestamp, 0);
160 return getIterator().seek(timestamp);
161 }
162
163 /**
164 * Seeks to a given location. Wrapper to help CtfTmfTrace
165 * @param location
166 * unique location to find the event.
167 *
168 * @return success or not
169 * @since 2.0
170 */
171 public synchronized boolean seek(final CtfLocationInfo location) {
172 fCurLocation = new CtfLocation(location);
173 return getIterator().seek(location);
174 }
175
176 @Override
177 public CtfTmfContext clone() {
178 CtfTmfContext ret = null;
179 try {
180 ret = (CtfTmfContext) super.clone();
181 /* Fields are immutable, no need to deep-copy them */
182 } catch (CloneNotSupportedException e) {
183 /* Should not happen, we're calling Object.clone() */
184 }
185 return ret;
186 }
187
188 // -------------------------------------------
189 // Private helpers
190 // -------------------------------------------
191
192 /**
193 * Get iterator, called every time to get an iterator, no local copy is
194 * stored so that there is no need to "update"
195 *
196 * @return an iterator
197 */
198 private CtfIterator getIterator() {
199 return CtfIteratorManager.getIterator(fTrace, this);
200 }
201 }
This page took 0.050139 seconds and 4 git commands to generate.