Skip to main content
summaryrefslogtreecommitdiffstats
blob: d86219b22997bba5ec98ea6cb31d3441a1828e15 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
//#ifdef exercises
package org.eclipse.cdt.examples.dsf.dataviewer;
//#else

//#package org.eclipse.cdt.examples.dsf.dataviewer.answers;
//#endif

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.eclipse.cdt.dsf.concurrent.CountingRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfExecutor;
import org.eclipse.cdt.dsf.concurrent.ImmediateExecutor;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;

/**
 * A data generator which performs a sum computation on data retrieved from a
 * number of other data generators.  The data retrieval from other generators
 * is performed in parallel and the result is calculated once all data is
 * received.
 * <p>
 * This calculating generator does not listen to events from the data
 * providers so it relies on the client to re-retrieve data as needed.
 * </p>
 */
public class AsyncSumDataGenerator implements IDataGenerator {

	/**
	 * DSF executor used to serialize data access within this data generator.
	 */
	final private DsfExecutor fExecutor;

	/**
	 * Data generators to retrieve original data to perform calculations on.
	 */
	final private IDataGenerator[] fDataGenerators;

	public AsyncSumDataGenerator(DsfExecutor executor, IDataGenerator[] generators) {
		fExecutor = executor;
		fDataGenerators = generators;
	}

	@Override
	public void getCount(final DataRequestMonitor<Integer> rm) {
		// Artificially delay the retrieval of the sum data to simulate
		// real processing time.
		fExecutor.schedule(new Runnable() {
			@Override
			public void run() {
				doGetCount(rm);
			}
		}, PROCESSING_DELAY, TimeUnit.MILLISECONDS);
	}

	/**
	 * Performs the actual count retrieval and calculation.
	 * @param rm Request monitor to complete with data.
	 */
	private void doGetCount(final DataRequestMonitor<Integer> rm) {
		// Array to store counts retrieved asynchronously
		final int[] counts = new int[fDataGenerators.length];

		// Counting request monitor is called once all data is retrieved.
		final CountingRequestMonitor crm = new CountingRequestMonitor(fExecutor, rm) {
			@Override
			protected void handleSuccess() {
				// Pick the highest count value.
				Arrays.sort(counts, 0, counts.length - 1);
				int maxCount = counts[counts.length - 1];
				rm.setData(maxCount);
				rm.done();
			};
		};

		// Each call to data generator fills in one value in array.
		for (int i = 0; i < fDataGenerators.length; i++) {
			final int finalI = i;
			fDataGenerators[i].getCount(new DataRequestMonitor<Integer>(ImmediateExecutor.getInstance(), crm) {
				@Override
				protected void handleSuccess() {
					counts[finalI] = getData();
					crm.done();
				}
			});
		}
		crm.setDoneCount(fDataGenerators.length);
	}

	@Override
	public void getValue(final int index, final DataRequestMonitor<Integer> rm) {
		// Artificially delay the retrieval of the sum data to simulate
		// real processing time.
		fExecutor.schedule(new Runnable() {
			@Override
			public void run() {
				doGetValue(index, rm);
			}
		}, PROCESSING_DELAY, TimeUnit.MILLISECONDS);
	}

	/**
	 * Performs the actual value retrieval and calculation.
	 * @param rm Request monitor to complete with data.
	 */
	private void doGetValue(int index, final DataRequestMonitor<Integer> rm) {
		// Array to store counts retrieved asynchronously
		final int[] values = new int[fDataGenerators.length];

		// Counting request monitor is called once all data is retrieved.
		final CountingRequestMonitor crm = new CountingRequestMonitor(fExecutor, rm) {
			@Override
			protected void handleSuccess() {
				// Sum up values in array.
				int sum = 0;
				for (int value : values) {
					sum += value;
				}
				rm.setData(sum);
				rm.done();
			};
		};

		// Each call to data generator fills in one value in array.
		for (int i = 0; i < fDataGenerators.length; i++) {
			final int finalI = i;
			fDataGenerators[i].getValue(index, new DataRequestMonitor<Integer>(ImmediateExecutor.getInstance(), crm) {
				@Override
				protected void handleSuccess() {
					values[finalI] = getData();
					crm.done();
				}
			});
		}
		crm.setDoneCount(fDataGenerators.length);
	}

	@Override
	public void shutdown(RequestMonitor rm) {
		rm.done();
	}

	@Override
	public void addListener(final Listener listener) {
		// no events generated
	}

	@Override
	public void removeListener(Listener listener) {
		// no events generated
	}

}

Back to the top