tmf/ctf: Make CtfLocationInfo inline with code style
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfContext.java
CommitLineData
788ddcbc 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 Ericsson
788ddcbc
MK
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 *
f0414df8
SD
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Delisle - Remove the iterator in dispose()
788ddcbc
MK
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.core.ctfadaptor;
15
788ddcbc 16import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
a3db8436 17import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
788ddcbc
MK
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
81a2d02e
AM
24 * @version 1.0
25 * @since 2.0
788ddcbc 26 */
81a2d02e 27public class CtfTmfContext implements ITmfContext {
788ddcbc 28
788ddcbc
MK
29 // -------------------------------------------
30 // Fields
31 // -------------------------------------------
81a2d02e 32
788ddcbc
MK
33 private CtfLocation curLocation;
34 private long curRank;
788ddcbc 35
53b235e1 36 private final CtfTmfTrace fTrace;
788ddcbc
MK
37
38 // -------------------------------------------
39 // Constructor
40 // -------------------------------------------
53b235e1
MK
41
42 /**
81a2d02e 43 * Constructor
53b235e1
MK
44 *
45 * @param ctfTmfTrace
46 * the parent trace
47 * @since 1.1
48 */
81a2d02e 49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace) {
53b235e1 50 fTrace = ctfTmfTrace;
f5df94f8 51 curLocation = new CtfLocation(new CtfLocationInfo(0, 0));
788ddcbc
MK
52 }
53
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
57
58 @Override
59 public long getRank() {
60 return curRank;
61 }
62
a3db8436
AM
63 /**
64 * @since 3.0
65 */
788ddcbc 66 @Override
1e1bef82 67 public ITmfLocation getLocation() {
788ddcbc
MK
68 return curLocation;
69 }
70
71 @Override
72 public boolean hasValidRank() {
132a02b0 73 return curRank != CtfLocation.INVALID_LOCATION.getTimestamp();
788ddcbc
MK
74 }
75
a3db8436
AM
76 /**
77 * @since 3.0
78 */
788ddcbc 79 @Override
1e1bef82 80 public void setLocation(ITmfLocation location) {
788ddcbc 81 curLocation = (CtfLocation) location;
575beffc
PT
82 if (curLocation != null) {
83 getIterator().seek(curLocation.getLocationInfo());
84 }
788ddcbc
MK
85 }
86
87 @Override
88 public void setRank(long rank) {
89 curRank = rank;
90
91 }
92
93 @Override
94 public void increaseRank() {
95 if (hasValidRank()) {
96 curRank++;
97 }
98 }
99
100 // -------------------------------------------
101 // CtfTmfTrace Helpers
102 // -------------------------------------------
103
81a2d02e
AM
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
788ddcbc
MK
113 /**
114 * Gets the current event. Wrapper to help CtfTmfTrace
53b235e1 115 *
788ddcbc
MK
116 * @return The event or null
117 */
118 public synchronized CtfTmfEvent getCurrentEvent() {
53b235e1 119 return getIterator().getCurrentEvent();
788ddcbc
MK
120 }
121
122 /**
123 * Advances to a the next event. Wrapper to help CtfTmfTrace
53b235e1 124 *
788ddcbc
MK
125 * @return success or not
126 */
127 public synchronized boolean advance() {
f5df94f8 128 final CtfLocationInfo curLocationData = this.curLocation.getLocationInfo();
53b235e1
MK
129 boolean retVal = getIterator().advance();
130 CtfTmfEvent currentEvent = getIterator().getCurrentEvent();
132a02b0 131
788ddcbc 132 if (currentEvent != null) {
58f3bc52 133 final long timestampValue = currentEvent.getTimestamp().getValue();
132a02b0 134 if (curLocationData.getTimestamp() == timestampValue) {
d62bb185 135 curLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
132a02b0 136 } else {
d62bb185 137 curLocation = new CtfLocation(timestampValue, 0L);
132a02b0 138 }
788ddcbc 139 } else {
d62bb185 140 curLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
788ddcbc
MK
141 }
142
143 return retVal;
144 }
145
146 @Override
147 public void dispose() {
f0414df8 148 CtfIteratorManager.removeIterator(fTrace, this);
788ddcbc
MK
149 }
150
151 /**
152 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
53b235e1
MK
153 *
154 * @param timestamp
155 * desired timestamp
788ddcbc
MK
156 * @return success or not
157 */
158 public synchronized boolean seek(final long timestamp) {
d62bb185 159 curLocation = new CtfLocation(timestamp, 0);
53b235e1 160 return getIterator().seek(timestamp);
788ddcbc
MK
161 }
162
132a02b0
MK
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 */
f5df94f8 171 public synchronized boolean seek(final CtfLocationInfo location) {
d62bb185 172 curLocation = new CtfLocation(location);
132a02b0
MK
173 return getIterator().seek(location);
174 }
175
788ddcbc 176 @Override
81a2d02e
AM
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 }
788ddcbc
MK
185 return ret;
186 }
187
188 // -------------------------------------------
189 // Private helpers
190 // -------------------------------------------
81a2d02e 191
788ddcbc 192 /**
53b235e1
MK
193 * Get iterator, called every time to get an iterator, no local copy is
194 * stored so that there is no need to "update"
788ddcbc 195 *
53b235e1 196 * @return an iterator
788ddcbc
MK
197 */
198 private CtfIterator getIterator() {
53b235e1 199 return CtfIteratorManager.getIterator(fTrace, this);
788ddcbc 200 }
788ddcbc 201}
This page took 0.048938 seconds and 5 git commands to generate.