Change ITmfTrace<T extends TmfEvent> to ITmfTrace<T extends ITmfEvent>
[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 * TODO: Implement me. Please.
23 */
24 public class TmfCoalescedDataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
25
26 // ------------------------------------------------------------------------
27 // Attributes
28 // ------------------------------------------------------------------------
29
30 protected Vector<ITmfDataRequest<T>> fRequests = new Vector<ITmfDataRequest<T>>();
31
32 // ------------------------------------------------------------------------
33 // Constructor
34 // ------------------------------------------------------------------------
35
36 /**
37 * Default constructor
38 */
39 public TmfCoalescedDataRequest(Class<T> dataType) {
40 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
41 }
42
43 public TmfCoalescedDataRequest(Class<T> dataType, ExecutionType execType) {
44 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, execType);
45 }
46
47 /**
48 * @param nbRequested
49 */
50 public TmfCoalescedDataRequest(Class<T> dataType, int index) {
51 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
52 }
53
54 public TmfCoalescedDataRequest(Class<T> dataType, int index, ExecutionType execType) {
55 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, execType);
56 }
57
58 /**
59 * @param index
60 * @param nbRequested
61 */
62 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested) {
63 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
64 }
65
66 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, ExecutionType execType) {
67 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, execType);
68 }
69
70 /**
71 * @param index
72 * @param nbRequested
73 * @param blockSize
74 */
75 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize) {
76 super(dataType, index, nbRequested, blockSize, ExecutionType.FOREGROUND);
77 }
78
79 public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize, ExecutionType execType) {
80 super(dataType, index, nbRequested, blockSize, execType);
81 }
82
83 // ------------------------------------------------------------------------
84 // Management
85 // ------------------------------------------------------------------------
86
87 public void addRequest(ITmfDataRequest<T> request) {
88 fRequests.add(request);
89 }
90
91 public boolean isCompatible(ITmfDataRequest<T> request) {
92
93 boolean ok = request.getIndex() == getIndex();
94 ok &= request.getNbRequested() == getNbRequested();
95 ok &= request.getExecType() == getExecType();
96 //ok &= request.getDataType() == getDataType();
97
98 return ok;
99 }
100
101 // ------------------------------------------------------------------------
102 // ITmfDataRequest
103 // ------------------------------------------------------------------------
104
105 @Override
106 public void handleData(T data) {
107 super.handleData(data);
108 // Don't call sub-requests handleData() unless this is a
109 // TmfCoalescedDataRequest; extended classes should call
110 // the sub-requests handleData().
111 if (getClass() == TmfCoalescedDataRequest.class) {
112 for (ITmfDataRequest<T> request : fRequests) {
113 if (!request.isCompleted()) {
114 if (request.getDataType().isInstance(data)) {
115 request.handleData(data);
116 }
117 }
118 }
119 }
120 }
121
122 @Override
123 public void start() {
124 for (ITmfDataRequest<T> request : fRequests) {
125 if (!request.isCompleted()) {
126 request.start();
127 }
128 }
129 super.start();
130 }
131
132 @Override
133 public void done() {
134 for (ITmfDataRequest<T> request : fRequests) {
135 if (!request.isCompleted()) {
136 request.done();
137 }
138 }
139 super.done();
140 }
141
142 @Override
143 public void fail() {
144 for (ITmfDataRequest<T> request : fRequests) {
145 request.fail();
146 }
147 super.fail();
148 }
149
150 @Override
151 public void cancel() {
152 for (ITmfDataRequest<T> request : fRequests) {
153 if (!request.isCompleted()) {
154 request.cancel();
155 }
156 }
157 super.cancel();
158 }
159
160 @Override
161 public boolean isCompleted() {
162 // Firstly, check if coalescing request is completed
163 if (super.isCompleted()) {
164 return true;
165 }
166
167 // Secondly, check if all sub-requests are finished
168 if (fRequests.size() > 0) {
169 // If all sub requests are completed the coalesced request is
170 // treated as completed, too.
171 for (ITmfDataRequest<T> request : fRequests) {
172 if (!request.isCompleted()) {
173 return false;
174 }
175 }
176 return true;
177 }
178
179 // Coalescing request is not finished if there are no sub-requests
180 return false;
181 }
182
183 @Override
184 public boolean isCancelled() {
185 // Firstly, check if coalescing request is canceled
186 if (super.isCancelled()) {
187 return true;
188 }
189
190 // Secondly, check if all sub-requests are canceled
191 if (fRequests.size() > 0) {
192 // If all sub requests are canceled the coalesced request is
193 // treated as completed, too.
194 for (ITmfDataRequest<T> request : fRequests) {
195 if (!request.isCancelled()) {
196 return false;
197 }
198 }
199 return true;
200 }
201
202 // Coalescing request is not canceled if there are no sub-requests
203 return false;
204
205 }
206
207
208 // ------------------------------------------------------------------------
209 // Object
210 // ------------------------------------------------------------------------
211
212 @Override
213 // All requests have a unique id
214 public int hashCode() {
215 return super.hashCode();
216 }
217
218 @Override
219 public boolean equals(Object other) {
220 if (other instanceof TmfCoalescedDataRequest<?>) {
221 TmfCoalescedDataRequest<?> request = (TmfCoalescedDataRequest<?>) other;
222 return (request.getDataType() == getDataType()) &&
223 (request.getIndex() == getIndex()) &&
224 (request.getNbRequested() == getNbRequested() &&
225 (request.getExecType() == getExecType()));
226 }
227 return false;
228 }
229
230 @Override
231 @SuppressWarnings("nls")
232 public String toString() {
233 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
234 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
235 }
236 }
This page took 0.036002 seconds and 5 git commands to generate.