/** * Return a new {@link DeferredPromiseImpl} using the {@link PromiseFactory} * of this PromiseImpl. * * @return A new DeferredPromiseImpl. * @since 1.1 */ <V> DeferredPromiseImpl<V> deferred() { return new DeferredPromiseImpl<>(factory); }
/** * Return a resolved PromiseImpl if this DeferredPromiseImpl is resolved. * * @return A ResolvedPromiseImpl holding the value of this * DeferredPromiseImpl or a FailedPromiseImpl holding the failure of * this DeferredPromiseImpl or this DeferredPromiseImpl if this * DeferredPromiseImpl is not resolved. */ PromiseImpl<T> orDone() { // ensure latch open before reading state if (!isDone()) { return this; } if (fail == null) { return resolved(value); } return failed(fail); }
/** * Successfully resolve the Promise associated with this Deferred. * * <p> * After the associated Promise is resolved with the specified value, all * registered {@link Promise#onResolve(Runnable) callbacks} are called and * any {@link Promise#then(Success, Failure) chained} Promises are resolved. * This may occur asynchronously to this method. * <p> * Resolving the associated Promise <i>happens-before</i> any registered * callback is called. That is, in a registered callback, * {@link Promise#isDone()} must return {@code true} and * {@link Promise#getValue()} and {@link Promise#getFailure()} must not * block. * * @param value The value of the resolved Promise. * @throws IllegalStateException If the associated Promise was already * resolved. */ public void resolve(T value) { promise.resolve(value, null); }
/** * Returns a new Promise that will hold the result of the specified task. * <p> * The returned Promise uses the callback executor and scheduled executor of * this PromiseFactory object * <p> * The specified task will be executed on the {@link #executor() callback * executor}. * * @param <T> The value type associated with the returned Promise. * @param task The task whose result will be available from the returned * Promise. * @return A new Promise that will hold the result of the specified task. */ public <T> Promise<T> submit(Callable< ? extends T> task) { DeferredPromiseImpl<T> promise = new DeferredPromiseImpl<>(this); Runnable submit = promise.new Submit(task); try { executor().execute(submit); } catch (Exception t) { promise.tryResolve(null, t); } return promise.orDone(); }
if (isDone()) { return false; notifyCallbacks(); // call any registered callbacks return true;
/** * 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) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * {@inheritDoc} */ @Override public <R> Promise<R> then(Success<? super T, ? extends R> success, Failure failure) { DeferredPromiseImpl<R> chained = deferred(); onResolve(chained.new Then<>(this, success, failure)); return chained.orDone(); }
@Override public String toString() { // ensure latch open before reading state if (!isDone()) { return super.toString() + "[unresolved]"; } if (fail == null) { return super.toString() + "[resolved: " + value + "]"; } return super.toString() + "[failed: " + fail + "]"; }
/** * 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) { DeferredPromiseImpl<Void> chained = deferred(); chain(with, chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * Returns a string representation of the associated Promise. * * @return A string representation of the associated Promise. * @since 1.1 */ @Override public String toString() { return promise.toString(); } }
return promise.resolveWith(with);
/** * Returns a new Promise that will hold the result of the specified task. * <p> * The returned Promise uses the callback executor and scheduled executor of * this PromiseFactory object. * <p> * The specified task will be executed on the {@link #executor() callback * executor}. * * @param <T> The value type associated with the returned Promise. * @param task The task whose result will be available from the returned * Promise. * @return A new Promise that will hold the result of the specified task. */ public <T> Promise<T> submit(Callable< ? extends T> task) { DeferredPromiseImpl<T> promise = new DeferredPromiseImpl<>(this); Runnable submit = promise.new Submit(task); try { executor().execute(submit); } catch (Exception t) { promise.tryResolve(null, t); } return promise.orDone(); }
if (isDone()) { return false; notifyCallbacks(); // call any registered callbacks return true;
/** * 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) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * {@inheritDoc} */ @Override public <R> Promise<R> then(Success<? super T, ? extends R> success, Failure failure) { DeferredPromiseImpl<R> chained = deferred(); chain(this, chained.new Then<>(this, success, failure)); return chained.orDone(); }
@Override public String toString() { // ensure latch open before reading state if (!isDone()) { return super.toString() + "[unresolved]"; } if (fail == null) { return super.toString() + "[resolved: " + value + "]"; } return super.toString() + "[failed: " + fail + "]"; }
/** * 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) { DeferredPromiseImpl<Void> chained = deferred(); chain(with, chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * Returns a string representation of the associated Promise. * * @return A string representation of the associated Promise. * @since 1.1 */ @Override public String toString() { return promise.toString(); } }
return promise.resolveWith(with);
/** * Return a resolved PromiseImpl if this DeferredPromiseImpl is resolved. * * @return A ResolvedPromiseImpl holding the value of this * DeferredPromiseImpl or a FailedPromiseImpl holding the failure of * this DeferredPromiseImpl or this DeferredPromiseImpl if this * DeferredPromiseImpl is not resolved. */ PromiseImpl<T> orDone() { // ensure latch open before reading state if (!isDone()) { return this; } if (fail == null) { return resolved(value); } return failed(fail); }