/** * {@inheritDoc} */ @Override public Promise<T> onResolve(Runnable callback) { requireNonNull(callback); if (factory.allowCurrentThread() && isDone()) { try { callback.run(); } catch (Throwable t) { uncaughtException(t); } } else { callbacks.offer(callback); notifyCallbacks(); // call any registered callbacks } return this; }
/** * {@inheritDoc} */ @Override public <R> Promise<R> then(Success<? super T, ? extends R> success) { return then(success, null); }
/** * {@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 <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; }
/** * Call any registered callbacks if this Promise is resolved. */ void notifyCallbacks() { if (!isDone()) { return; // return if not resolved } /* * Note: multiple threads can be in this method removing callbacks from * the queue and executing them, so the order in which callbacks are * executed cannot be specified. */ for (Runnable callback = callbacks.poll(); callback != null; callback = callbacks.poll()) { try { try { factory.executor().execute(callback); } catch (RejectedExecutionException e) { callback.run(); } } catch (Throwable t) { uncaughtException(t); } } }
/** * Call the callback using the caller's thread because the thread pool * rejected the execution. */ @Override public void rejectedExecution(Runnable callback, ThreadPoolExecutor executor) { try { callback.run(); } catch (Throwable t) { uncaughtException(t); } }
@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<S> p : promises) { Result<S> result = PromiseImpl.collect(p); if (result.fail != null) { failed.add(p); if (cause == null) { cause = result.fail; } } else { value.add(result.value); } } if (failed.isEmpty()) { chained.tryResolve(value, null); } else { chained.tryResolve(null, new FailedPromisesException(failed, cause)); } } }
/** * {@inheritDoc} */ @Override public Promise<T> onFailure(Consumer< ? super Throwable> failure) { return onResolve(new OnFailure(failure)); }
/** * {@inheritDoc} */ @Override public Promise<T> onResolve(Runnable callback) { callbacks.offer(callback); notifyCallbacks(); // call any registered callbacks return this; }
/** * Call any registered callbacks if this Promise is resolved. */ void notifyCallbacks() { if (!isDone()) { return; // return if not resolved } /* * Note: multiple threads can be in this method removing callbacks from * the queue and executing them, so the order in which callbacks are * executed cannot be specified. */ for (Runnable callback = callbacks.poll(); callback != null; callback = callbacks.poll()) { try { try { factory.executor().execute(callback); } catch (RejectedExecutionException e) { callback.run(); } } catch (Throwable t) { uncaughtException(t); } } }
/** * Call the callback using the caller's thread because the thread pool * rejected the execution. */ @Override public void rejectedExecution(Runnable callback, ThreadPoolExecutor executor) { try { callback.run(); } catch (Throwable t) { uncaughtException(t); } }
@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<S> p : promises) { Result<S> result = PromiseImpl.collect(p); if (result.fail != null) { failed.add(p); if (cause == null) { cause = result.fail; } } else { value.add(result.value); } } if (failed.isEmpty()) { chained.tryResolve(value, null); } else { chained.tryResolve(null, new FailedPromisesException(failed, cause)); } } }
/** * {@inheritDoc} */ @Override public Promise<T> onSuccess(Consumer< ? super T> success) { return onResolve(new OnSuccess(success)); }
/** * {@inheritDoc} */ @Override public Promise<T> onResolve(Runnable callback) { callbacks.offer(callback); notifyCallbacks(); // call any registered callbacks return this; }
/** * {@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; }
/** * {@inheritDoc} */ @Override public Promise<T> onResolve(Runnable callback) { requireNonNull(callback); if (factory.allowCurrentThread() && isDone()) { try { callback.run(); } catch (Throwable t) { uncaughtException(t); } } else { callbacks.offer(callback); notifyCallbacks(); // call any registered callbacks } return this; }