42fde555fa6a46bafb16eb0bb6860c170afe1761
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / request / TmfCoalescedEventRequest.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 org.eclipse.linuxtools.internal.tmf.core.Tracer;
16 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
17 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
18 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
19 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
20 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
21 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
22 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
23
24 /**
25 * The TMF coalesced event request
26 *
27 * @version 1.0
28 * @author Francois Chouinard
29 */
30 public class TmfCoalescedEventRequest<T extends ITmfEvent> extends TmfCoalescedDataRequest<T> implements ITmfEventRequest<T> {
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
36 private TmfTimeRange fRange; // The requested events time range
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 TmfCoalescedEventRequest(Class<T> dataType) {
49 this(dataType, TmfTimeRange.ETERNITY, 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 TmfCoalescedEventRequest(Class<T> dataType, ExecutionType priority) {
60 this(dataType, TmfTimeRange.ETERNITY, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
61 }
62
63 /**
64 * Request all the events of a given type for the given time range (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 range the time range of the requested events
69 */
70 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range) {
71 this(dataType, range, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
72 }
73
74 /**
75 * Request all the events of a given type for the given time range (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 range the time range of the requested events
80 * @param priority the requested execution priority
81 */
82 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, ExecutionType priority) {
83 this(dataType, range, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
84 }
85
86 /**
87 * Request 'n' events of a given type from the given time range (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 range the time range of the requested events
92 * @param nbRequested the number of events requested
93 */
94 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested) {
95 this(dataType, range, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
96 }
97
98 /**
99 * Request 'n' events of a given type for the given time range (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 range the time range of the requested events
104 * @param nbRequested the number of events requested
105 * @param priority the requested execution priority
106 */
107 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, ExecutionType priority) {
108 this(dataType, range, nbRequested, DEFAULT_BLOCK_SIZE, priority);
109 }
110
111 /**
112 * Request 'n' events of a given type for the given time range (high priority).
113 * Events are returned in blocks of the given size.
114 *
115 * @param dataType the requested data type
116 * @param range the time range of the requested events
117 * @param nbRequested the number of events requested
118 * @param blockSize the number of events per block
119 */
120 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, int blockSize) {
121 this(dataType, range, 0, nbRequested, blockSize, ExecutionType.FOREGROUND);
122 }
123
124 /**
125 * Request 'n' events of a given type for the given time range (given priority).
126 * Events are returned in blocks of the given size.
127 *
128 * @param dataType the requested data type
129 * @param range the time range of the requested events
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 TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, int blockSize, ExecutionType priority) {
135 this(dataType, range, 0, nbRequested, blockSize, priority);
136 }
137
138 /**
139 * Request 'n' events of a given type for the given time range (given priority).
140 * Events are returned in blocks of the given size.
141 *
142 * @param dataType the requested data type
143 * @param range the time range of the requested events
144 * @param index the index of the first event to retrieve
145 * @param nbRequested the number of events requested
146 * @param blockSize the number of events per block
147 * @param priority the requested execution priority
148 */
149 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, long index, int nbRequested, int blockSize, ExecutionType priority) {
150 super(dataType, index, nbRequested, blockSize, priority);
151 fRange = range;
152
153 if (Tracer.isRequestTraced()) {
154 String type = getClass().getName();
155 type = type.substring(type.lastIndexOf('.') + 1);
156 @SuppressWarnings("nls")
157 String message = "CREATED "
158 + (getExecType() == ITmfDataRequest.ExecutionType.BACKGROUND ? "(BG)" : "(FG)")
159 + " Type=" + type + " Index=" + getIndex() + " NbReq=" + getNbRequested()
160 + " Range=" + getRange()
161 + " DataType=" + getDataType().getSimpleName();
162 Tracer.traceRequest(this, message);
163 }
164 }
165
166 // ------------------------------------------------------------------------
167 // Management
168 // ------------------------------------------------------------------------
169
170 @Override
171 public void addRequest(ITmfDataRequest<T> request) {
172 super.addRequest(request);
173 if (request instanceof ITmfEventRequest<?>) {
174 merge((ITmfEventRequest<T>) request);
175 }
176 }
177
178 @Override
179 public boolean isCompatible(ITmfDataRequest<T> request) {
180 if (request instanceof ITmfEventRequest<?>) {
181 if (super.isCompatible(request)) {
182 return overlaps((ITmfEventRequest<T>) request);
183 }
184 }
185 return false;
186 }
187
188 private boolean overlaps(ITmfEventRequest<T> request) {
189 ITmfTimestamp startTime = request.getRange().getStartTime();
190 ITmfTimestamp endTime = request.getRange().getEndTime();
191 return (startTime.compareTo(endTime) <= 0) && (fRange.getStartTime().compareTo(fRange.getEndTime()) <= 0);
192 }
193
194 private void merge(ITmfEventRequest<T> request) {
195 ITmfTimestamp startTime = request.getRange().getStartTime();
196 ITmfTimestamp endTime = request.getRange().getEndTime();
197 if (!fRange.contains(startTime) && fRange.getStartTime().compareTo(startTime) > 0) {
198 fRange = new TmfTimeRange(startTime, fRange.getEndTime());
199 }
200 if (!fRange.contains(endTime) && fRange.getEndTime().compareTo(endTime) < 0) {
201 fRange = new TmfTimeRange(fRange.getStartTime(), endTime);
202 }
203 }
204
205 // ------------------------------------------------------------------------
206 // ITmfDataRequest
207 // ------------------------------------------------------------------------
208
209 @Override
210 public void handleData(T data) {
211 super.handleData(data);
212 for (ITmfDataRequest<T> request : fRequests) {
213 if (data == null) {
214 request.handleData(null);
215 } else {
216 if (request instanceof TmfEventRequest<?>) {
217 TmfEventRequest<T> req = (TmfEventRequest<T>) request;
218 if (!req.isCompleted() && (getNbRead() > request.getIndex())) {
219 ITmfTimestamp ts = data.getTimestamp();
220 if (req.getRange().contains(ts)) {
221 if (req.getDataType().isInstance(data)) {
222 req.handleData(data);
223 }
224 }
225 }
226 }
227 else {
228 TmfDataRequest<T> req = (TmfDataRequest<T>) request;
229 if (!req.isCompleted()) {
230 if (req.getDataType().isInstance(data)) {
231 req.handleData(data);
232 }
233 }
234 }
235 }
236 }
237 }
238
239 // ------------------------------------------------------------------------
240 // ITmfEventRequest
241 // ------------------------------------------------------------------------
242
243 @Override
244 public TmfTimeRange getRange() {
245 return fRange;
246 }
247
248 @Override
249 public void setStartIndex(int index) {
250 setIndex(index);
251 }
252
253 // ------------------------------------------------------------------------
254 // Object
255 // ------------------------------------------------------------------------
256
257 @Override
258 // All requests have a unique id
259 public int hashCode() {
260 return super.hashCode();
261 }
262
263 @Override
264 public boolean equals(Object other) {
265 if (other instanceof TmfCoalescedEventRequest<?>) {
266 TmfCoalescedEventRequest<?> request = (TmfCoalescedEventRequest<?>) other;
267 return (request.getDataType() == getDataType()) &&
268 (request.getIndex() == getIndex()) &&
269 (request.getNbRequested() == getNbRequested()) &&
270 (request.getRange().equals(getRange()));
271 }
272 if (other instanceof TmfCoalescedDataRequest<?>) {
273 return super.equals(other);
274 }
275 return false;
276 }
277
278 @Override
279 @SuppressWarnings("nls")
280 public String toString() {
281 return "[TmfCoalescedEventRequest(" + getRequestId() + "," + getDataType().getSimpleName()
282 + "," + getRange() + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
283 }
284
285 }
This page took 0.035471 seconds and 4 git commands to generate.