1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
;
15 import java
.io
.FileNotFoundException
;
16 import java
.io
.IOException
;
17 import java
.io
.RandomAccessFile
;
18 import java
.util
.concurrent
.locks
.ReentrantLock
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.parser
.ITmfEventParser
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
32 * <b><u>TmfTraceStub</u></b>
34 * Dummy test trace. Use in conjunction with TmfEventParserStub.
36 @SuppressWarnings("nls")
37 public class TmfTraceStub
extends TmfTrace
<TmfEvent
> {
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 private RandomAccessFile fTrace
;
46 // The associated event parser
47 private final ITmfEventParser
<TmfEvent
> fParser
;
49 // The synchronization lock
50 private final ReentrantLock fLock
= new ReentrantLock();
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
58 * @throws FileNotFoundException
60 public TmfTraceStub(final String path
) throws FileNotFoundException
{
61 this(path
, DEFAULT_INDEX_PAGE_SIZE
, false);
67 * @throws FileNotFoundException
69 public TmfTraceStub(final String path
, final int cacheSize
) throws FileNotFoundException
{
70 this(path
, cacheSize
, false);
75 * @param waitForCompletion
76 * @throws FileNotFoundException
78 public TmfTraceStub(final String path
, final boolean waitForCompletion
) throws FileNotFoundException
{
79 this(path
, DEFAULT_INDEX_PAGE_SIZE
, waitForCompletion
);
85 * @param waitForCompletion
86 * @throws FileNotFoundException
88 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
) throws FileNotFoundException
{
89 super(null, TmfEvent
.class, path
, cacheSize
, false);
90 fTrace
= new RandomAccessFile(path
, "r");
91 fParser
= new TmfEventParserStub();
98 * @param waitForCompletion
100 * @throws FileNotFoundException
102 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
, final ITmfEventParser
<TmfEvent
> parser
) throws FileNotFoundException
{
103 super(null, TmfEvent
.class, path
, cacheSize
, false);
104 fTrace
= new RandomAccessFile(path
, "r");
111 public TmfTraceStub(final TmfTraceStub trace
) throws FileNotFoundException
{
112 super(trace
.getResource(), TmfEvent
.class, trace
.getPath(), trace
.getIndexPageSize(), false);
113 fTrace
= new RandomAccessFile(getPath(), "r");
114 fParser
= new TmfEventParserStub();
115 // This is really not pretty (the object is not constructed yet...)
122 // public TmfTraceStub clone() {
123 // TmfTraceStub clone = null;
125 // clone = (TmfTraceStub) super.clone();
126 // clone.fTrace = new RandomAccessFile(getPath(), "r");
127 // clone.fParser = new TmfEventParserStub();
128 // } catch (final FileNotFoundException e) {
133 // ------------------------------------------------------------------------
135 // ------------------------------------------------------------------------
137 public RandomAccessFile
getStream() {
141 // ------------------------------------------------------------------------
143 // ------------------------------------------------------------------------
146 @SuppressWarnings("unchecked")
147 public TmfContext
seekLocation(final ITmfLocation
<?
> location
) {
150 if (fTrace
!= null) {
151 // Position the trace at the requested location and
152 // returns the corresponding context
155 if (location
!= null) {
156 loc
= ((TmfLocation
<Long
>) location
).getLocation();
157 rank
= ITmfContext
.UNKNOWN_RANK
;
159 if (loc
!= fTrace
.getFilePointer())
161 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
164 } catch (final IOException e
) {
175 public TmfContext
seekLocation(final double ratio
) {
178 if (fTrace
!= null) {
179 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(Long
.valueOf((long) (ratio
* fTrace
.length())));
180 final TmfContext context
= seekLocation(location
);
181 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
184 } catch (final IOException e
) {
194 @SuppressWarnings("rawtypes")
195 public double getLocationRatio(final ITmfLocation location
) {
199 if (location
.getLocation() instanceof Long
)
200 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
201 } catch (final IOException e
) {
210 public TmfLocation
<Long
> getCurrentLocation() {
214 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
215 } catch (final IOException e
) {
224 public ITmfEvent
parseEvent(final ITmfContext context
) {
227 // parseNextEvent will update the context
228 if (fTrace
!= null) {
229 final ITmfEvent event
= fParser
.parseNextEvent(this, context
.clone());
233 catch (final IOException e
) {
242 public void setTimeRange(final TmfTimeRange range
) {
243 super.setTimeRange(range
);
247 public void setStartTime(final ITmfTimestamp startTime
) {
248 super.setStartTime(startTime
);
252 public void setEndTime(final ITmfTimestamp endTime
) {
253 super.setEndTime(endTime
);
257 public void dispose() {
260 if (fTrace
!= null) {
264 } catch (final IOException e
) {
This page took 0.038172 seconds and 6 git commands to generate.