2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[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 @Override
144 public TmfLocation<Long> getCurrentLocation() {
145 try {
146 return new TmfLocation<Long>(fTrace.getFilePointer());
147 } catch (IOException e) {
148 e.printStackTrace();
149 }
150 return null;
151 }
152
153 @Override
154 public TmfEvent parseEvent(TmfContext context) {
155 try {
156 // parseNextEvent will update the context
157 TmfEvent event = fParser.parseNextEvent(this, context.clone());
158 return event;
159 }
160 catch (IOException e) {
161 e.printStackTrace();
162 }
163 return null;
164 }
165
166 @Override
167 public void setTimeRange(TmfTimeRange range) {
168 super.setTimeRange(range);
169 }
170
171 @Override
172 public void setStartTime(TmfTimestamp startTime) {
173 super.setStartTime(startTime);
174 }
175
176 @Override
177 public void setEndTime(TmfTimestamp endTime) {
178 super.setEndTime(endTime);
179 }
180
181 }
This page took 0.036043 seconds and 6 git commands to generate.