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