Refactor TmfTrace and dependencies - remove getTrace()
[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
d18dd09b
ASL
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
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
72f1e62a 20import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
dfee01ae 21import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
22import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
23import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
6c13869b
FC
24import org.eclipse.linuxtools.tmf.core.parser.ITmfEventParser;
25import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
26import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
6c13869b
FC
27import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
28import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
29import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
d18dd09b
ASL
30
31/**
32 * <b><u>TmfTraceStub</u></b>
33 * <p>
8d2e2848 34 * Dummy test trace. Use in conjunction with TmfEventParserStub.
d18dd09b 35 */
3b38ea61 36@SuppressWarnings("nls")
e31e01e8 37public class TmfTraceStub extends TmfTrace<TmfEvent> {
d18dd09b 38
e31e01e8 39 // ------------------------------------------------------------------------
d18dd09b 40 // Attributes
e31e01e8 41 // ------------------------------------------------------------------------
d18dd09b
ASL
42
43 // The actual stream
ff4ed569 44 private RandomAccessFile fTrace;
d18dd09b
ASL
45
46 // The associated event parser
f17b2f70 47 private final ITmfEventParser<TmfEvent> fParser;
d18dd09b 48
73005152 49 // The synchronization lock
085d898f
FC
50 private final ReentrantLock fLock = new ReentrantLock();
51
e31e01e8 52 // ------------------------------------------------------------------------
d18dd09b 53 // Constructors
e31e01e8 54 // ------------------------------------------------------------------------
d18dd09b
ASL
55
56 /**
3ef62bac 57 * @param path
d18dd09b
ASL
58 * @throws FileNotFoundException
59 */
085d898f 60 public TmfTraceStub(final String path) throws FileNotFoundException {
3ef62bac 61 this(path, DEFAULT_INDEX_PAGE_SIZE, false);
8d2e2848
FC
62 }
63
64 /**
3ef62bac 65 * @param path
e31e01e8 66 * @param cacheSize
8d2e2848
FC
67 * @throws FileNotFoundException
68 */
085d898f 69 public TmfTraceStub(final String path, final int cacheSize) throws FileNotFoundException {
3ef62bac 70 this(path, cacheSize, false);
d18dd09b
ASL
71 }
72
73 /**
3ef62bac 74 * @param path
ff4ed569 75 * @param waitForCompletion
d18dd09b
ASL
76 * @throws FileNotFoundException
77 */
085d898f 78 public TmfTraceStub(final String path, final boolean waitForCompletion) throws FileNotFoundException {
3ef62bac 79 this(path, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
8d2e2848 80 }
085d898f 81
8d2e2848 82 /**
3ef62bac 83 * @param path
8d2e2848 84 * @param cacheSize
ff4ed569 85 * @param waitForCompletion
8d2e2848
FC
86 * @throws FileNotFoundException
87 */
085d898f 88 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException {
3ef62bac
FC
89 super(null, TmfEvent.class, path, cacheSize, false);
90 fTrace = new RandomAccessFile(path, "r");
d18dd09b
ASL
91 fParser = new TmfEventParserStub();
92 }
93
085d898f 94
73005152 95 /**
3ef62bac 96 * @param path
73005152
BH
97 * @param cacheSize
98 * @param waitForCompletion
99 * @param parser
100 * @throws FileNotFoundException
101 */
085d898f 102 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, final ITmfEventParser<TmfEvent> parser) throws FileNotFoundException {
25e48683 103 super(null, TmfEvent.class, path, cacheSize, false);
3ef62bac 104 fTrace = new RandomAccessFile(path, "r");
73005152
BH
105 fParser = parser;
106 }
085d898f 107
ff4ed569 108 /**
f17b2f70 109 * Copy constructor
ff4ed569 110 */
f17b2f70 111 public TmfTraceStub(final TmfTraceStub trace) throws FileNotFoundException {
25e48683
FC
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...)
116 indexTrace(true);
ff4ed569 117 }
085d898f 118
f17b2f70
FC
119 // /**
120 // */
121 // @Override
122 // public TmfTraceStub clone() {
123 // TmfTraceStub clone = null;
124 // try {
125 // clone = (TmfTraceStub) super.clone();
126 // clone.fTrace = new RandomAccessFile(getPath(), "r");
127 // clone.fParser = new TmfEventParserStub();
128 // } catch (final FileNotFoundException e) {
129 // }
130 // return clone;
131 // }
132
e31e01e8 133 // ------------------------------------------------------------------------
d18dd09b 134 // Accessors
e31e01e8 135 // ------------------------------------------------------------------------
d18dd09b
ASL
136
137 public RandomAccessFile getStream() {
138 return fTrace;
139 }
140
e31e01e8 141 // ------------------------------------------------------------------------
d18dd09b 142 // Operators
e31e01e8 143 // ------------------------------------------------------------------------
d18dd09b 144
085d898f
FC
145 @Override
146 @SuppressWarnings("unchecked")
147 public TmfContext seekLocation(final ITmfLocation<?> location) {
148 fLock.lock();
d18dd09b 149 try {
73005152
BH
150 if (fTrace != null) {
151 // Position the trace at the requested location and
152 // returns the corresponding context
153 long loc = 0;
154 long rank = 0;
155 if (location != null) {
156 loc = ((TmfLocation<Long>) location).getLocation();
157 rank = ITmfContext.UNKNOWN_RANK;
158 }
085d898f 159 if (loc != fTrace.getFilePointer())
73005152 160 fTrace.seek(loc);
085d898f 161 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
73005152
BH
162 return context;
163 }
085d898f
FC
164 } catch (final IOException e) {
165 e.printStackTrace();
166 }
73005152
BH
167 finally{
168 fLock.unlock();
169 }
085d898f 170 return null;
d18dd09b
ASL
171 }
172
c76c54bb 173
085d898f
FC
174 @Override
175 public TmfContext seekLocation(final double ratio) {
176 fLock.lock();
c76c54bb 177 try {
73005152 178 if (fTrace != null) {
085d898f
FC
179 final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
180 final TmfContext context = seekLocation(location);
73005152
BH
181 context.setRank(ITmfContext.UNKNOWN_RANK);
182 return context;
183 }
085d898f 184 } catch (final IOException e) {
c76c54bb 185 e.printStackTrace();
73005152
BH
186 } finally {
187 fLock.unlock();
c76c54bb 188 }
085d898f 189
c76c54bb
FC
190 return null;
191 }
192
193 @Override
12c155f5 194 @SuppressWarnings("rawtypes")
085d898f 195 public double getLocationRatio(final ITmfLocation location) {
73005152 196 fLock.lock();
c76c54bb 197 try {
085d898f
FC
198 if (fTrace != null)
199 if (location.getLocation() instanceof Long)
73005152 200 return (double) ((Long) location.getLocation()) / fTrace.length();
085d898f 201 } catch (final IOException e) {
c76c54bb 202 e.printStackTrace();
73005152
BH
203 } finally {
204 fLock.unlock();
c76c54bb
FC
205 }
206 return 0;
207 }
208
4e3aa37d 209 @Override
085d898f 210 public TmfLocation<Long> getCurrentLocation() {
73005152 211 fLock.lock();
d18dd09b 212 try {
085d898f 213 if (fTrace != null)
73005152 214 return new TmfLocation<Long>(fTrace.getFilePointer());
085d898f
FC
215 } catch (final IOException e) {
216 e.printStackTrace();
217 } finally {
218 fLock.unlock();
219 }
220 return null;
221 }
222
223 @Override
224 public ITmfEvent parseEvent(final ITmfContext context) {
225 fLock.lock();
226 try {
227 // parseNextEvent will update the context
228 if (fTrace != null) {
229 final ITmfEvent event = fParser.parseNextEvent(this, context.clone());
230 return event;
73005152 231 }
085d898f
FC
232 }
233 catch (final IOException e) {
d18dd09b 234 e.printStackTrace();
73005152
BH
235 } finally {
236 fLock.unlock();
d18dd09b
ASL
237 }
238 return null;
239 }
240
085d898f
FC
241 @Override
242 public void setTimeRange(final TmfTimeRange range) {
243 super.setTimeRange(range);
244 }
d18dd09b 245
085d898f
FC
246 @Override
247 public void setStartTime(final ITmfTimestamp startTime) {
248 super.setStartTime(startTime);
ff4ed569
FC
249 }
250
085d898f
FC
251 @Override
252 public void setEndTime(final ITmfTimestamp endTime) {
253 super.setEndTime(endTime);
ff4ed569
FC
254 }
255
085d898f
FC
256 @Override
257 public void dispose() {
258 fLock.lock();
259 try {
260 if (fTrace != null) {
261 fTrace.close();
262 fTrace = null;
263 }
264 } catch (final IOException e) {
265 // Ignore
266 } finally {
267 fLock.unlock();
268 }
269 super.dispose();
ff4ed569
FC
270 }
271
e31e01e8 272}
This page took 0.049233 seconds and 5 git commands to generate.