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