1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
23 * The TMF coalesced data request
26 * @author Francois Chouinard
28 public class TmfCoalescedDataRequest
extends TmfDataRequest
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
35 * The list of coalesced requests
37 protected final List
<ITmfDataRequest
> fRequests
= new ArrayList
<ITmfDataRequest
>();
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 * Request 'n' events of a given type from the given index (given priority).
45 * Events are returned in blocks of the given size.
48 * The requested data type
50 * The index of the first event to retrieve. Use '0' to start at
53 * The number of events requested. You can use
54 * {@link TmfDataRequest#ALL_DATA} to request all events.
56 * The requested execution priority
58 public TmfCoalescedDataRequest(Class
<?
extends ITmfEvent
> dataType
,
61 ExecutionType priority
) {
62 super(ITmfEvent
.class, index
, nbRequested
, priority
);
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Add a request to this one.
75 public void addRequest(ITmfDataRequest request
) {
76 fRequests
.add(request
);
81 * Check if a request is compatible with the current coalesced one
84 * The request to verify
85 * @return If the request is compatible, true or false
87 public boolean isCompatible(ITmfDataRequest request
) {
88 if (request
.getExecType() == getExecType()) {
89 return overlaps(request
);
94 private boolean overlaps(ITmfDataRequest request
) {
95 long start
= request
.getIndex();
96 long end
= start
+ request
.getNbRequested();
98 // Return true if either the start or end index falls within
99 // the coalesced request boundaries
100 return (start
<= (fIndex
+ fNbRequested
+ 1) && (end
>= fIndex
- 1));
103 private void merge(ITmfDataRequest request
) {
104 long start
= request
.getIndex();
105 long end
= Math
.min(start
+ request
.getNbRequested(), TmfDataRequest
.ALL_DATA
);
107 if (start
< fIndex
) {
108 if (fNbRequested
!= TmfDataRequest
.ALL_DATA
) {
109 fNbRequested
+= (fIndex
- start
);
113 if ((request
.getNbRequested() == TmfDataRequest
.ALL_DATA
) ||
114 (fNbRequested
== TmfDataRequest
.ALL_DATA
))
116 fNbRequested
= TmfDataRequest
.ALL_DATA
;
118 fNbRequested
= (int) Math
.max(end
- fIndex
, fNbRequested
);
123 * @return The list of IDs of the sub-requests
125 @SuppressWarnings("nls")
126 public String
getSubRequestIds() {
127 StringBuffer result
= new StringBuffer("[");
128 for (int i
= 0; i
< fRequests
.size(); i
++) {
132 result
.append(fRequests
.get(i
).getRequestId());
135 return result
.toString();
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 public void handleData(ITmfEvent data
) {
144 super.handleData(data
);
145 // Don't call sub-requests handleData() unless this is a
146 // TmfCoalescedDataRequest; extended classes should call
147 // the sub-requests handleData().
148 if (getClass() == TmfCoalescedDataRequest
.class) {
149 long index
= getIndex() + getNbRead() - 1;
150 for (ITmfDataRequest request
: fRequests
) {
151 if (!request
.isCompleted()) {
152 if (request
.getDataType().isInstance(data
)) {
153 long start
= request
.getIndex();
154 long end
= start
+ request
.getNbRequested();
155 if (index
>= start
&& index
< end
) {
156 request
.handleData(data
);
165 public void start() {
166 for (ITmfDataRequest request
: fRequests
) {
167 if (!request
.isCompleted()) {
176 for (ITmfDataRequest request
: fRequests
) {
177 if (!request
.isCompleted()) {
186 for (ITmfDataRequest request
: fRequests
) {
193 public void cancel() {
194 for (ITmfDataRequest request
: fRequests
) {
195 if (!request
.isCompleted()) {
203 public synchronized boolean isCompleted() {
204 // Firstly, check if coalescing request is completed
205 if (super.isCompleted()) {
209 // Secondly, check if all sub-requests are finished
210 if (fRequests
.size() > 0) {
211 // If all sub requests are completed the coalesced request is
212 // treated as completed, too.
213 for (ITmfDataRequest request
: fRequests
) {
214 if (!request
.isCompleted()) {
221 // Coalescing request is not finished if there are no sub-requests
226 public synchronized boolean isCancelled() {
227 // Firstly, check if coalescing request is canceled
228 if (super.isCancelled()) {
232 // Secondly, check if all sub-requests are canceled
233 if (fRequests
.size() > 0) {
234 // If all sub requests are canceled the coalesced request is
235 // treated as completed, too.
236 for (ITmfDataRequest request
: fRequests
) {
237 if (!request
.isCancelled()) {
244 // Coalescing request is not canceled if there are no sub-requests
249 // ------------------------------------------------------------------------
251 // ------------------------------------------------------------------------
254 // All requests have a unique id
255 public int hashCode() {
256 return super.hashCode();
260 public boolean equals(Object other
) {
261 if (other
instanceof TmfCoalescedDataRequest
) {
262 TmfCoalescedDataRequest request
= (TmfCoalescedDataRequest
) other
;
263 return (request
.getDataType() == getDataType()) &&
264 (request
.getIndex() == getIndex()) &&
265 (request
.getNbRequested() == getNbRequested() &&
266 (request
.getExecType() == getExecType()));
272 @SuppressWarnings("nls")
273 public String
toString() {
274 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
275 + "," + getExecType() + "," + getIndex() + "," + getNbRequested() + ","
276 + fRequests
.toString() + ")]";