b1ef6d79559a2f8479c37527276aafb2570815e9
[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.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 @Override
62 public ITmfLocation getLocation() {
63 return curLocation;
64 }
65
66 @Override
67 public boolean hasValidRank() {
68 return curRank != CtfLocation.INVALID_LOCATION.getTimestamp();
69 }
70
71 @Override
72 public void setLocation(ITmfLocation location) {
73 curLocation = (CtfLocation) location;
74 if (curLocation != null) {
75 getIterator().seek(curLocation.getLocationInfo());
76 }
77 }
78
79 @Override
80 public void setRank(long rank) {
81 curRank = rank;
82
83 }
84
85 @Override
86 public void increaseRank() {
87 if (hasValidRank()) {
88 curRank++;
89 }
90 }
91
92 // -------------------------------------------
93 // CtfTmfTrace Helpers
94 // -------------------------------------------
95
96 /**
97 * Gets the trace of this context.
98 *
99 * @return The trace of this context
100 */
101 public CtfTmfTrace getTrace() {
102 return fTrace;
103 }
104
105 /**
106 * Gets the current event. Wrapper to help CtfTmfTrace
107 *
108 * @return The event or null
109 */
110 public synchronized CtfTmfEvent getCurrentEvent() {
111 return getIterator().getCurrentEvent();
112 }
113
114 /**
115 * Advances to a the next event. Wrapper to help CtfTmfTrace
116 *
117 * @return success or not
118 */
119 public synchronized boolean advance() {
120 final CtfLocationInfo curLocationData = this.curLocation.getLocationInfo();
121 boolean retVal = getIterator().advance();
122 CtfTmfEvent currentEvent = getIterator().getCurrentEvent();
123
124 if (currentEvent != null) {
125 final long timestampValue = currentEvent.getTimestamp().getValue();
126 if (curLocationData.getTimestamp() == timestampValue) {
127 curLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
128 } else {
129 curLocation = new CtfLocation(timestampValue, 0L);
130 }
131 } else {
132 curLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
133 }
134
135 return retVal;
136 }
137
138 @Override
139 public void dispose() {
140 // do nothing
141 }
142
143 /**
144 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
145 *
146 * @param timestamp
147 * desired timestamp
148 * @return success or not
149 */
150 public synchronized boolean seek(final long timestamp) {
151 curLocation = new CtfLocation(timestamp, 0);
152 return getIterator().seek(timestamp);
153 }
154
155 /**
156 * Seeks to a given location. Wrapper to help CtfTmfTrace
157 * @param location
158 * unique location to find the event.
159 *
160 * @return success or not
161 * @since 2.0
162 */
163 public synchronized boolean seek(final CtfLocationInfo location) {
164 curLocation = new CtfLocation(location);
165 return getIterator().seek(location);
166 }
167
168 @Override
169 public CtfTmfContext clone() {
170 CtfTmfContext ret = null;
171 try {
172 ret = (CtfTmfContext) super.clone();
173 /* Fields are immutable, no need to deep-copy them */
174 } catch (CloneNotSupportedException e) {
175 /* Should not happen, we're calling Object.clone() */
176 }
177 return ret;
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 CtfIteratorManager.getIterator(fTrace, this);
192 }
193 }
This page took 0.035961 seconds and 4 git commands to generate.