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