Fix for bug 391277: Rounding error in seekEvent by ratio.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / TmfTraceStub.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.tests.stubs.trace;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.RandomAccessFile;
18 import java.util.concurrent.locks.ReentrantLock;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
24 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer;
31 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
34
35 /**
36 * <b><u>TmfTraceStub</u></b>
37 * <p>
38 * Dummy test trace. Use in conjunction with TmfEventParserStub.
39 */
40 @SuppressWarnings({"nls","javadoc"})
41 public class TmfTraceStub extends TmfTrace<TmfEvent> implements ITmfEventParser<TmfEvent> {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 // The actual stream
48 private RandomAccessFile fTrace;
49
50 // // The associated event parser
51 // private ITmfEventParser<TmfEvent> fParser;
52
53 // The synchronization lock
54 private final ReentrantLock fLock = new ReentrantLock();
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 public TmfTraceStub() {
61 super();
62 setParser(new TmfEventParserStub(this));
63 }
64
65 /**
66 * @param path
67 * @throws FileNotFoundException
68 */
69 public TmfTraceStub(final String path) throws TmfTraceException {
70 this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false);
71 }
72
73 /**
74 * @param path
75 * @param cacheSize
76 * @throws FileNotFoundException
77 */
78 public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
79 this(path, cacheSize, false);
80 }
81
82 /**
83 * @param path
84 * @param cacheSize
85 * @throws FileNotFoundException
86 */
87 public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
88 super(null, TmfEvent.class, path, cacheSize, interval);
89 try {
90 fTrace = new RandomAccessFile(path, "r");
91 } catch (FileNotFoundException e) {
92 throw new TmfTraceException(e.getMessage());
93 }
94 setParser(new TmfEventParserStub(this));
95 }
96
97 /**
98 * @param path
99 * @param cacheSize
100 * @throws FileNotFoundException
101 */
102 public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
103 this(path, cacheSize, false, null, indexer);
104 }
105
106 /**
107 * @param path
108 * @param waitForCompletion
109 * @throws FileNotFoundException
110 */
111 public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
112 this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
113 }
114
115 /**
116 * @param path
117 * @param cacheSize
118 * @param waitForCompletion
119 * @throws FileNotFoundException
120 */
121 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
122 super(null, TmfEvent.class, path, cacheSize);
123 try {
124 fTrace = new RandomAccessFile(path, "r");
125 } catch (FileNotFoundException e) {
126 throw new TmfTraceException(e.getMessage());
127 }
128 setParser(new TmfEventParserStub(this));
129 if (waitForCompletion) {
130 indexTrace();
131 }
132 }
133
134 /**
135 * @param path
136 * @param cacheSize
137 * @param waitForCompletion
138 * @throws FileNotFoundException
139 */
140 public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
141 super(resource, TmfEvent.class, path, cacheSize);
142 try {
143 fTrace = new RandomAccessFile(path, "r");
144 } catch (FileNotFoundException e) {
145 throw new TmfTraceException(e.getMessage());
146 }
147 setParser(new TmfEventParserStub(this));
148 }
149
150 /**
151 * @param path
152 * @param cacheSize
153 * @param waitForCompletion
154 * @param parser
155 * @throws FileNotFoundException
156 */
157 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
158 final ITmfEventParser<TmfEvent> parser, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
159 super(null, TmfEvent.class, path, cacheSize, 0, indexer);
160 try {
161 fTrace = new RandomAccessFile(path, "r");
162 } catch (FileNotFoundException e) {
163 throw new TmfTraceException(e.getMessage());
164 }
165 setParser((parser != null) ? parser : new TmfEventParserStub(this));
166 }
167
168 /**
169 * Copy constructor
170 */
171 public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
172 super(trace);
173 try {
174 fTrace = new RandomAccessFile(getPath(), "r");
175 } catch (FileNotFoundException e) {
176 throw new TmfTraceException(e.getMessage());
177 }
178 setParser(new TmfEventParserStub(this));
179 }
180
181 public void indexTrace() {
182 indexTrace(true);
183 }
184
185 @Override
186 public void initTrace(final IResource resource, final String path, final Class<TmfEvent> type) throws TmfTraceException {
187 try {
188 fTrace = new RandomAccessFile(path, "r");
189 } catch (FileNotFoundException e) {
190 throw new TmfTraceException(e.getMessage());
191 }
192 setParser(new TmfEventParserStub(this));
193 super.initTrace(resource, path, type);
194 }
195
196 @Override
197 public void initialize(final IResource resource, final String path, final Class<TmfEvent> type) throws TmfTraceException {
198 super.initialize(resource, path, type);
199 }
200
201 // ------------------------------------------------------------------------
202 // Accessors
203 // ------------------------------------------------------------------------
204
205 public RandomAccessFile getStream() {
206 return fTrace;
207 }
208
209 // ------------------------------------------------------------------------
210 // Operators
211 // ------------------------------------------------------------------------
212
213 @Override
214 @SuppressWarnings("unchecked")
215 public TmfContext seekEvent(final ITmfLocation<?> location) {
216 try {
217 fLock.lock();
218 try {
219 if (fTrace != null) {
220 // Position the trace at the requested location and
221 // returns the corresponding context
222 long loc = 0;
223 long rank = 0;
224 if (location != null) {
225 loc = ((TmfLocation<Long>) location).getLocation();
226 rank = ITmfContext.UNKNOWN_RANK;
227 }
228 if (loc != fTrace.getFilePointer()) {
229 fTrace.seek(loc);
230 }
231 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
232 return context;
233 }
234 } catch (final IOException e) {
235 e.printStackTrace();
236 } catch (final NullPointerException e) {
237 e.printStackTrace();
238 }
239 finally{
240 fLock.unlock();
241 }
242 } catch (final NullPointerException e) {
243 e.printStackTrace();
244 }
245 return null;
246 }
247
248
249 @Override
250 public TmfContext seekEvent(final double ratio) {
251 fLock.lock();
252 try {
253 if (fTrace != null) {
254 final ITmfLocation<?> location = new TmfLocation<Long>(Math.round(ratio * fTrace.length()));
255 final TmfContext context = seekEvent(location);
256 context.setRank(ITmfContext.UNKNOWN_RANK);
257 return context;
258 }
259 } catch (final IOException e) {
260 e.printStackTrace();
261 } finally {
262 fLock.unlock();
263 }
264
265 return null;
266 }
267
268 @Override
269 public double getLocationRatio(ITmfLocation<?> location) {
270 fLock.lock();
271 try {
272 if (fTrace != null) {
273 if (location.getLocation() instanceof Long) {
274 return (double) ((Long) location.getLocation()) / fTrace.length();
275 }
276 }
277 } catch (final IOException e) {
278 e.printStackTrace();
279 } finally {
280 fLock.unlock();
281 }
282 return 0;
283 }
284
285 @Override
286 public TmfLocation<Long> getCurrentLocation() {
287 fLock.lock();
288 try {
289 if (fTrace != null) {
290 return new TmfLocation<Long>(fTrace.getFilePointer());
291 }
292 } catch (final IOException e) {
293 e.printStackTrace();
294 } finally {
295 fLock.unlock();
296 }
297 return null;
298 }
299
300 @Override
301 public TmfEvent parseEvent(final ITmfContext context) {
302 fLock.lock();
303 try {
304 // parseNextEvent will update the context
305 if (fTrace != null && getParser() != null && context != null) {
306 final TmfEvent event = getParser().parseEvent(context.clone());
307 return event;
308 }
309 } finally {
310 fLock.unlock();
311 }
312 return null;
313 }
314
315 @Override
316 public synchronized void setNbEvents(final long nbEvents) {
317 super.setNbEvents(nbEvents);
318 }
319
320 @Override
321 public void setTimeRange(final TmfTimeRange range) {
322 super.setTimeRange(range);
323 }
324
325 @Override
326 public void setStartTime(final ITmfTimestamp startTime) {
327 super.setStartTime(startTime);
328 }
329
330 @Override
331 public void setEndTime(final ITmfTimestamp endTime) {
332 super.setEndTime(endTime);
333 }
334
335 @Override
336 public void setStreamingInterval(final long interval) {
337 super.setStreamingInterval(interval);
338 }
339
340 @Override
341 public synchronized void dispose() {
342 fLock.lock();
343 try {
344 if (fTrace != null) {
345 fTrace.close();
346 fTrace = null;
347 }
348 } catch (final IOException e) {
349 // Ignore
350 } finally {
351 fLock.unlock();
352 }
353 super.dispose();
354 }
355
356 /* (non-Javadoc)
357 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
358 */
359 @Override
360 public boolean validate(IProject project, String path) {
361 return fileExists(path);
362 }
363
364 }
This page took 0.040931 seconds and 6 git commands to generate.