diff options
Diffstat (limited to 'bundles/org.eclipse.osgi.util/src/org/osgi/util/promise/Promises.java')
-rw-r--r-- | bundles/org.eclipse.osgi.util/src/org/osgi/util/promise/Promises.java | 159 |
1 files changed, 15 insertions, 144 deletions
diff --git a/bundles/org.eclipse.osgi.util/src/org/osgi/util/promise/Promises.java b/bundles/org.eclipse.osgi.util/src/org/osgi/util/promise/Promises.java index a25bdf3c5..650a2b0e2 100644 --- a/bundles/org.eclipse.osgi.util/src/org/osgi/util/promise/Promises.java +++ b/bundles/org.eclipse.osgi.util/src/org/osgi/util/promise/Promises.java @@ -16,20 +16,21 @@ package org.osgi.util.promise; -import static org.osgi.util.promise.PromiseExecutors.defaultExecutors; +import static org.osgi.util.promise.PromiseFactory.defaultFactory; -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. + * <p> + * These methods return Promises which use the default callback executor and + * default scheduled executor. See {@link PromiseFactory} for similar methods + * which use executors other than the default executors. * * @ThreadSafe + * @see PromiseFactory * @author $Id$ */ public class Promises { @@ -45,10 +46,10 @@ public class Promises { * @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) + * @see PromiseFactory#resolved(Object) */ public static <T> Promise<T> resolved(T value) { - return defaultExecutors.resolved(value); + return defaultFactory.resolved(value); } /** @@ -60,10 +61,10 @@ public class Promises { * @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) + * @see PromiseFactory#failed(Throwable) */ public static <T> Promise<T> failed(Throwable failure) { - return defaultExecutors.failed(failure); + return defaultFactory.failed(failure); } /** @@ -91,58 +92,11 @@ public class Promises { * 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() + * @see PromiseFactory#all(Collection) */ 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(); + Collection<Promise<S>> promises) { + return defaultFactory.all(promises); } /** @@ -167,95 +121,12 @@ public class Promises { * 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...) + * @see PromiseFactory#all(Collection) */ @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)); - } - } + return defaultFactory.all(list); } } |