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