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