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