Internalize some classes and fix a pile of 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 * @version 1.0
25 * @author Francois Chouinard
26 */
27 public class TmfCoalescedDataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 /**
34 * The list of coalesced requests
35 */
36 protected Vector<ITmfDataRequest<T>> fRequests = new Vector<ITmfDataRequest<T>>();
37
38 // ------------------------------------------------------------------------
39 // Constructor
40 // ------------------------------------------------------------------------
41
42 /**
43 * Request all the events of a given type (high priority)
44 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
45 *
46 * @param dataType the requested data type
47 */
48 public TmfCoalescedDataRequest(Class<T> dataType) {
49 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
50 }
51
52 /**
53 * Request all the events of a given type (given priority)
54 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
55 *
56 * @param dataType the requested data type
57 * @param priority the requested execution priority
58 */
59 public TmfCoalescedDataRequest(Class<T> dataType, ExecutionType priority) {
60 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
61 }
62
63 /**
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).
66 *
67 * @param dataType the requested data type
68 * @param index the index of the first event to retrieve
69 */
70 public TmfCoalescedDataRequest(Class<T> dataType, long index) {
71 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
72 }
73
74 /**
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).
77 *
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
81 */
82 public TmfCoalescedDataRequest(Class<T> dataType, long index, ExecutionType priority) {
83 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
84 }
85
86 /**
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).
89 *
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
93 */
94 public TmfCoalescedDataRequest(Class<T> dataType, long index, int nbRequested) {
95 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
96 }
97
98 /**
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).
101 *
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
106 */
107 public TmfCoalescedDataRequest(Class<T> dataType, long index, int nbRequested, ExecutionType priority) {
108 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, priority);
109 }
110
111 /**
112 * Request 'n' events of a given type from the given index (high priority).
113 * Events are returned in blocks of the given size.
114 *
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
119 */
120 public TmfCoalescedDataRequest(Class<T> dataType, long index, int nbRequested, int blockSize) {
121 super(dataType, index, nbRequested, blockSize, ExecutionType.FOREGROUND);
122 }
123
124 /**
125 * Request 'n' events of a given type from the given index (given priority).
126 * Events are returned in blocks of the given size.
127 *
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
133 */
134 public TmfCoalescedDataRequest(Class<T> dataType, long index, int nbRequested, int blockSize, ExecutionType priority) {
135 super(dataType, index, nbRequested, blockSize, priority);
136 }
137
138 // ------------------------------------------------------------------------
139 // Management
140 // ------------------------------------------------------------------------
141
142 public void addRequest(ITmfDataRequest<T> request) {
143 fRequests.add(request);
144 merge(request);
145 }
146
147 public boolean isCompatible(ITmfDataRequest<T> request) {
148 if (request.getExecType() == getExecType()) {
149 return overlaps(request);
150 }
151 return false;
152 }
153
154 private boolean overlaps(ITmfDataRequest<T> request) {
155 long start = request.getIndex();
156 long end = start + request.getNbRequested();
157
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));
161 }
162
163 private void merge(ITmfDataRequest<T> request) {
164 long start = request.getIndex();
165 long end = Math.min(start + request.getNbRequested(), TmfDataRequest.ALL_DATA);
166
167 if (start < fIndex) {
168 if (fNbRequested != TmfDataRequest.ALL_DATA) {
169 fNbRequested += (fIndex - start);
170 }
171 fIndex = start;
172 }
173 if ((request.getNbRequested() == TmfDataRequest.ALL_DATA) ||
174 (fNbRequested == TmfDataRequest.ALL_DATA))
175 {
176 fNbRequested = TmfDataRequest.ALL_DATA;
177 } else {
178 fNbRequested = (int) Math.max(end - fIndex, fNbRequested);
179 }
180 }
181
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());
188 }
189 result.append("]");
190 return result.toString();
191 }
192
193 // ------------------------------------------------------------------------
194 // ITmfDataRequest
195 // ------------------------------------------------------------------------
196
197 @Override
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);
212 }
213 }
214 }
215 }
216 }
217 }
218
219 @Override
220 public void start() {
221 for (ITmfDataRequest<T> request : fRequests) {
222 if (!request.isCompleted()) {
223 request.start();
224 }
225 }
226 super.start();
227 }
228
229 @Override
230 public void done() {
231 for (ITmfDataRequest<T> request : fRequests) {
232 if (!request.isCompleted()) {
233 request.done();
234 }
235 }
236 super.done();
237 }
238
239 @Override
240 public void fail() {
241 for (ITmfDataRequest<T> request : fRequests) {
242 request.fail();
243 }
244 super.fail();
245 }
246
247 @Override
248 public void cancel() {
249 for (ITmfDataRequest<T> request : fRequests) {
250 if (!request.isCompleted()) {
251 request.cancel();
252 }
253 }
254 super.cancel();
255 }
256
257 @Override
258 public boolean isCompleted() {
259 // Firstly, check if coalescing request is completed
260 if (super.isCompleted()) {
261 return true;
262 }
263
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()) {
270 return false;
271 }
272 }
273 return true;
274 }
275
276 // Coalescing request is not finished if there are no sub-requests
277 return false;
278 }
279
280 @Override
281 public boolean isCancelled() {
282 // Firstly, check if coalescing request is canceled
283 if (super.isCancelled()) {
284 return true;
285 }
286
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()) {
293 return false;
294 }
295 }
296 return true;
297 }
298
299 // Coalescing request is not canceled if there are no sub-requests
300 return false;
301
302 }
303
304
305 // ------------------------------------------------------------------------
306 // Object
307 // ------------------------------------------------------------------------
308
309 @Override
310 // All requests have a unique id
311 public int hashCode() {
312 return super.hashCode();
313 }
314
315 @Override
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()));
323 }
324 return false;
325 }
326
327 @Override
328 @SuppressWarnings("nls")
329 public String toString() {
330 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
331 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
332 }
333 }
This page took 0.039634 seconds and 6 git commands to generate.