Fix for Bug338162
[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 @SuppressWarnings("nls")
30 public class TmfTraceStub extends TmfTrace<TmfEvent> {
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
36 // The actual stream
37 private RandomAccessFile fTrace;
38
39 // The associated event parser
40 private ITmfEventParser fParser;
41
42 // ------------------------------------------------------------------------
43 // Constructors
44 // ------------------------------------------------------------------------
45
46 /**
47 * @param filename
48 * @throws FileNotFoundException
49 */
50 public TmfTraceStub(String filename) throws FileNotFoundException {
51 this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
52 }
53
54 /**
55 * @param filename
56 * @param cacheSize
57 * @throws FileNotFoundException
58 */
59 public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
60 this(filename, cacheSize, false);
61 }
62
63 /**
64 * @param filename
65 * @param waitForCompletion
66 * @throws FileNotFoundException
67 */
68 public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
69 this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
70 }
71
72 /**
73 * @param filename
74 * @param cacheSize
75 * @param waitForCompletion
76 * @throws FileNotFoundException
77 */
78 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
79 super(filename, TmfEvent.class, filename, cacheSize);
80 fTrace = new RandomAccessFile(filename, "r");
81 fParser = new TmfEventParserStub();
82 }
83
84 /**
85 */
86 @Override
87 public TmfTraceStub clone() {
88 TmfTraceStub clone = null;
89 try {
90 clone = (TmfTraceStub) super.clone();
91 clone.fTrace = new RandomAccessFile(getPath(), "r");
92 clone.fParser = new TmfEventParserStub();
93 } catch (CloneNotSupportedException e) {
94 } catch (FileNotFoundException e) {
95 }
96 return clone;
97 }
98
99 @Override
100 public ITmfTrace createTraceCopy() {
101 ITmfTrace returnedValue = null;
102 returnedValue = clone();
103 return returnedValue;
104 }
105
106 // ------------------------------------------------------------------------
107 // Accessors
108 // ------------------------------------------------------------------------
109
110 public RandomAccessFile getStream() {
111 return fTrace;
112 }
113
114 // ------------------------------------------------------------------------
115 // Operators
116 // ------------------------------------------------------------------------
117
118 @Override
119 @SuppressWarnings("unchecked")
120 public TmfContext seekLocation(ITmfLocation<?> location) {
121 try {
122 synchronized(fTrace) {
123 // Position the trace at the requested location and
124 // returns the corresponding context
125 long loc = 0;
126 long rank = 0;
127 if (location != null) {
128 loc = ((TmfLocation<Long>) location).getLocation();
129 rank = ITmfContext.UNKNOWN_RANK;
130 }
131 if (loc != fTrace.getFilePointer()) {
132 fTrace.seek(loc);
133 }
134 TmfContext context = new TmfContext(getCurrentLocation(), rank);
135 return context;
136 }
137 } catch (IOException e) {
138 e.printStackTrace();
139 }
140 return null;
141 }
142
143
144 @Override
145 public TmfContext seekLocation(double ratio) {
146 try {
147 ITmfLocation<?> location = new TmfLocation<Long>(new Long((long) (ratio * fTrace.length())));
148 TmfContext context = seekLocation(location);
149 context.setRank(ITmfContext.UNKNOWN_RANK);
150 return context;
151 } catch (IOException e) {
152 e.printStackTrace();
153 }
154 return null;
155 }
156
157 @Override
158 public double getLocationRatio(ITmfLocation<?> location) {
159 try {
160 if (location.getLocation() instanceof Long) {
161 return (double) ((Long) location.getLocation()) / fTrace.length();
162 }
163 } catch (IOException e) {
164 e.printStackTrace();
165 }
166 return 0;
167 }
168
169 @Override
170 public TmfLocation<Long> getCurrentLocation() {
171 try {
172 return new TmfLocation<Long>(fTrace.getFilePointer());
173 } catch (IOException e) {
174 e.printStackTrace();
175 }
176 return null;
177 }
178
179 @Override
180 public TmfEvent parseEvent(TmfContext context) {
181 try {
182 // parseNextEvent will update the context
183 TmfEvent event = fParser.parseNextEvent(this, context.clone());
184 return event;
185 }
186 catch (IOException e) {
187 e.printStackTrace();
188 }
189 return null;
190 }
191
192 @Override
193 public void setTimeRange(TmfTimeRange range) {
194 super.setTimeRange(range);
195 }
196
197 @Override
198 public void setStartTime(TmfTimestamp startTime) {
199 super.setStartTime(startTime);
200 }
201
202 @Override
203 public void setEndTime(TmfTimestamp endTime) {
204 super.setEndTime(endTime);
205 }
206
207 }
This page took 0.042133 seconds and 6 git commands to generate.