Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 262eabe26ac4b8fee76bd4c3b063c0ef51e83d6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
 * Copyright (c) 2009, 2010 Ericsson
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.tmf.request;

import java.util.Vector;

import org.eclipse.linuxtools.tmf.event.TmfData;

/**
 * <b><u>TmfCoalescedDataRequest</u></b>
 * <p>
 * TODO: Implement me. Please.
 */
public class TmfCoalescedDataRequest<T extends TmfData> extends TmfDataRequest<T> {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

	protected Vector<ITmfDataRequest<T>> fRequests = new Vector<ITmfDataRequest<T>>();

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------

    /**
     * Default constructor
     */
    public TmfCoalescedDataRequest(Class<T> dataType) {
        this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
    }

    public TmfCoalescedDataRequest(Class<T> dataType, ExecutionType execType) {
        this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, execType);
    }

    /**
     * @param nbRequested
     */
    public TmfCoalescedDataRequest(Class<T> dataType, int index) {
        this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
    }

    public TmfCoalescedDataRequest(Class<T> dataType, int index, ExecutionType execType) {
        this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, execType);
    }

    /**
     * @param index
     * @param nbRequested
     */
    public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested) {
        this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
    }

    public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, ExecutionType execType) {
        this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, execType);
    }

    /**
     * @param index
     * @param nbRequested
     * @param blockSize
     */
    public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize) {
        super(dataType, index, nbRequested, blockSize, ExecutionType.FOREGROUND);
    }

    public TmfCoalescedDataRequest(Class<T> dataType, int index, int nbRequested, int blockSize, ExecutionType execType) {
        super(dataType, index, nbRequested, blockSize, execType);
    }

    // ------------------------------------------------------------------------
    // Management
    // ------------------------------------------------------------------------

	public void addRequest(ITmfDataRequest<T> request) {
		fRequests.add(request);
	}

	public boolean isCompatible(ITmfDataRequest<T> request) {

		boolean ok = request.getIndex() == getIndex();
		ok &= request.getNbRequested()  == getNbRequested();
		ok &= request.getExecType()     == getExecType();
		
		return ok;
	}

    // ------------------------------------------------------------------------
    // ITmfDataRequest
    // ------------------------------------------------------------------------

    @Override
	public void handleData(T data) {
		super.handleData(data);
    	// Don't call sub-requests handleData() unless this is a
		// TmfCoalescedDataRequest; extended classes should call
		// the sub-requests handleData().
		if (getClass() == TmfCoalescedDataRequest.class) {
	    	for (ITmfDataRequest<T> request : fRequests) {
	    	    if (!request.isCompleted()) {
	    	        request.handleData(data);
	    	    }
	    	}
		}
    }

    @Override
    public void start() {
        for (ITmfDataRequest<T> request : fRequests) {
            if (!request.isCompleted()) {
                request.start();
            }
        }
        super.start();
    }
    
	@Override
    public void done() {
    	for (ITmfDataRequest<T> request : fRequests) {
    	    if (!request.isCompleted()) {
    	        request.done();
    	    }
    	}
    	super.done();
    }

    @Override
    public void fail() {
    	for (ITmfDataRequest<T> request : fRequests) {
    		request.fail();
    	}
    	super.fail();
    }

    @Override
    public void cancel() {
    	for (ITmfDataRequest<T> request : fRequests) {
    	    if (!request.isCompleted()) {
    	        request.cancel();
    	    }
    	}
    	super.cancel();
    }
    
    @Override
    public boolean isCompleted() {
        // Firstly, check if coalescing request is completed
        if (super.isCompleted()) {
            return true;
        }

        // Secondly, check if all sub-requests are finished
        if (fRequests.size() > 0) {
            // If all sub requests are completed the coalesced request is 
            // treated as completed, too.
            for (ITmfDataRequest<T> request : fRequests) {
                if (!request.isCompleted()) {
                    return false;
                }
            }
            return true;
        }

        // Coalescing request is not finished if there are no sub-requests
        return false;
    }

    @Override
    public boolean isCancelled() {
        // Firstly, check if coalescing request is canceled
        if (super.isCancelled()) {
            return true;
        }

        // Secondly, check if all sub-requests are canceled
        if (fRequests.size() > 0) {
            // If all sub requests are canceled the coalesced request is 
            // treated as completed, too.
            for (ITmfDataRequest<T> request : fRequests) {
                if (!request.isCancelled()) {
                    return false;
                }
            }
            return true;
        }

        // Coalescing request is not canceled if there are no sub-requests
        return false;

    }

    
    // ------------------------------------------------------------------------
    // Object
    // ------------------------------------------------------------------------

    @Override
    // All requests have a unique id
    public int hashCode() {
    	return super.hashCode();
    }

    @Override
    public boolean equals(Object other) {
    	if (other instanceof TmfCoalescedDataRequest<?>) {
    		TmfCoalescedDataRequest<?> request = (TmfCoalescedDataRequest<?>) other;
       		return 	(request.getDataType()    == getDataType())   &&
       				(request.getIndex()       == getIndex())      &&
       				(request.getNbRequested() == getNbRequested() &&
      				(request.getExecType()    == getExecType()));
       	}
       	return false;
    }

    @Override
    @SuppressWarnings("nls")
    public String toString() {
		return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName() 
			+ "," + getIndex() + "," + getNbRequested() + ")]";
    }
}

Back to the top