Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a25bdf3c541ee02e7a179482da40befb3eed6fa6 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) OSGi Alliance (2014, 2017). All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.osgi.util.promise;

import static org.osgi.util.promise.PromiseExecutors.defaultExecutors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.osgi.util.promise.PromiseImpl.Result;

/**
 * Static helper methods for {@link Promise}s.
 * 
 * @ThreadSafe
 * @author $Id$
 */
public class Promises {
	private Promises() {
		// disallow object creation
	}

	/**
	 * Returns a new Promise that has been resolved with the specified value.
	 * 
	 * @param <T> The value type associated with the returned Promise.
	 * @param value The value of the resolved Promise.
	 * @return A new Promise which uses the default callback executor and
	 *         default scheduled executor that has been resolved with the
	 *         specified value.
	 * @see PromiseExecutors#resolved(Object)
	 */
	public static <T> Promise<T> resolved(T value) {
		return defaultExecutors.resolved(value);
	}

	/**
	 * Returns a new Promise that has been resolved with the specified failure.
	 * 
	 * @param <T> The value type associated with the returned Promise.
	 * @param failure The failure of the resolved Promise. Must not be
	 *            {@code null}.
	 * @return A new Promise which uses the default callback executor and
	 *         default scheduled executor that has been resolved with the
	 *         specified failure.
	 * @see PromiseExecutors#failed(Throwable)
	 */
	public static <T> Promise<T> failed(Throwable failure) {
		return defaultExecutors.failed(failure);
	}

	/**
	 * Returns a new Promise that is a latch on the resolution of the specified
	 * Promises.
	 * <p>
	 * The returned Promise acts as a gate and must be resolved after all of the
	 * specified Promises are resolved.
	 * 
	 * @param <T> The value type of the List value associated with the returned
	 *            Promise.
	 * @param <S> A subtype of the value type of the List value associated with
	 *            the returned Promise.
	 * @param promises The Promises which must be resolved before the returned
	 *            Promise must be resolved. Must not be {@code null} and all of
	 *            the elements in the collection must not be {@code null}.
	 * @return A Promise which uses the default callback executor and default
	 *         scheduled executor that is resolved only when all the specified
	 *         Promises are resolved. The returned Promise must be successfully
	 *         resolved with a List of the values in the order of the specified
	 *         Promises if all the specified Promises are successfully resolved.
	 *         The List in the returned Promise is the property of the caller
	 *         and is modifiable. The returned Promise must be resolved with a
	 *         failure of {@link FailedPromisesException} if any of the
	 *         specified Promises are resolved with a failure. The failure
	 *         {@link FailedPromisesException} must contain all of the specified
	 *         Promises which resolved with a failure.
	 * @see #all(Deferred, Collection)
	 */
	public static <T, S extends T> Promise<List<T>> all(Collection<Promise<S>> promises) {
		return all(defaultExecutors.<List<T>> deferred(), promises);
	}

	/**
	 * Resolves the Promise returned by the specified Deferred when the
	 * specified Promises are all resolved.
	 * <p>
	 * The returned Promise acts as a gate and must be resolved after all of the
	 * specified Promises are resolved.
	 * 
	 * @param <T> The value type of the List value associated with the returned
	 *            Promise.
	 * @param <S> A subtype of the value type of the List value associated with
	 *            the returned Promise.
	 * @param deferred The specified Deferred is used to obtain and resolve the
	 *            returned Promise. Must not be {@code null} and must not have
	 *            already resolved the returned Promise.
	 * @param promises The Promises which must be resolved before the returned
	 *            Promise must be resolved. Must not be {@code null} and all of
	 *            the elements in the collection must not be {@code null}.
	 * @return The Promise from the specified Deferred. It is resolved only when
	 *         all the specified Promises are resolved. The returned Promise
	 *         must be successfully resolved with a List of the values in the
	 *         order of the specified Promises if all the specified Promises are
	 *         successfully resolved. The List in the returned Promise is the
	 *         property of the caller and is modifiable. The returned Promise
	 *         must be resolved with a failure of
	 *         {@link FailedPromisesException} if any of the specified Promises
	 *         are resolved with a failure. The failure
	 *         {@link FailedPromisesException} must contain all of the specified
	 *         Promises which resolved with a failure.
	 * @since 1.1
	 * @see PromiseExecutors#deferred()
	 */
	public static <T, S extends T> Promise<List<T>> all(
			Deferred<List<T>> deferred, Collection<Promise<S>> promises) {
		if (promises.isEmpty()) {
			List<T> result = new ArrayList<>();
			deferred.resolve(result);
		} else {
			/* make a copy and capture the ordering */
			List<Promise< ? extends T>> list = new ArrayList<Promise< ? extends T>>(
					promises);
			All<T> all = new All<>(deferred, list);
			for (Promise< ? extends T> promise : list) {
				promise.onResolve(all);
			}
		}
		return deferred.getPromise();
	}

	/**
	 * Returns a new Promise that is a latch on the resolution of the specified
	 * Promises.
	 * <p>
	 * The new Promise acts as a gate and must be resolved after all of the
	 * specified Promises are resolved.
	 * 
	 * @param <T> The value type associated with the specified Promises.
	 * @param promises The Promises which must be resolved before the returned
	 *            Promise must be resolved. Must not be {@code null} and all of
	 *            the arguments must not be {@code null}.
	 * @return A Promise which uses the default callback executor and scheduled
	 *         executor that is resolved only when all the specified Promises
	 *         are resolved. The returned Promise must be successfully resolved
	 *         with a List of the values in the order of the specified Promises
	 *         if all the specified Promises are successfully resolved. The List
	 *         in the returned Promise is the property of the caller and is
	 *         modifiable. The returned Promise must be resolved with a failure
	 *         of {@link FailedPromisesException} if any of the specified
	 *         Promises are resolved with a failure. The failure
	 *         {@link FailedPromisesException} must contain all of the specified
	 *         Promises which resolved with a failure.
	 * @see #all(Deferred, Promise...)
	 */
	@SafeVarargs
	public static <T> Promise<List<T>> all(Promise<? extends T>... promises) {
		@SuppressWarnings("unchecked")
		List<Promise<T>> list = Arrays.asList((Promise<T>[]) promises);
		return all(list);
	}

	/**
	 * Resolves the Promise returned by the specified Deferred when the
	 * specified Promises are all resolved.
	 * <p>
	 * The new Promise acts as a gate and must be resolved after all of the
	 * specified Promises are resolved.
	 * 
	 * @param <T> The value type associated with the specified Promises.
	 * @param deferred The specified Deferred is used to obtain and resolve the
	 *            returned Promise. Must not be {@code null} and must not have
	 *            already resolved the returned Promise.
	 * @param promises The Promises which must be resolved before the returned
	 *            Promise must be resolved. Must not be {@code null} and all of
	 *            the arguments must not be {@code null}.
	 * @return The Promise from the specified Deferred. It is resolved only when
	 *         all the specified Promises are resolved. The returned Promise
	 *         must be successfully resolved with a List of the values in the
	 *         order of the specified Promises if all the specified Promises are
	 *         successfully resolved. The List in the returned Promise is the
	 *         property of the caller and is modifiable. The returned Promise
	 *         must be resolved with a failure of
	 *         {@link FailedPromisesException} if any of the specified Promises
	 *         are resolved with a failure. The failure
	 *         {@link FailedPromisesException} must contain all of the specified
	 *         Promises which resolved with a failure.
	 * @since 1.1
	 * @see PromiseExecutors#deferred()
	 */
	@SafeVarargs
	public static <T> Promise<List<T>> all(Deferred<List<T>> deferred,
			Promise< ? extends T>... promises) {
		@SuppressWarnings("unchecked")
		List<Promise<T>> list = Arrays.asList((Promise<T>[]) promises);
		return all(deferred, list);
	}

	/**
	 * A callback used to resolve a Deferred when the specified list of Promises
	 * are resolved for the {@link Promises#all(Collection)} method.
	 * 
	 * @ThreadSafe
	 */
	private static final class All<T> implements Runnable {
		private final Deferred<List<T>>				chained;
		private final List<Promise<? extends T>>	promises;
		private final AtomicInteger					promiseCount;

		All(Deferred<List<T>> chained, List<Promise< ? extends T>> promises) {
			if (chained.getPromise().isDone()) {
				throw new IllegalStateException("Already resolved");
			}
			this.chained = chained;
			this.promises = promises;
			this.promiseCount = new AtomicInteger(promises.size());
		}

		@Override
		public void run() {
			if (promiseCount.decrementAndGet() != 0) {
				return;
			}
			List<T> value = new ArrayList<>(promises.size());
			List<Promise<?>> failed = new ArrayList<>(promises.size());
			Throwable cause = null;
			for (Promise<? extends T> promise : promises) {
				Result<T> result = PromiseImpl.collect(promise);
				if (result.fail != null) {
					failed.add(promise);
					if (cause == null) {
						cause = result.fail;
					}
				} else {
					value.add(result.value);
				}
			}
			if (failed.isEmpty()) {
				chained.resolve(value);
			} else {
				chained.fail(new FailedPromisesException(failed, cause));
			}
		}
	}
}

Back to the top