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