Ctf: add removeIterator() to prevent OutOfMemory exception (Bug 414998)
[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.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 curLocation;
34 private long curRank;
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 curLocation = new CtfLocation(new CtfLocationInfo(0, 0));
52 }
53
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
57
58 @Override
59 public long getRank() {
60 return curRank;
61 }
62
63 @Override
64 public ITmfLocation getLocation() {
65 return curLocation;
66 }
67
68 @Override
69 public boolean hasValidRank() {
70 return curRank != CtfLocation.INVALID_LOCATION.getTimestamp();
71 }
72
73 @Override
74 public void setLocation(ITmfLocation location) {
75 curLocation = (CtfLocation) location;
76 if (curLocation != null) {
77 getIterator().seek(curLocation.getLocationInfo());
78 }
79 }
80
81 @Override
82 public void setRank(long rank) {
83 curRank = rank;
84
85 }
86
87 @Override
88 public void increaseRank() {
89 if (hasValidRank()) {
90 curRank++;
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 = this.curLocation.getLocationInfo();
123 boolean retVal = getIterator().advance();
124 CtfTmfEvent currentEvent = getIterator().getCurrentEvent();
125
126 if (currentEvent != null) {
127 final long timestampValue = currentEvent.getTimestamp().getValue();
128 if (curLocationData.getTimestamp() == timestampValue) {
129 curLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
130 } else {
131 curLocation = new CtfLocation(timestampValue, 0L);
132 }
133 } else {
134 curLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
135 }
136
137 return retVal;
138 }
139
140 @Override
141 public void dispose() {
142 CtfIteratorManager.removeIterator(fTrace, this);
143 }
144
145 /**
146 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
147 *
148 * @param timestamp
149 * desired timestamp
150 * @return success or not
151 */
152 public synchronized boolean seek(final long timestamp) {
153 curLocation = new CtfLocation(timestamp, 0);
154 return getIterator().seek(timestamp);
155 }
156
157 /**
158 * Seeks to a given location. Wrapper to help CtfTmfTrace
159 * @param location
160 * unique location to find the event.
161 *
162 * @return success or not
163 * @since 2.0
164 */
165 public synchronized boolean seek(final CtfLocationInfo location) {
166 curLocation = new CtfLocation(location);
167 return getIterator().seek(location);
168 }
169
170 @Override
171 public CtfTmfContext clone() {
172 CtfTmfContext ret = null;
173 try {
174 ret = (CtfTmfContext) super.clone();
175 /* Fields are immutable, no need to deep-copy them */
176 } catch (CloneNotSupportedException e) {
177 /* Should not happen, we're calling Object.clone() */
178 }
179 return ret;
180 }
181
182 // -------------------------------------------
183 // Private helpers
184 // -------------------------------------------
185
186 /**
187 * Get iterator, called every time to get an iterator, no local copy is
188 * stored so that there is no need to "update"
189 *
190 * @return an iterator
191 */
192 private CtfIterator getIterator() {
193 return CtfIteratorManager.getIterator(fTrace, this);
194 }
195 }
This page took 0.03511 seconds and 5 git commands to generate.