2010-10-26 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug309042
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / 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.trace;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.RandomAccessFile;
18
19 import org.eclipse.linuxtools.tmf.event.TmfEvent;
20 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
21 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
22 import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
23
24 /**
25 * <b><u>TmfTraceStub</u></b>
26 * <p>
27 * Dummy test trace. Use in conjunction with TmfEventParserStub.
28 */
29 public class TmfTraceStub extends TmfTrace<TmfEvent> {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 // The actual stream
36 private RandomAccessFile fTrace;
37
38 // The associated event parser
39 private ITmfEventParser fParser;
40
41 // ------------------------------------------------------------------------
42 // Constructors
43 // ------------------------------------------------------------------------
44
45 /**
46 * @param filename
47 * @throws FileNotFoundException
48 */
49 public TmfTraceStub(String filename) throws FileNotFoundException {
50 this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
51 }
52
53 /**
54 * @param filename
55 * @param cacheSize
56 * @throws FileNotFoundException
57 */
58 public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
59 this(filename, cacheSize, false);
60 }
61
62 /**
63 * @param filename
64 * @param waitForCompletion
65 * @throws FileNotFoundException
66 */
67 public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
68 this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
69 }
70
71 /**
72 * @param filename
73 * @param cacheSize
74 * @param waitForCompletion
75 * @throws FileNotFoundException
76 */
77 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
78 super(filename, TmfEvent.class, filename, cacheSize);
79 fTrace = new RandomAccessFile(filename, "r");
80 fParser = new TmfEventParserStub();
81 }
82
83 /**
84 */
85 @Override
86 public TmfTraceStub clone() {
87 TmfTraceStub clone = null;
88 try {
89 clone = (TmfTraceStub) super.clone();
90 clone.fTrace = new RandomAccessFile(getPath(), "r");
91 clone.fParser = new TmfEventParserStub();
92 } catch (CloneNotSupportedException e) {
93 } catch (FileNotFoundException e) {
94 }
95 return clone;
96 }
97
98 @Override
99 public ITmfTrace createTraceCopy() {
100 ITmfTrace returnedValue = null;
101 returnedValue = clone();
102 return returnedValue;
103 }
104
105 // ------------------------------------------------------------------------
106 // Accessors
107 // ------------------------------------------------------------------------
108
109 public RandomAccessFile getStream() {
110 return fTrace;
111 }
112
113 // ------------------------------------------------------------------------
114 // Operators
115 // ------------------------------------------------------------------------
116
117 @Override
118 @SuppressWarnings("unchecked")
119 public TmfContext seekLocation(ITmfLocation<?> location) {
120 try {
121 synchronized(fTrace) {
122 // Position the trace at the requested location and
123 // returns the corresponding context
124 long loc = 0;
125 long rank = 0;
126 if (location != null) {
127 loc = ((TmfLocation<Long>) location).getLocation();
128 rank = ITmfContext.UNKNOWN_RANK;
129 }
130 if (loc != fTrace.getFilePointer()) {
131 fTrace.seek(loc);
132 }
133 TmfContext context = new TmfContext(getCurrentLocation(), rank);
134 return context;
135 }
136 } catch (IOException e) {
137 e.printStackTrace();
138 }
139 return null;
140 }
141
142 @Override
143 public TmfLocation<Long> getCurrentLocation() {
144 try {
145 return new TmfLocation<Long>(fTrace.getFilePointer());
146 } catch (IOException e) {
147 e.printStackTrace();
148 }
149 return null;
150 }
151
152 @Override
153 public TmfEvent parseEvent(TmfContext context) {
154 try {
155 // parseNextEvent will update the context
156 TmfEvent event = fParser.parseNextEvent(this, context.clone());
157 return event;
158 }
159 catch (IOException e) {
160 e.printStackTrace();
161 }
162 return null;
163 }
164
165 @Override
166 public void setTimeRange(TmfTimeRange range) {
167 super.setTimeRange(range);
168 }
169
170 @Override
171 public void setStartTime(TmfTimestamp startTime) {
172 super.setStartTime(startTime);
173 }
174
175 @Override
176 public void setEndTime(TmfTimestamp endTime) {
177 super.setEndTime(endTime);
178 }
179
180 }
This page took 0.038573 seconds and 5 git commands to generate.