/** * Create a new Deferred with an associated Promise. */ public Deferred() { promise = new PromiseImpl<T>(); }
/** * Create 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 that has been resolved with the specified value. */ public static <T> Promise<T> resolved(T value) { return new PromiseImpl<T>(value, null); }
/** * Create 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 that has been resolved with the specified failure. */ public static <T> Promise<T> failed(Throwable failure) { return new PromiseImpl<T>(null, requireNonNull(failure)); }
/** * {@inheritDoc} */ public Promise<T> recoverWith(Function<Promise<?>, Promise<? extends T>> recovery) { PromiseImpl<T> chained = new PromiseImpl<T>(); RecoverWith<T> recoverWith = new RecoverWith<T>(chained, recovery); then(recoverWith, recoverWith); return chained; }
public Promise<R> call(Promise<T> resolved) throws Exception { return new PromiseImpl<R>(mapper.apply(resolved.getValue()), null); } }
/** * {@inheritDoc} */ public <R> Promise<R> then(Success<? super T, ? extends R> success, Failure failure) { PromiseImpl<R> chained = new PromiseImpl<R>(); onResolve(new Then<R>(chained, success, failure)); return chained; }
/** * {@inheritDoc} */ public Promise<T> recover(Function<Promise<?>, ? extends T> recovery) { PromiseImpl<T> chained = new PromiseImpl<T>(); Recover<T> recover = new Recover<T>(chained, recovery); then(recover, recover); return chained; }
/** * {@inheritDoc} */ public Promise<T> fallbackTo(Promise<? extends T> fallback) { PromiseImpl<T> chained = new PromiseImpl<T>(); FallbackTo<T> fallbackTo = new FallbackTo<T>(chained, fallback); then(fallbackTo, fallbackTo); return chained; }
/** * Resolve this Promise with the specified Promise. * * <p> * If the specified Promise is successfully resolved, this Promise is * resolved with the value of the specified Promise. If the specified * Promise is resolved with a failure, this Promise is resolved with the * failure of the specified Promise. * * @param with A Promise whose value or failure must be used to resolve this * Promise. Must not be {@code null}. * @return A Promise that is resolved only when this Promise is resolved by * the specified Promise. The returned Promise must be successfully * resolved with the value {@code null}, if this Promise was * resolved by the specified Promise. The returned Promise must be * resolved with a failure of {@link IllegalStateException}, if this * Promise was already resolved when the specified Promise was * resolved. */ Promise<Void> resolveWith(Promise<? extends T> with) { PromiseImpl<Void> chained = new PromiseImpl<Void>(); ResolveWith resolveWith = new ResolveWith(chained); with.then(resolveWith, resolveWith); return chained; }
PromiseImpl<List<T>> chained = new PromiseImpl<List<T>>(); All<T> all = new All<T>(chained, list); for (Promise<? extends T> promise : list) {