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
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.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;
30
31 /**
32 * <b><u>TmfTraceStub</u></b>
33 * <p>
34 * Dummy test trace. Use in conjunction with TmfEventParserStub.
35 */
36 @SuppressWarnings("nls")
37 public class TmfTraceStub extends TmfTrace<TmfEvent> {
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 // The actual stream
44 private RandomAccessFile fTrace;
45
46 // The associated event parser
47 private final ITmfEventParser<TmfEvent> fParser;
48
49 // The synchronization lock
50 private final ReentrantLock fLock = new ReentrantLock();
51
52 // ------------------------------------------------------------------------
53 // Constructors
54 // ------------------------------------------------------------------------
55
56 /**
57 * @param path
58 * @throws FileNotFoundException
59 */
60 public TmfTraceStub(final String path) throws FileNotFoundException {
61 this(path, DEFAULT_INDEX_PAGE_SIZE, false);
62 }
63
64 /**
65 * @param path
66 * @param cacheSize
67 * @throws FileNotFoundException
68 */
69 public TmfTraceStub(final String path, final int cacheSize) throws FileNotFoundException {
70 this(path, cacheSize, false);
71 }
72
73 /**
74 * @param path
75 * @param waitForCompletion
76 * @throws FileNotFoundException
77 */
78 public TmfTraceStub(final String path, final boolean waitForCompletion) throws FileNotFoundException {
79 this(path, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
80 }
81
82 /**
83 * @param path
84 * @param cacheSize
85 * @param waitForCompletion
86 * @throws FileNotFoundException
87 */
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();
92 }
93
94
95 /**
96 * @param path
97 * @param cacheSize
98 * @param waitForCompletion
99 * @param parser
100 * @throws FileNotFoundException
101 */
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");
105 fParser = parser;
106 }
107
108 /**
109 * Copy constructor
110 */
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...)
116 indexTrace(true);
117 }
118
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
133 // ------------------------------------------------------------------------
134 // Accessors
135 // ------------------------------------------------------------------------
136
137 public RandomAccessFile getStream() {
138 return fTrace;
139 }
140
141 // ------------------------------------------------------------------------
142 // Operators
143 // ------------------------------------------------------------------------
144
145 @Override
146 @SuppressWarnings("unchecked")
147 public TmfContext seekLocation(final ITmfLocation<?> location) {
148 fLock.lock();
149 try {
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 }
159 if (loc != fTrace.getFilePointer())
160 fTrace.seek(loc);
161 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
162 return context;
163 }
164 } catch (final IOException e) {
165 e.printStackTrace();
166 }
167 finally{
168 fLock.unlock();
169 }
170 return null;
171 }
172
173
174 @Override
175 public TmfContext seekLocation(final double ratio) {
176 fLock.lock();
177 try {
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);
182 return context;
183 }
184 } catch (final IOException e) {
185 e.printStackTrace();
186 } finally {
187 fLock.unlock();
188 }
189
190 return null;
191 }
192
193 @Override
194 @SuppressWarnings("rawtypes")
195 public double getLocationRatio(final ITmfLocation location) {
196 fLock.lock();
197 try {
198 if (fTrace != null)
199 if (location.getLocation() instanceof Long)
200 return (double) ((Long) location.getLocation()) / fTrace.length();
201 } catch (final IOException e) {
202 e.printStackTrace();
203 } finally {
204 fLock.unlock();
205 }
206 return 0;
207 }
208
209 @Override
210 public TmfLocation<Long> getCurrentLocation() {
211 fLock.lock();
212 try {
213 if (fTrace != null)
214 return new TmfLocation<Long>(fTrace.getFilePointer());
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;
231 }
232 }
233 catch (final IOException e) {
234 e.printStackTrace();
235 } finally {
236 fLock.unlock();
237 }
238 return null;
239 }
240
241 @Override
242 public void setTimeRange(final TmfTimeRange range) {
243 super.setTimeRange(range);
244 }
245
246 @Override
247 public void setStartTime(final ITmfTimestamp startTime) {
248 super.setStartTime(startTime);
249 }
250
251 @Override
252 public void setEndTime(final ITmfTimestamp endTime) {
253 super.setEndTime(endTime);
254 }
255
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();
270 }
271
272 }
This page took 0.038172 seconds and 6 git commands to generate.