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
25 * @author Francois Chouinard
27 public class TmfCoalescedDataRequest
<T
extends ITmfEvent
> extends TmfDataRequest
<T
> {
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
34 * The list of coalesced requests
36 protected Vector
<ITmfDataRequest
<T
>> fRequests
= new Vector
<ITmfDataRequest
<T
>>();
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
43 * Request all the events of a given type (high priority)
44 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
46 * @param dataType the requested data type
48 public TmfCoalescedDataRequest(Class
<T
> dataType
) {
49 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
53 * Request all the events of a given type (given priority)
54 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
56 * @param dataType the requested data type
57 * @param priority the requested execution priority
59 public TmfCoalescedDataRequest(Class
<T
> dataType
, ExecutionType priority
) {
60 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, priority
);
64 * Request all the events of a given type from the given index (high priority)
65 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
67 * @param dataType the requested data type
68 * @param index the index of the first event to retrieve
70 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
) {
71 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
75 * Request all the events of a given type from the given index (given priority)
76 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
78 * @param dataType the requested data type
79 * @param index the index of the first event to retrieve
80 * @param priority the requested execution priority
82 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, ExecutionType priority
) {
83 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, priority
);
87 * Request 'n' events of a given type from the given index (high priority)
88 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
90 * @param dataType the requested data type
91 * @param index the index of the first event to retrieve
92 * @param nbRequested the number of events requested
94 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
) {
95 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
99 * Request 'n' events of a given type from the given index (given priority)
100 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
102 * @param dataType the requested data type
103 * @param index the index of the first event to retrieve
104 * @param nbRequested the number of events requested
105 * @param priority the requested execution priority
107 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, ExecutionType priority
) {
108 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, priority
);
112 * Request 'n' events of a given type from the given index (high priority).
113 * Events are returned in blocks of the given size.
115 * @param dataType the requested data type
116 * @param index the index of the first event to retrieve
117 * @param nbRequested the number of events requested
118 * @param blockSize the number of events per block
120 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, int blockSize
) {
121 super(dataType
, index
, nbRequested
, blockSize
, ExecutionType
.FOREGROUND
);
125 * Request 'n' events of a given type from the given index (given priority).
126 * Events are returned in blocks of the given size.
128 * @param dataType the requested data type
129 * @param index the index of the first event to retrieve
130 * @param nbRequested the number of events requested
131 * @param blockSize the number of events per block
132 * @param priority the requested execution priority
134 public TmfCoalescedDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, int blockSize
, ExecutionType priority
) {
135 super(dataType
, index
, nbRequested
, blockSize
, priority
);
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
142 public void addRequest(ITmfDataRequest
<T
> request
) {
143 fRequests
.add(request
);
147 public boolean isCompatible(ITmfDataRequest
<T
> request
) {
148 if (request
.getExecType() == getExecType()) {
149 return overlaps(request
);
154 private boolean overlaps(ITmfDataRequest
<T
> request
) {
155 long start
= request
.getIndex();
156 long end
= start
+ request
.getNbRequested();
158 // Return true if either the start or end index falls within
159 // the coalesced request boundaries
160 return (start
<= (fIndex
+ fNbRequested
+ 1) && (end
>= fIndex
- 1));
163 private void merge(ITmfDataRequest
<T
> request
) {
164 long start
= request
.getIndex();
165 long end
= Math
.min(start
+ request
.getNbRequested(), TmfDataRequest
.ALL_DATA
);
167 if (start
< fIndex
) {
168 if (fNbRequested
!= TmfDataRequest
.ALL_DATA
) {
169 fNbRequested
+= (fIndex
- start
);
173 if ((request
.getNbRequested() == TmfDataRequest
.ALL_DATA
) ||
174 (fNbRequested
== TmfDataRequest
.ALL_DATA
))
176 fNbRequested
= TmfDataRequest
.ALL_DATA
;
178 fNbRequested
= (int) Math
.max(end
- fIndex
, fNbRequested
);
182 @SuppressWarnings("nls")
183 public String
getSubRequestIds() {
184 StringBuffer result
= new StringBuffer("[");
185 for (int i
= 0; i
< fRequests
.size(); i
++) {
186 if (i
!= 0) result
.append(", ");
187 result
.append(fRequests
.get(i
).getRequestId());
190 return result
.toString();
193 // ------------------------------------------------------------------------
195 // ------------------------------------------------------------------------
198 public void handleData(T data
) {
199 super.handleData(data
);
200 // Don't call sub-requests handleData() unless this is a
201 // TmfCoalescedDataRequest; extended classes should call
202 // the sub-requests handleData().
203 if (getClass() == TmfCoalescedDataRequest
.class) {
204 long index
= getNbRead();
205 for (ITmfDataRequest
<T
> request
: fRequests
) {
206 if (!request
.isCompleted()) {
207 if (request
.getDataType().isInstance(data
)) {
208 long start
= request
.getIndex();
209 long end
= start
+ request
.getNbRequested() - 1;
210 if (index
>= start
&& index
< end
) {
211 request
.handleData(data
);
220 public void start() {
221 for (ITmfDataRequest
<T
> request
: fRequests
) {
222 if (!request
.isCompleted()) {
231 for (ITmfDataRequest
<T
> request
: fRequests
) {
232 if (!request
.isCompleted()) {
241 for (ITmfDataRequest
<T
> request
: fRequests
) {
248 public void cancel() {
249 for (ITmfDataRequest
<T
> request
: fRequests
) {
250 if (!request
.isCompleted()) {
258 public boolean isCompleted() {
259 // Firstly, check if coalescing request is completed
260 if (super.isCompleted()) {
264 // Secondly, check if all sub-requests are finished
265 if (fRequests
.size() > 0) {
266 // If all sub requests are completed the coalesced request is
267 // treated as completed, too.
268 for (ITmfDataRequest
<T
> request
: fRequests
) {
269 if (!request
.isCompleted()) {
276 // Coalescing request is not finished if there are no sub-requests
281 public boolean isCancelled() {
282 // Firstly, check if coalescing request is canceled
283 if (super.isCancelled()) {
287 // Secondly, check if all sub-requests are canceled
288 if (fRequests
.size() > 0) {
289 // If all sub requests are canceled the coalesced request is
290 // treated as completed, too.
291 for (ITmfDataRequest
<T
> request
: fRequests
) {
292 if (!request
.isCancelled()) {
299 // Coalescing request is not canceled if there are no sub-requests
305 // ------------------------------------------------------------------------
307 // ------------------------------------------------------------------------
310 // All requests have a unique id
311 public int hashCode() {
312 return super.hashCode();
316 public boolean equals(Object other
) {
317 if (other
instanceof TmfCoalescedDataRequest
<?
>) {
318 TmfCoalescedDataRequest
<?
> request
= (TmfCoalescedDataRequest
<?
>) other
;
319 return (request
.getDataType() == getDataType()) &&
320 (request
.getIndex() == getIndex()) &&
321 (request
.getNbRequested() == getNbRequested() &&
322 (request
.getExecType() == getExecType()));
328 @SuppressWarnings("nls")
329 public String
toString() {
330 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
331 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
This page took 0.037512 seconds and 5 git commands to generate.