Fix Sonar findings in TmfEvent
[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
20658947 20import org.eclipse.core.resources.IResource;
72f1e62a 21import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
dfee01ae 22import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
23import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
24import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
b4f71e4a 25import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
6c13869b 26import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
7e6347b0 27import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
6c13869b 28import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
20658947 29import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer;
6c13869b
FC
30import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
31import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
32import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
d18dd09b
ASL
33
34/**
35 * <b><u>TmfTraceStub</u></b>
36 * <p>
8d2e2848 37 * Dummy test trace. Use in conjunction with TmfEventParserStub.
d18dd09b 38 */
3b38ea61 39@SuppressWarnings("nls")
7e6347b0 40public class TmfTraceStub extends TmfTrace<TmfEvent> implements ITmfEventParser<TmfEvent> {
d18dd09b 41
e31e01e8 42 // ------------------------------------------------------------------------
d18dd09b 43 // Attributes
e31e01e8 44 // ------------------------------------------------------------------------
d18dd09b
ASL
45
46 // The actual stream
ff4ed569 47 private RandomAccessFile fTrace;
d18dd09b 48
7e6347b0
FC
49// // The associated event parser
50// private ITmfEventParser<TmfEvent> fParser;
d18dd09b 51
73005152 52 // The synchronization lock
085d898f
FC
53 private final ReentrantLock fLock = new ReentrantLock();
54
e31e01e8 55 // ------------------------------------------------------------------------
d18dd09b 56 // Constructors
e31e01e8 57 // ------------------------------------------------------------------------
d18dd09b 58
20658947
FC
59 /**
60 * @param path
61 * @throws FileNotFoundException
62 */
63 public TmfTraceStub() {
64 super();
7e6347b0 65 fParser = new TmfEventParserStub(this);
20658947
FC
66 }
67
d18dd09b 68 /**
3ef62bac 69 * @param path
d18dd09b
ASL
70 * @throws FileNotFoundException
71 */
b4f71e4a 72 public TmfTraceStub(final String path) throws TmfTraceException {
20658947 73 this(path, DEFAULT_TRACE_CACHE_SIZE, false);
8d2e2848
FC
74 }
75
76 /**
3ef62bac 77 * @param path
e31e01e8 78 * @param cacheSize
8d2e2848
FC
79 * @throws FileNotFoundException
80 */
b4f71e4a 81 public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
3ef62bac 82 this(path, cacheSize, false);
d18dd09b
ASL
83 }
84
1703b536
FC
85 /**
86 * @param path
87 * @param cacheSize
88 * @throws FileNotFoundException
89 */
b4f71e4a 90 public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
1703b536 91 super(null, TmfEvent.class, path, cacheSize, interval);
b4f71e4a
FC
92 try {
93 fTrace = new RandomAccessFile(path, "r");
94 } catch (FileNotFoundException e) {
95 throw new TmfTraceException(e.getMessage());
96 }
7e6347b0 97 fParser = new TmfEventParserStub(this);
1703b536
FC
98 }
99
20658947
FC
100 /**
101 * @param path
102 * @param cacheSize
103 * @throws FileNotFoundException
104 */
b4f71e4a 105 public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
20658947
FC
106 this(path, cacheSize, false, null, indexer);
107 }
108
d18dd09b 109 /**
3ef62bac 110 * @param path
ff4ed569 111 * @param waitForCompletion
d18dd09b
ASL
112 * @throws FileNotFoundException
113 */
b4f71e4a 114 public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
20658947 115 this(path, DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
8d2e2848 116 }
085d898f 117
8d2e2848 118 /**
3ef62bac 119 * @param path
8d2e2848 120 * @param cacheSize
ff4ed569 121 * @param waitForCompletion
8d2e2848
FC
122 * @throws FileNotFoundException
123 */
b4f71e4a 124 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
09e86496 125 super(null, TmfEvent.class, path, cacheSize);
b4f71e4a
FC
126 try {
127 fTrace = new RandomAccessFile(path, "r");
128 } catch (FileNotFoundException e) {
129 throw new TmfTraceException(e.getMessage());
130 }
7e6347b0 131 fParser = new TmfEventParserStub(this);
d18dd09b
ASL
132 }
133
20658947
FC
134 /**
135 * @param path
136 * @param cacheSize
137 * @param waitForCompletion
138 * @throws FileNotFoundException
139 */
b4f71e4a 140 public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
20658947 141 super(resource, TmfEvent.class, path, cacheSize);
b4f71e4a
FC
142 try {
143 fTrace = new RandomAccessFile(path, "r");
144 } catch (FileNotFoundException e) {
145 throw new TmfTraceException(e.getMessage());
146 }
7e6347b0 147 fParser = new TmfEventParserStub(this);
20658947 148 }
085d898f 149
73005152 150 /**
3ef62bac 151 * @param path
73005152
BH
152 * @param cacheSize
153 * @param waitForCompletion
154 * @param parser
155 * @throws FileNotFoundException
156 */
7e6347b0 157 @SuppressWarnings("unchecked")
20658947 158 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
b4f71e4a 159 final ITmfEventParser<TmfEvent> parser, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
20658947 160 super(null, TmfEvent.class, path, cacheSize, 0, indexer);
b4f71e4a
FC
161 try {
162 fTrace = new RandomAccessFile(path, "r");
163 } catch (FileNotFoundException e) {
164 throw new TmfTraceException(e.getMessage());
165 }
7e6347b0 166 fParser = (ITmfEventParser<ITmfEvent>) ((parser != null) ? parser : new TmfEventParserStub(this));
73005152 167 }
085d898f 168
ff4ed569 169 /**
f17b2f70 170 * Copy constructor
ff4ed569 171 */
b4f71e4a 172 public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
20658947 173 super(trace);
b4f71e4a
FC
174 try {
175 fTrace = new RandomAccessFile(getPath(), "r");
176 } catch (FileNotFoundException e) {
177 throw new TmfTraceException(e.getMessage());
178 }
7e6347b0 179 fParser = new TmfEventParserStub(this);
ff4ed569 180 }
085d898f 181
20658947
FC
182 public void indexTrace() {
183 fIndexer.buildIndex(true);
184 }
185
186 @Override
b4f71e4a
FC
187 public void initTrace(final IResource resource, final String path, final Class<TmfEvent> type) throws TmfTraceException {
188 try {
189 fTrace = new RandomAccessFile(path, "r");
190 } catch (FileNotFoundException e) {
191 throw new TmfTraceException(e.getMessage());
192 }
7e6347b0 193 fParser = new TmfEventParserStub(this);
20658947
FC
194 super.initTrace(resource, path, type);
195 }
f17b2f70 196
1703b536 197 @Override
b4f71e4a 198 public void initialize(final IResource resource, final String path, final Class<TmfEvent> type) throws TmfTraceException {
1703b536
FC
199 super.initialize(resource, path, type);
200 }
201
e31e01e8 202 // ------------------------------------------------------------------------
d18dd09b 203 // Accessors
e31e01e8 204 // ------------------------------------------------------------------------
d18dd09b
ASL
205
206 public RandomAccessFile getStream() {
207 return fTrace;
208 }
209
e31e01e8 210 // ------------------------------------------------------------------------
d18dd09b 211 // Operators
e31e01e8 212 // ------------------------------------------------------------------------
d18dd09b 213
085d898f
FC
214 @Override
215 @SuppressWarnings("unchecked")
7e6347b0 216 public TmfContext seekEvent(final ITmfLocation<?> location) {
d18dd09b 217 try {
09e86496
FC
218 fLock.lock();
219 try {
220 if (fTrace != null) {
221 // Position the trace at the requested location and
222 // returns the corresponding context
223 long loc = 0;
224 long rank = 0;
225 if (location != null) {
226 loc = ((TmfLocation<Long>) location).getLocation();
227 rank = ITmfContext.UNKNOWN_RANK;
228 }
20658947 229 if (loc != fTrace.getFilePointer()) {
09e86496 230 fTrace.seek(loc);
20658947 231 }
09e86496
FC
232 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
233 return context;
73005152 234 }
09e86496
FC
235 } catch (final IOException e) {
236 e.printStackTrace();
237 } catch (final NullPointerException e) {
238 e.printStackTrace();
73005152 239 }
09e86496 240 } catch (final NullPointerException e) {
085d898f
FC
241 e.printStackTrace();
242 }
73005152
BH
243 finally{
244 fLock.unlock();
245 }
085d898f 246 return null;
d18dd09b
ASL
247 }
248
c76c54bb 249
085d898f 250 @Override
7e6347b0 251 public TmfContext seekEvent(final double ratio) {
085d898f 252 fLock.lock();
c76c54bb 253 try {
73005152 254 if (fTrace != null) {
085d898f 255 final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
7e6347b0 256 final TmfContext context = seekEvent(location);
73005152
BH
257 context.setRank(ITmfContext.UNKNOWN_RANK);
258 return context;
259 }
085d898f 260 } catch (final IOException e) {
c76c54bb 261 e.printStackTrace();
73005152
BH
262 } finally {
263 fLock.unlock();
c76c54bb 264 }
085d898f 265
c76c54bb
FC
266 return null;
267 }
268
269 @Override
12c155f5 270 @SuppressWarnings("rawtypes")
085d898f 271 public double getLocationRatio(final ITmfLocation location) {
73005152 272 fLock.lock();
c76c54bb 273 try {
085d898f
FC
274 if (fTrace != null)
275 if (location.getLocation() instanceof Long)
73005152 276 return (double) ((Long) location.getLocation()) / fTrace.length();
085d898f 277 } catch (final IOException e) {
c76c54bb 278 e.printStackTrace();
73005152
BH
279 } finally {
280 fLock.unlock();
c76c54bb
FC
281 }
282 return 0;
283 }
284
4e3aa37d 285 @Override
085d898f 286 public TmfLocation<Long> getCurrentLocation() {
73005152 287 fLock.lock();
d18dd09b 288 try {
085d898f 289 if (fTrace != null)
73005152 290 return new TmfLocation<Long>(fTrace.getFilePointer());
085d898f
FC
291 } catch (final IOException e) {
292 e.printStackTrace();
293 } finally {
294 fLock.unlock();
295 }
296 return null;
297 }
298
299 @Override
300 public ITmfEvent parseEvent(final ITmfContext context) {
301 fLock.lock();
302 try {
303 // parseNextEvent will update the context
304 if (fTrace != null) {
7e6347b0 305 final ITmfEvent event = fParser.parseEvent(context.clone());
085d898f 306 return event;
73005152 307 }
7e6347b0
FC
308// }
309// catch (final IOException e) {
310// e.printStackTrace();
73005152
BH
311 } finally {
312 fLock.unlock();
d18dd09b
ASL
313 }
314 return null;
315 }
316
085d898f
FC
317 @Override
318 public void setTimeRange(final TmfTimeRange range) {
319 super.setTimeRange(range);
320 }
d18dd09b 321
085d898f
FC
322 @Override
323 public void setStartTime(final ITmfTimestamp startTime) {
324 super.setStartTime(startTime);
ff4ed569
FC
325 }
326
085d898f
FC
327 @Override
328 public void setEndTime(final ITmfTimestamp endTime) {
329 super.setEndTime(endTime);
ff4ed569
FC
330 }
331
1703b536
FC
332 @Override
333 public void setStreamingInterval(final long interval) {
334 super.setStreamingInterval(interval);
335 }
336
085d898f
FC
337 @Override
338 public void dispose() {
339 fLock.lock();
340 try {
341 if (fTrace != null) {
342 fTrace.close();
343 fTrace = null;
344 }
345 } catch (final IOException e) {
346 // Ignore
347 } finally {
348 fLock.unlock();
349 }
350 super.dispose();
ff4ed569
FC
351 }
352
e31e01e8 353}
This page took 0.0527 seconds and 5 git commands to generate.