Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
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 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.request; |
8c8bf09f ASL |
14 | |
15 | import java.util.Vector; | |
16 | ||
72f1e62a | 17 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
8c8bf09f ASL |
18 | |
19 | /** | |
20 | * <b><u>TmfCoalescedDataRequest</u></b> | |
21 | * <p> | |
8c8bf09f | 22 | */ |
72f1e62a | 23 | public class TmfCoalescedDataRequest<T extends ITmfEvent> extends TmfDataRequest<T> { |
8c8bf09f ASL |
24 | |
25 | // ------------------------------------------------------------------------ | |
26 | // Attributes | |
27 | // ------------------------------------------------------------------------ | |
28 | ||
0d9a6d76 FC |
29 | /** |
30 | * The list of coalesced requests | |
31 | */ | |
2fb2eb37 | 32 | protected Vector<ITmfDataRequest<T>> fRequests = new Vector<ITmfDataRequest<T>>(); |
8c8bf09f ASL |
33 | |
34 | // ------------------------------------------------------------------------ | |
35 | // Constructor | |
36 | // ------------------------------------------------------------------------ | |
37 | ||
38 | /** | |
0d9a6d76 FC |
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 | |
8c8bf09f ASL |
43 | */ |
44 | public TmfCoalescedDataRequest(Class<T> dataType) { | |
f6b14ce2 | 45 | this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND); |
cb866e08 FC |
46 | } |
47 | ||
0d9a6d76 FC |
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); | |
8c8bf09f ASL |
57 | } |
58 | ||
59 | /** | |
0d9a6d76 FC |
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 | |
8c8bf09f ASL |
65 | */ |
66 | public TmfCoalescedDataRequest(Class<T> dataType, int index) { | |
f6b14ce2 | 67 | this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND); |
cb866e08 FC |
68 | } |
69 | ||
0d9a6d76 FC |
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); | |
8c8bf09f ASL |
80 | } |
81 | ||
82 | /** | |
0d9a6d76 FC |
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 | |
8c8bf09f ASL |
89 | */ |
90 | public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested) { | |
f6b14ce2 | 91 | this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND); |
cb866e08 FC |
92 | } |
93 | ||
0d9a6d76 FC |
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); | |
8c8bf09f ASL |
105 | } |
106 | ||
107 | /** | |
0d9a6d76 FC |
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 | |
8c8bf09f ASL |
115 | */ |
116 | public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize) { | |
f6b14ce2 | 117 | super(dataType, index, nbRequested, blockSize, ExecutionType.FOREGROUND); |
cb866e08 FC |
118 | } |
119 | ||
0d9a6d76 FC |
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); | |
8c8bf09f ASL |
132 | } |
133 | ||
134 | // ------------------------------------------------------------------------ | |
135 | // Management | |
136 | // ------------------------------------------------------------------------ | |
137 | ||
2fb2eb37 | 138 | public void addRequest(ITmfDataRequest<T> request) { |
8c8bf09f ASL |
139 | fRequests.add(request); |
140 | } | |
141 | ||
f9673903 | 142 | public boolean isCompatible(ITmfDataRequest<T> request) { |
8c8bf09f | 143 | |
f9673903 FC |
144 | boolean ok = request.getIndex() == getIndex(); |
145 | ok &= request.getNbRequested() == getNbRequested(); | |
146 | ok &= request.getExecType() == getExecType(); | |
1b70b6dc | 147 | //ok &= request.getDataType() == getDataType(); |
8c8bf09f ASL |
148 | |
149 | return ok; | |
150 | } | |
151 | ||
152 | // ------------------------------------------------------------------------ | |
153 | // ITmfDataRequest | |
154 | // ------------------------------------------------------------------------ | |
155 | ||
156 | @Override | |
f9673903 FC |
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) { | |
c1c69938 | 164 | if (!request.isCompleted()) { |
1b70b6dc PT |
165 | if (request.getDataType().isInstance(data)) { |
166 | request.handleData(data); | |
167 | } | |
c1c69938 | 168 | } |
f9673903 FC |
169 | } |
170 | } | |
8c8bf09f ASL |
171 | } |
172 | ||
c1c69938 FC |
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 | ||
f9673903 FC |
183 | @Override |
184 | public void done() { | |
2fb2eb37 | 185 | for (ITmfDataRequest<T> request : fRequests) { |
c1c69938 FC |
186 | if (!request.isCompleted()) { |
187 | request.done(); | |
188 | } | |
8c8bf09f | 189 | } |
2fb2eb37 | 190 | super.done(); |
8c8bf09f ASL |
191 | } |
192 | ||
193 | @Override | |
f9673903 | 194 | public void fail() { |
2fb2eb37 FC |
195 | for (ITmfDataRequest<T> request : fRequests) { |
196 | request.fail(); | |
8c8bf09f | 197 | } |
2fb2eb37 | 198 | super.fail(); |
8c8bf09f ASL |
199 | } |
200 | ||
201 | @Override | |
f9673903 | 202 | public void cancel() { |
2fb2eb37 | 203 | for (ITmfDataRequest<T> request : fRequests) { |
c1c69938 FC |
204 | if (!request.isCompleted()) { |
205 | request.cancel(); | |
206 | } | |
8c8bf09f | 207 | } |
2fb2eb37 | 208 | super.cancel(); |
8c8bf09f | 209 | } |
c1c69938 FC |
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 | } | |
8c8bf09f | 233 | |
c1c69938 FC |
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 | ||
2fb2eb37 FC |
259 | // ------------------------------------------------------------------------ |
260 | // Object | |
261 | // ------------------------------------------------------------------------ | |
8c8bf09f ASL |
262 | |
263 | @Override | |
2fb2eb37 FC |
264 | // All requests have a unique id |
265 | public int hashCode() { | |
266 | return super.hashCode(); | |
8c8bf09f ASL |
267 | } |
268 | ||
269 | @Override | |
2fb2eb37 FC |
270 | public boolean equals(Object other) { |
271 | if (other instanceof TmfCoalescedDataRequest<?>) { | |
272 | TmfCoalescedDataRequest<?> request = (TmfCoalescedDataRequest<?>) other; | |
cb866e08 FC |
273 | return (request.getDataType() == getDataType()) && |
274 | (request.getIndex() == getIndex()) && | |
275 | (request.getNbRequested() == getNbRequested() && | |
276 | (request.getExecType() == getExecType())); | |
2fb2eb37 FC |
277 | } |
278 | return false; | |
8c8bf09f ASL |
279 | } |
280 | ||
281 | @Override | |
3b38ea61 | 282 | @SuppressWarnings("nls") |
2fb2eb37 FC |
283 | public String toString() { |
284 | return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName() | |
8016d660 | 285 | + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]"; |
8c8bf09f | 286 | } |
8c8bf09f | 287 | } |