Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / request / TmfCoalescedDataRequest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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.ArrayList;
16 import java.util.List;
17
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;
21
22 /**
23 * The TMF coalesced data request
24 *
25 * @version 1.0
26 * @author Francois Chouinard
27 */
28 public class TmfCoalescedDataRequest extends TmfDataRequest {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 /**
35 * The list of coalesced requests
36 */
37 protected final List<ITmfDataRequest> fRequests = new ArrayList<ITmfDataRequest>();
38
39 // ------------------------------------------------------------------------
40 // Constructor
41 // ------------------------------------------------------------------------
42
43 /**
44 * Request 'n' events of a given type from the given index (given priority).
45 * Events are returned in blocks of the given size.
46 *
47 * @param dataType
48 * The requested data type
49 * @param index
50 * The index of the first event to retrieve. Use '0' to start at
51 * the beginning.
52 * @param nbRequested
53 * The number of events requested. You can use
54 * {@link TmfDataRequest#ALL_DATA} to request all events.
55 * @param priority
56 * The requested execution priority
57 */
58 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType,
59 long index,
60 int nbRequested,
61 ExecutionType priority) {
62 super(ITmfEvent.class, index, nbRequested, priority);
63 }
64
65 // ------------------------------------------------------------------------
66 // Management
67 // ------------------------------------------------------------------------
68
69 /**
70 * Add a request to this one.
71 *
72 * @param request
73 * The request to add
74 */
75 public void addRequest(ITmfDataRequest request) {
76 fRequests.add(request);
77 merge(request);
78 }
79
80 /**
81 * Check if a request is compatible with the current coalesced one
82 *
83 * @param request
84 * The request to verify
85 * @return If the request is compatible, true or false
86 */
87 public boolean isCompatible(ITmfDataRequest request) {
88 if (request.getExecType() == getExecType()) {
89 return overlaps(request);
90 }
91 return false;
92 }
93
94 private boolean overlaps(ITmfDataRequest request) {
95 long start = request.getIndex();
96 long end = start + request.getNbRequested();
97
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));
101 }
102
103 private void merge(ITmfDataRequest request) {
104 long start = request.getIndex();
105 long end = Math.min(start + request.getNbRequested(), TmfDataRequest.ALL_DATA);
106
107 if (start < fIndex) {
108 if (fNbRequested != TmfDataRequest.ALL_DATA) {
109 fNbRequested += (fIndex - start);
110 }
111 fIndex = start;
112 }
113 if ((request.getNbRequested() == TmfDataRequest.ALL_DATA) ||
114 (fNbRequested == TmfDataRequest.ALL_DATA))
115 {
116 fNbRequested = TmfDataRequest.ALL_DATA;
117 } else {
118 fNbRequested = (int) Math.max(end - fIndex, fNbRequested);
119 }
120 }
121
122 /**
123 * @return The list of IDs of the sub-requests
124 */
125 @SuppressWarnings("nls")
126 public String getSubRequestIds() {
127 StringBuffer result = new StringBuffer("[");
128 for (int i = 0; i < fRequests.size(); i++) {
129 if (i != 0) {
130 result.append(", ");
131 }
132 result.append(fRequests.get(i).getRequestId());
133 }
134 result.append("]");
135 return result.toString();
136 }
137
138 // ------------------------------------------------------------------------
139 // ITmfDataRequest
140 // ------------------------------------------------------------------------
141
142 @Override
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);
157 }
158 }
159 }
160 }
161 }
162 }
163
164 @Override
165 public void start() {
166 for (ITmfDataRequest request : fRequests) {
167 if (!request.isCompleted()) {
168 request.start();
169 }
170 }
171 super.start();
172 }
173
174 @Override
175 public void done() {
176 for (ITmfDataRequest request : fRequests) {
177 if (!request.isCompleted()) {
178 request.done();
179 }
180 }
181 super.done();
182 }
183
184 @Override
185 public void fail() {
186 for (ITmfDataRequest request : fRequests) {
187 request.fail();
188 }
189 super.fail();
190 }
191
192 @Override
193 public void cancel() {
194 for (ITmfDataRequest request : fRequests) {
195 if (!request.isCompleted()) {
196 request.cancel();
197 }
198 }
199 super.cancel();
200 }
201
202 @Override
203 public synchronized boolean isCompleted() {
204 // Firstly, check if coalescing request is completed
205 if (super.isCompleted()) {
206 return true;
207 }
208
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()) {
215 return false;
216 }
217 }
218 return true;
219 }
220
221 // Coalescing request is not finished if there are no sub-requests
222 return false;
223 }
224
225 @Override
226 public synchronized boolean isCancelled() {
227 // Firstly, check if coalescing request is canceled
228 if (super.isCancelled()) {
229 return true;
230 }
231
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()) {
238 return false;
239 }
240 }
241 return true;
242 }
243
244 // Coalescing request is not canceled if there are no sub-requests
245 return false;
246
247 }
248
249 // ------------------------------------------------------------------------
250 // Object
251 // ------------------------------------------------------------------------
252
253 @Override
254 // All requests have a unique id
255 public int hashCode() {
256 return super.hashCode();
257 }
258
259 @Override
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()));
267 }
268 return false;
269 }
270
271 @Override
272 @SuppressWarnings("nls")
273 public String toString() {
274 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
275 + "," + getExecType() + "," + getIndex() + "," + getNbRequested() + ","
276 + fRequests.toString() + ")]";
277 }
278 }
This page took 0.053685 seconds and 5 git commands to generate.