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