1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.request
;
15 import java
.util
.Vector
;
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
;
22 * The TMF coalesced data request
24 * @param <T> The request data type
27 * @author Francois Chouinard
29 public class TmfCoalescedDataRequest
<T
extends ITmfEvent
> extends TmfDataRequest
<T
> {
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
36 * The list of coalesced requests
38 protected Vector
<ITmfDataRequest
<T
>> fRequests
= new Vector
<ITmfDataRequest
<T
>>();
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Request all the events of a given type (high priority)
46 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
48 * @param dataType the requested data type
50 public TmfCoalescedDataRequest(Class
<T
> dataType
) {
51 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
55 * Request all the events of a given type (given priority)
56 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
58 * @param dataType the requested data type
59 * @param priority the requested execution priority
61 public TmfCoalescedDataRequest(Class
<T
> dataType
, ExecutionType priority
) {
62 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, priority
);
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).
69 * @param dataType the requested data type
70 * @param index the index of the first event to retrieve
72 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
) {
73 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
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).
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
84 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, ExecutionType priority
) {
85 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, priority
);
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).
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
96 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
) {
97 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
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).
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
109 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, ExecutionType priority
) {
110 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, priority
);
114 * Request 'n' events of a given type from the given index (high priority).
115 * Events are returned in blocks of the given size.
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
122 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, int blockSize
) {
123 super(dataType
, index
, nbRequested
, blockSize
, ExecutionType
.FOREGROUND
);
127 * Request 'n' events of a given type from the given index (given priority).
128 * Events are returned in blocks of the given size.
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
136 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, int blockSize
, ExecutionType priority
) {
137 super(dataType
, index
, nbRequested
, blockSize
, priority
);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Add a request to this one.
147 * @param request The request to add
149 public void addRequest(ITmfDataRequest
<T
> request
) {
150 fRequests
.add(request
);
155 * Check if a request is compatible with the current coalesced one
158 * The request to verify
159 * @return If the request is compatible, true or false
161 public boolean isCompatible(ITmfDataRequest
<T
> request
) {
162 if (request
.getExecType() == getExecType()) {
163 return overlaps(request
);
168 private boolean overlaps(ITmfDataRequest
<T
> request
) {
169 long start
= request
.getIndex();
170 long end
= start
+ request
.getNbRequested();
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));
177 private void merge(ITmfDataRequest
<T
> request
) {
178 long start
= request
.getIndex();
179 long end
= Math
.min(start
+ request
.getNbRequested(), TmfDataRequest
.ALL_DATA
);
181 if (start
< fIndex
) {
182 if (fNbRequested
!= TmfDataRequest
.ALL_DATA
) {
183 fNbRequested
+= (fIndex
- start
);
187 if ((request
.getNbRequested() == TmfDataRequest
.ALL_DATA
) ||
188 (fNbRequested
== TmfDataRequest
.ALL_DATA
))
190 fNbRequested
= TmfDataRequest
.ALL_DATA
;
192 fNbRequested
= (int) Math
.max(end
- fIndex
, fNbRequested
);
197 * @return The list of IDs of the sub-requests
199 @SuppressWarnings("nls")
200 public String
getSubRequestIds() {
201 StringBuffer result
= new StringBuffer("[");
202 for (int i
= 0; i
< fRequests
.size(); i
++) {
206 result
.append(fRequests
.get(i
).getRequestId());
209 return result
.toString();
212 // ------------------------------------------------------------------------
214 // ------------------------------------------------------------------------
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
);
239 public void start() {
240 for (ITmfDataRequest
<T
> request
: fRequests
) {
241 if (!request
.isCompleted()) {
250 for (ITmfDataRequest
<T
> request
: fRequests
) {
251 if (!request
.isCompleted()) {
260 for (ITmfDataRequest
<T
> request
: fRequests
) {
267 public void cancel() {
268 for (ITmfDataRequest
<T
> request
: fRequests
) {
269 if (!request
.isCompleted()) {
277 public synchronized boolean isCompleted() {
278 // Firstly, check if coalescing request is completed
279 if (super.isCompleted()) {
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()) {
295 // Coalescing request is not finished if there are no sub-requests
300 public synchronized boolean isCancelled() {
301 // Firstly, check if coalescing request is canceled
302 if (super.isCancelled()) {
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()) {
318 // Coalescing request is not canceled if there are no sub-requests
324 // ------------------------------------------------------------------------
326 // ------------------------------------------------------------------------
329 // All requests have a unique id
330 public int hashCode() {
331 return super.hashCode();
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()));
347 @SuppressWarnings("nls")
348 public String
toString() {
349 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
350 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
This page took 0.038157 seconds and 5 git commands to generate.