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