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
extends TmfDataRequest
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
34 * The list of coalesced requests
36 protected Vector
<ITmfDataRequest
> fRequests
= new Vector
<ITmfDataRequest
>();
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
<?
extends ITmfEvent
> 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
<?
extends ITmfEvent
> 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
<?
extends ITmfEvent
> 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
<?
extends ITmfEvent
> 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
<?
extends ITmfEvent
> 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
<?
extends ITmfEvent
> 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
<?
extends ITmfEvent
> dataType
, long index
, int nbRequested
, int blockSize
) {
121 super(ITmfEvent
.class, 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
<?
extends ITmfEvent
> dataType
, long index
, int nbRequested
, int blockSize
, ExecutionType priority
) {
135 super(ITmfEvent
.class, index
, nbRequested
, blockSize
, priority
);
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 * Add a request to this one.
145 * @param request The request to add
147 public void addRequest(ITmfDataRequest request
) {
148 fRequests
.add(request
);
153 * Check if a request is compatible with the current coalesced one
156 * The request to verify
157 * @return If the request is compatible, true or false
159 public boolean isCompatible(ITmfDataRequest request
) {
160 if (request
.getExecType() == getExecType()) {
161 return overlaps(request
);
166 private boolean overlaps(ITmfDataRequest request
) {
167 long start
= request
.getIndex();
168 long end
= start
+ request
.getNbRequested();
170 // Return true if either the start or end index falls within
171 // the coalesced request boundaries
172 return (start
<= (fIndex
+ fNbRequested
+ 1) && (end
>= fIndex
- 1));
175 private void merge(ITmfDataRequest request
) {
176 long start
= request
.getIndex();
177 long end
= Math
.min(start
+ request
.getNbRequested(), TmfDataRequest
.ALL_DATA
);
179 if (start
< fIndex
) {
180 if (fNbRequested
!= TmfDataRequest
.ALL_DATA
) {
181 fNbRequested
+= (fIndex
- start
);
185 if ((request
.getNbRequested() == TmfDataRequest
.ALL_DATA
) ||
186 (fNbRequested
== TmfDataRequest
.ALL_DATA
))
188 fNbRequested
= TmfDataRequest
.ALL_DATA
;
190 fNbRequested
= (int) Math
.max(end
- fIndex
, fNbRequested
);
195 * @return The list of IDs of the sub-requests
197 @SuppressWarnings("nls")
198 public String
getSubRequestIds() {
199 StringBuffer result
= new StringBuffer("[");
200 for (int i
= 0; i
< fRequests
.size(); i
++) {
204 result
.append(fRequests
.get(i
).getRequestId());
207 return result
.toString();
210 // ------------------------------------------------------------------------
212 // ------------------------------------------------------------------------
215 public void handleData(ITmfEvent data
) {
216 super.handleData(data
);
217 // Don't call sub-requests handleData() unless this is a
218 // TmfCoalescedDataRequest; extended classes should call
219 // the sub-requests handleData().
220 if (getClass() == TmfCoalescedDataRequest
.class) {
221 long index
= getIndex() + getNbRead() - 1;
222 for (ITmfDataRequest request
: fRequests
) {
223 if (!request
.isCompleted()) {
224 if (request
.getDataType().isInstance(data
)) {
225 long start
= request
.getIndex();
226 long end
= start
+ request
.getNbRequested();
227 if (index
>= start
&& index
< end
) {
228 request
.handleData(data
);
237 public void start() {
238 for (ITmfDataRequest request
: fRequests
) {
239 if (!request
.isCompleted()) {
248 for (ITmfDataRequest request
: fRequests
) {
249 if (!request
.isCompleted()) {
258 for (ITmfDataRequest request
: fRequests
) {
265 public void cancel() {
266 for (ITmfDataRequest request
: fRequests
) {
267 if (!request
.isCompleted()) {
275 public synchronized boolean isCompleted() {
276 // Firstly, check if coalescing request is completed
277 if (super.isCompleted()) {
281 // Secondly, check if all sub-requests are finished
282 if (fRequests
.size() > 0) {
283 // If all sub requests are completed the coalesced request is
284 // treated as completed, too.
285 for (ITmfDataRequest request
: fRequests
) {
286 if (!request
.isCompleted()) {
293 // Coalescing request is not finished if there are no sub-requests
298 public synchronized boolean isCancelled() {
299 // Firstly, check if coalescing request is canceled
300 if (super.isCancelled()) {
304 // Secondly, check if all sub-requests are canceled
305 if (fRequests
.size() > 0) {
306 // If all sub requests are canceled the coalesced request is
307 // treated as completed, too.
308 for (ITmfDataRequest request
: fRequests
) {
309 if (!request
.isCancelled()) {
316 // Coalescing request is not canceled if there are no sub-requests
322 // ------------------------------------------------------------------------
324 // ------------------------------------------------------------------------
327 // All requests have a unique id
328 public int hashCode() {
329 return super.hashCode();
333 public boolean equals(Object other
) {
334 if (other
instanceof TmfCoalescedDataRequest
) {
335 TmfCoalescedDataRequest request
= (TmfCoalescedDataRequest
) other
;
336 return (request
.getDataType() == getDataType()) &&
337 (request
.getIndex() == getIndex()) &&
338 (request
.getNbRequested() == getNbRequested() &&
339 (request
.getExecType() == getExecType()));
345 @SuppressWarnings("nls")
346 public String
toString() {
347 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
348 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";