Partial fixes for experiment indexing with CTF traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / request / TmfCoalescedDataRequest.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.core.request;
14
15 import java.util.Vector;
16
17 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
18
19 /**
20 * <b><u>TmfCoalescedDataRequest</u></b>
21 * <p>
22 */
23 public class TmfCoalescedDataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 /**
30 * The list of coalesced requests
31 */
32 protected Vector<ITmfDataRequest<T>> fRequests = new Vector<ITmfDataRequest<T>>();
33
34 // ------------------------------------------------------------------------
35 // Constructor
36 // ------------------------------------------------------------------------
37
38 /**
39 * Request all the events of a given type (high priority)
40 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
41 *
42 * @param dataType the requested data type
43 */
44 public TmfCoalescedDataRequest(Class<T> dataType) {
45 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
46 }
47
48 /**
49 * Request all the events of a given type (given priority)
50 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
51 *
52 * @param dataType the requested data type
53 * @param priority the requested execution priority
54 */
55 public TmfCoalescedDataRequest(Class<T> dataType, ExecutionType priority) {
56 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
57 }
58
59 /**
60 * Request all the events of a given type from the given index (high priority)
61 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
62 *
63 * @param dataType the requested data type
64 * @param index the index of the first event to retrieve
65 */
66 public TmfCoalescedDataRequest(Class<T> dataType, int index) {
67 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
68 }
69
70 /**
71 * Request all the events of a given type from the given index (given priority)
72 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
73 *
74 * @param dataType the requested data type
75 * @param index the index of the first event to retrieve
76 * @param priority the requested execution priority
77 */
78 public TmfCoalescedDataRequest(Class<T> dataType, int index, ExecutionType priority) {
79 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
80 }
81
82 /**
83 * Request 'n' events of a given type from the given index (high priority)
84 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
85 *
86 * @param dataType the requested data type
87 * @param index the index of the first event to retrieve
88 * @param nbRequested the number of events requested
89 */
90 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested) {
91 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
92 }
93
94 /**
95 * Request 'n' events of a given type from the given index (given priority)
96 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
97 *
98 * @param dataType the requested data type
99 * @param index the index of the first event to retrieve
100 * @param nbRequested the number of events requested
101 * @param priority the requested execution priority
102 */
103 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, ExecutionType priority) {
104 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, priority);
105 }
106
107 /**
108 * Request 'n' events of a given type from the given index (high priority).
109 * Events are returned in blocks of the given size.
110 *
111 * @param dataType the requested data type
112 * @param index the index of the first event to retrieve
113 * @param nbRequested the number of events requested
114 * @param blockSize the number of events per block
115 */
116 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize) {
117 super(dataType, index, nbRequested, blockSize, ExecutionType.FOREGROUND);
118 }
119
120 /**
121 * Request 'n' events of a given type from the given index (given priority).
122 * Events are returned in blocks of the given size.
123 *
124 * @param dataType the requested data type
125 * @param index the index of the first event to retrieve
126 * @param nbRequested the number of events requested
127 * @param blockSize the number of events per block
128 * @param priority the requested execution priority
129 */
130 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize, ExecutionType priority) {
131 super(dataType, index, nbRequested, blockSize, priority);
132 }
133
134 // ------------------------------------------------------------------------
135 // Management
136 // ------------------------------------------------------------------------
137
138 public void addRequest(ITmfDataRequest<T> request) {
139 fRequests.add(request);
140 }
141
142 public boolean isCompatible(ITmfDataRequest<T> request) {
143
144 boolean ok = request.getIndex() == getIndex();
145 ok &= request.getNbRequested() == getNbRequested();
146 ok &= request.getExecType() == getExecType();
147
148 return ok;
149 }
150
151 @SuppressWarnings("nls")
152 public String getSubRequestIds() {
153 StringBuffer result = new StringBuffer("[");
154 for (int i = 0; i < fRequests.size(); i++) {
155 if (i != 0) result.append(", ");
156 result.append(fRequests.get(i).getRequestId());
157 }
158 result.append("]");
159 return result.toString();
160 }
161
162 // ------------------------------------------------------------------------
163 // ITmfDataRequest
164 // ------------------------------------------------------------------------
165
166 @Override
167 public void handleData(T data) {
168 super.handleData(data);
169 // Don't call sub-requests handleData() unless this is a
170 // TmfCoalescedDataRequest; extended classes should call
171 // the sub-requests handleData().
172 if (getClass() == TmfCoalescedDataRequest.class) {
173 for (ITmfDataRequest<T> request : fRequests) {
174 if (!request.isCompleted()) {
175 if (request.getDataType().isInstance(data)) {
176 request.handleData(data);
177 }
178 }
179 }
180 }
181 }
182
183 @Override
184 public void start() {
185 for (ITmfDataRequest<T> request : fRequests) {
186 if (!request.isCompleted()) {
187 request.start();
188 }
189 }
190 super.start();
191 }
192
193 @Override
194 public void done() {
195 for (ITmfDataRequest<T> request : fRequests) {
196 if (!request.isCompleted()) {
197 request.done();
198 }
199 }
200 super.done();
201 }
202
203 @Override
204 public void fail() {
205 for (ITmfDataRequest<T> request : fRequests) {
206 request.fail();
207 }
208 super.fail();
209 }
210
211 @Override
212 public void cancel() {
213 for (ITmfDataRequest<T> request : fRequests) {
214 if (!request.isCompleted()) {
215 request.cancel();
216 }
217 }
218 super.cancel();
219 }
220
221 @Override
222 public boolean isCompleted() {
223 // Firstly, check if coalescing request is completed
224 if (super.isCompleted()) {
225 return true;
226 }
227
228 // Secondly, check if all sub-requests are finished
229 if (fRequests.size() > 0) {
230 // If all sub requests are completed the coalesced request is
231 // treated as completed, too.
232 for (ITmfDataRequest<T> request : fRequests) {
233 if (!request.isCompleted()) {
234 return false;
235 }
236 }
237 return true;
238 }
239
240 // Coalescing request is not finished if there are no sub-requests
241 return false;
242 }
243
244 @Override
245 public boolean isCancelled() {
246 // Firstly, check if coalescing request is canceled
247 if (super.isCancelled()) {
248 return true;
249 }
250
251 // Secondly, check if all sub-requests are canceled
252 if (fRequests.size() > 0) {
253 // If all sub requests are canceled the coalesced request is
254 // treated as completed, too.
255 for (ITmfDataRequest<T> request : fRequests) {
256 if (!request.isCancelled()) {
257 return false;
258 }
259 }
260 return true;
261 }
262
263 // Coalescing request is not canceled if there are no sub-requests
264 return false;
265
266 }
267
268
269 // ------------------------------------------------------------------------
270 // Object
271 // ------------------------------------------------------------------------
272
273 @Override
274 // All requests have a unique id
275 public int hashCode() {
276 return super.hashCode();
277 }
278
279 @Override
280 public boolean equals(Object other) {
281 if (other instanceof TmfCoalescedDataRequest<?>) {
282 TmfCoalescedDataRequest<?> request = (TmfCoalescedDataRequest<?>) other;
283 return (request.getDataType() == getDataType()) &&
284 (request.getIndex() == getIndex()) &&
285 (request.getNbRequested() == getNbRequested() &&
286 (request.getExecType() == getExecType()));
287 }
288 return false;
289 }
290
291 @Override
292 @SuppressWarnings("nls")
293 public String toString() {
294 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
295 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
296 }
297 }
This page took 0.036651 seconds and 5 git commands to generate.