Re-structure LTTng sub-project as per the Linux Tools guidelines
[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.TmfData;
18
19 /**
20 * <b><u>TmfCoalescedDataRequest</u></b>
21 * <p>
22 * TODO: Implement me. Please.
23 */
24 public class TmfCoalescedDataRequest<T extends TmfData> 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
97 return ok;
98 }
99
100 // ------------------------------------------------------------------------
101 // ITmfDataRequest
102 // ------------------------------------------------------------------------
103
104 @Override
105 public void handleData(T data) {
106 super.handleData(data);
107 // Don't call sub-requests handleData() unless this is a
108 // TmfCoalescedDataRequest; extended classes should call
109 // the sub-requests handleData().
110 if (getClass() == TmfCoalescedDataRequest.class) {
111 for (ITmfDataRequest<T> request : fRequests) {
112 if (!request.isCompleted()) {
113 request.handleData(data);
114 }
115 }
116 }
117 }
118
119 @Override
120 public void start() {
121 for (ITmfDataRequest<T> request : fRequests) {
122 if (!request.isCompleted()) {
123 request.start();
124 }
125 }
126 super.start();
127 }
128
129 @Override
130 public void done() {
131 for (ITmfDataRequest<T> request : fRequests) {
132 if (!request.isCompleted()) {
133 request.done();
134 }
135 }
136 super.done();
137 }
138
139 @Override
140 public void fail() {
141 for (ITmfDataRequest<T> request : fRequests) {
142 request.fail();
143 }
144 super.fail();
145 }
146
147 @Override
148 public void cancel() {
149 for (ITmfDataRequest<T> request : fRequests) {
150 if (!request.isCompleted()) {
151 request.cancel();
152 }
153 }
154 super.cancel();
155 }
156
157 @Override
158 public boolean isCompleted() {
159 // Firstly, check if coalescing request is completed
160 if (super.isCompleted()) {
161 return true;
162 }
163
164 // Secondly, check if all sub-requests are finished
165 if (fRequests.size() > 0) {
166 // If all sub requests are completed the coalesced request is
167 // treated as completed, too.
168 for (ITmfDataRequest<T> request : fRequests) {
169 if (!request.isCompleted()) {
170 return false;
171 }
172 }
173 return true;
174 }
175
176 // Coalescing request is not finished if there are no sub-requests
177 return false;
178 }
179
180 @Override
181 public boolean isCancelled() {
182 // Firstly, check if coalescing request is canceled
183 if (super.isCancelled()) {
184 return true;
185 }
186
187 // Secondly, check if all sub-requests are canceled
188 if (fRequests.size() > 0) {
189 // If all sub requests are canceled the coalesced request is
190 // treated as completed, too.
191 for (ITmfDataRequest<T> request : fRequests) {
192 if (!request.isCancelled()) {
193 return false;
194 }
195 }
196 return true;
197 }
198
199 // Coalescing request is not canceled if there are no sub-requests
200 return false;
201
202 }
203
204
205 // ------------------------------------------------------------------------
206 // Object
207 // ------------------------------------------------------------------------
208
209 @Override
210 // All requests have a unique id
211 public int hashCode() {
212 return super.hashCode();
213 }
214
215 @Override
216 public boolean equals(Object other) {
217 if (other instanceof TmfCoalescedDataRequest<?>) {
218 TmfCoalescedDataRequest<?> request = (TmfCoalescedDataRequest<?>) other;
219 return (request.getDataType() == getDataType()) &&
220 (request.getIndex() == getIndex()) &&
221 (request.getNbRequested() == getNbRequested() &&
222 (request.getExecType() == getExecType()));
223 }
224 return false;
225 }
226
227 @Override
228 @SuppressWarnings("nls")
229 public String toString() {
230 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
231 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
232 }
233 }
This page took 0.03787 seconds and 5 git commands to generate.