Refactor TmfTrace and dependencies - remove indexTrace()
[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 super(null, TmfEvent.class, path, cacheSize);
91 fTrace = new RandomAccessFile(path, "r");
92 fParser = new TmfEventParserStub();
93 }
94
95
96 /**
97 * @param path
98 * @param cacheSize
99 * @param waitForCompletion
100 * @param parser
101 * @throws FileNotFoundException
102 */
103 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, final ITmfEventParser<TmfEvent> parser) throws FileNotFoundException {
104 // super(null, TmfEvent.class, path, cacheSize, false);
105 super(null, TmfEvent.class, path, cacheSize);
106 fTrace = new RandomAccessFile(path, "r");
107 fParser = parser;
108 }
109
110 /**
111 * Copy constructor
112 */
113 public TmfTraceStub(final TmfTraceStub trace) throws FileNotFoundException {
114 // super(trace.getResource(), TmfEvent.class, trace.getPath(), trace.getIndexPageSize(), false);
115 super(trace.getResource(), TmfEvent.class, trace.getPath(), trace.getIndexPageSize());
116 fTrace = new RandomAccessFile(getPath(), "r");
117 fParser = new TmfEventParserStub();
118 // This is really not pretty (the object is not constructed yet...)
119 indexTrace(true);
120 }
121
122 // /**
123 // */
124 // @Override
125 // public TmfTraceStub clone() {
126 // TmfTraceStub clone = null;
127 // try {
128 // clone = (TmfTraceStub) super.clone();
129 // clone.fTrace = new RandomAccessFile(getPath(), "r");
130 // clone.fParser = new TmfEventParserStub();
131 // } catch (final FileNotFoundException e) {
132 // }
133 // return clone;
134 // }
135
136 // ------------------------------------------------------------------------
137 // Accessors
138 // ------------------------------------------------------------------------
139
140 public RandomAccessFile getStream() {
141 return fTrace;
142 }
143
144 // ------------------------------------------------------------------------
145 // Operators
146 // ------------------------------------------------------------------------
147
148 @Override
149 @SuppressWarnings("unchecked")
150 public TmfContext seekLocation(final ITmfLocation<?> location) {
151 try {
152 fLock.lock();
153 try {
154 if (fTrace != null) {
155 // Position the trace at the requested location and
156 // returns the corresponding context
157 long loc = 0;
158 long rank = 0;
159 if (location != null) {
160 loc = ((TmfLocation<Long>) location).getLocation();
161 rank = ITmfContext.UNKNOWN_RANK;
162 }
163 if (loc != fTrace.getFilePointer())
164 fTrace.seek(loc);
165 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
166 return context;
167 }
168 } catch (final IOException e) {
169 e.printStackTrace();
170 } catch (final NullPointerException e) {
171 e.printStackTrace();
172 }
173 } catch (final NullPointerException e) {
174 e.printStackTrace();
175 }
176 finally{
177 fLock.unlock();
178 }
179 return null;
180 }
181
182
183 @Override
184 public TmfContext seekLocation(final double ratio) {
185 fLock.lock();
186 try {
187 if (fTrace != null) {
188 final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
189 final TmfContext context = seekLocation(location);
190 context.setRank(ITmfContext.UNKNOWN_RANK);
191 return context;
192 }
193 } catch (final IOException e) {
194 e.printStackTrace();
195 } finally {
196 fLock.unlock();
197 }
198
199 return null;
200 }
201
202 @Override
203 @SuppressWarnings("rawtypes")
204 public double getLocationRatio(final ITmfLocation location) {
205 fLock.lock();
206 try {
207 if (fTrace != null)
208 if (location.getLocation() instanceof Long)
209 return (double) ((Long) location.getLocation()) / fTrace.length();
210 } catch (final IOException e) {
211 e.printStackTrace();
212 } finally {
213 fLock.unlock();
214 }
215 return 0;
216 }
217
218 @Override
219 public TmfLocation<Long> getCurrentLocation() {
220 fLock.lock();
221 try {
222 if (fTrace != null)
223 return new TmfLocation<Long>(fTrace.getFilePointer());
224 } catch (final IOException e) {
225 e.printStackTrace();
226 } finally {
227 fLock.unlock();
228 }
229 return null;
230 }
231
232 @Override
233 public ITmfEvent parseEvent(final ITmfContext context) {
234 fLock.lock();
235 try {
236 // parseNextEvent will update the context
237 if (fTrace != null) {
238 final ITmfEvent event = fParser.parseNextEvent(this, context.clone());
239 return event;
240 }
241 }
242 catch (final IOException e) {
243 e.printStackTrace();
244 } finally {
245 fLock.unlock();
246 }
247 return null;
248 }
249
250 @Override
251 public void setTimeRange(final TmfTimeRange range) {
252 super.setTimeRange(range);
253 }
254
255 @Override
256 public void setStartTime(final ITmfTimestamp startTime) {
257 super.setStartTime(startTime);
258 }
259
260 @Override
261 public void setEndTime(final ITmfTimestamp endTime) {
262 super.setEndTime(endTime);
263 }
264
265 @Override
266 public void dispose() {
267 fLock.lock();
268 try {
269 if (fTrace != null) {
270 fTrace.close();
271 fTrace = null;
272 }
273 } catch (final IOException e) {
274 // Ignore
275 } finally {
276 fLock.unlock();
277 }
278 super.dispose();
279 }
280
281 }
This page took 0.037398 seconds and 6 git commands to generate.