5d1ad858f4b69f6d8a334c3310d609ed35e1d7eb
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / 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.tmf.core.request;
14
15 import java.util.Vector;
16
17 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
18
19 /**
20 * <b><u>TmfCoalescedDataRequest</u></b>
21 * <p>
22 */
23 public class TmfCoalescedDataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 /**
30 * The list of coalesced requests
31 */
32 protected Vector<ITmfDataRequest<T>> fRequests = new Vector<ITmfDataRequest<T>>();
33
34 // ------------------------------------------------------------------------
35 // Constructor
36 // ------------------------------------------------------------------------
37
38 /**
39 * Request all the events of a given type (high priority)
40 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
41 *
42 * @param dataType the requested data type
43 */
44 public TmfCoalescedDataRequest(Class<T> dataType) {
45 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
46 }
47
48 /**
49 * Request all the events of a given type (given priority)
50 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
51 *
52 * @param dataType the requested data type
53 * @param priority the requested execution priority
54 */
55 public TmfCoalescedDataRequest(Class<T> dataType, ExecutionType priority) {
56 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
57 }
58
59 /**
60 * Request all the events of a given type from the given index (high priority)
61 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
62 *
63 * @param dataType the requested data type
64 * @param index the index of the first event to retrieve
65 */
66 public TmfCoalescedDataRequest(Class<T> dataType, int index) {
67 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
68 }
69
70 /**
71 * Request all the events of a given type from the given index (given priority)
72 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
73 *
74 * @param dataType the requested data type
75 * @param index the index of the first event to retrieve
76 * @param priority the requested execution priority
77 */
78 public TmfCoalescedDataRequest(Class<T> dataType, int index, ExecutionType priority) {
79 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
80 }
81
82 /**
83 * Request 'n' events of a given type from the given index (high priority)
84 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
85 *
86 * @param dataType the requested data type
87 * @param index the index of the first event to retrieve
88 * @param nbRequested the number of events requested
89 */
90 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested) {
91 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
92 }
93
94 /**
95 * Request 'n' events of a given type from the given index (given priority)
96 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
97 *
98 * @param dataType the requested data type
99 * @param index the index of the first event to retrieve
100 * @param nbRequested the number of events requested
101 * @param priority the requested execution priority
102 */
103 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, ExecutionType priority) {
104 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, priority);
105 }
106
107 /**
108 * Request 'n' events of a given type from the given index (high priority).
109 * Events are returned in blocks of the given size.
110 *
111 * @param dataType the requested data type
112 * @param index the index of the first event to retrieve
113 * @param nbRequested the number of events requested
114 * @param blockSize the number of events per block
115 */
116 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize) {
117 super(dataType, index, nbRequested, blockSize, ExecutionType.FOREGROUND);
118 }
119
120 /**
121 * Request 'n' events of a given type from the given index (given priority).
122 * Events are returned in blocks of the given size.
123 *
124 * @param dataType the requested data type
125 * @param index the index of the first event to retrieve
126 * @param nbRequested the number of events requested
127 * @param blockSize the number of events per block
128 * @param priority the requested execution priority
129 */
130 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize, ExecutionType priority) {
131 super(dataType, index, nbRequested, blockSize, priority);
132 }
133
134 // ------------------------------------------------------------------------
135 // Management
136 // ------------------------------------------------------------------------
137
138 public void addRequest(ITmfDataRequest<T> request) {
139 fRequests.add(request);
140 }
141
142 public boolean isCompatible(ITmfDataRequest<T> request) {
143
144 boolean ok = request.getIndex() == getIndex();
145 ok &= request.getNbRequested() == getNbRequested();
146 ok &= request.getExecType() == getExecType();
147 //ok &= request.getDataType() == getDataType();
148
149 return ok;
150 }
151
152 // ------------------------------------------------------------------------
153 // ITmfDataRequest
154 // ------------------------------------------------------------------------
155
156 @Override
157 public void handleData(T data) {
158 super.handleData(data);
159 // Don't call sub-requests handleData() unless this is a
160 // TmfCoalescedDataRequest; extended classes should call
161 // the sub-requests handleData().
162 if (getClass() == TmfCoalescedDataRequest.class) {
163 for (ITmfDataRequest<T> request : fRequests) {
164 if (!request.isCompleted()) {
165 if (request.getDataType().isInstance(data)) {
166 request.handleData(data);
167 }
168 }
169 }
170 }
171 }
172
173 @Override
174 public void start() {
175 for (ITmfDataRequest<T> request : fRequests) {
176 if (!request.isCompleted()) {
177 request.start();
178 }
179 }
180 super.start();
181 }
182
183 @Override
184 public void done() {
185 for (ITmfDataRequest<T> request : fRequests) {
186 if (!request.isCompleted()) {
187 request.done();
188 }
189 }
190 super.done();
191 }
192
193 @Override
194 public void fail() {
195 for (ITmfDataRequest<T> request : fRequests) {
196 request.fail();
197 }
198 super.fail();
199 }
200
201 @Override
202 public void cancel() {
203 for (ITmfDataRequest<T> request : fRequests) {
204 if (!request.isCompleted()) {
205 request.cancel();
206 }
207 }
208 super.cancel();
209 }
210
211 @Override
212 public boolean isCompleted() {
213 // Firstly, check if coalescing request is completed
214 if (super.isCompleted()) {
215 return true;
216 }
217
218 // Secondly, check if all sub-requests are finished
219 if (fRequests.size() > 0) {
220 // If all sub requests are completed the coalesced request is
221 // treated as completed, too.
222 for (ITmfDataRequest<T> request : fRequests) {
223 if (!request.isCompleted()) {
224 return false;
225 }
226 }
227 return true;
228 }
229
230 // Coalescing request is not finished if there are no sub-requests
231 return false;
232 }
233
234 @Override
235 public boolean isCancelled() {
236 // Firstly, check if coalescing request is canceled
237 if (super.isCancelled()) {
238 return true;
239 }
240
241 // Secondly, check if all sub-requests are canceled
242 if (fRequests.size() > 0) {
243 // If all sub requests are canceled the coalesced request is
244 // treated as completed, too.
245 for (ITmfDataRequest<T> request : fRequests) {
246 if (!request.isCancelled()) {
247 return false;
248 }
249 }
250 return true;
251 }
252
253 // Coalescing request is not canceled if there are no sub-requests
254 return false;
255
256 }
257
258
259 // ------------------------------------------------------------------------
260 // Object
261 // ------------------------------------------------------------------------
262
263 @Override
264 // All requests have a unique id
265 public int hashCode() {
266 return super.hashCode();
267 }
268
269 @Override
270 public boolean equals(Object other) {
271 if (other instanceof TmfCoalescedDataRequest<?>) {
272 TmfCoalescedDataRequest<?> request = (TmfCoalescedDataRequest<?>) other;
273 return (request.getDataType() == getDataType()) &&
274 (request.getIndex() == getIndex()) &&
275 (request.getNbRequested() == getNbRequested() &&
276 (request.getExecType() == getExecType()));
277 }
278 return false;
279 }
280
281 @Override
282 @SuppressWarnings("nls")
283 public String toString() {
284 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
285 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
286 }
287 }
This page took 0.047318 seconds and 5 git commands to generate.