/** * Chains this promise to the given one. * <p> * Connects both, the successful path as well as the failure handling of this promise to the given one. * * @param promise the promise to be used as completion handler for this. */ public void chain(@Nonnull final Promise<V> promise) { onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { promise.success(value); } @Override public void onFailure(Throwable throwable) { promise.fail(throwable); } }); }
/** * Chains this promise to the given future. * <p> * Connects both, the successful path as well as the failure handling of this promise to the given future. * * @param future the future to be used as completion handler for this. */ public void chain(@Nonnull Future future) { onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { future.success(); } @Override public void onFailure(Throwable throwable) { future.fail(throwable); } }); }
/** * Provides a handler which is invoked when the promise completes. * * @param completionHandler the handler which is invoked on completion (successful or not). * @return <tt>this</tt> for fluent method chaining */ public Promise<V> then(@Nonnull Runnable completionHandler) { return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(@Nullable V value) throws Exception { completionHandler.run(); } @Override public void onFailure(@Nonnull Throwable throwable) throws Exception { completionHandler.run(); } }); }
/** * Provides a handler which is invoked when the promise completes. * <p> * The given handler is either supplied with the success value of the promise, wrapped as optional * or with an empty optional, if the promise failed. * <p> * Note that using this approach, one cannot determine if the promise failed or was completed * with <tt>null</tt>. * * @param completionHandler the handler which is invoked on completion (successful or not). * @return <tt>this</tt> for fluent method chaining */ public Promise<V> then(@Nonnull Consumer<Optional<V>> completionHandler) { return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(@Nullable V value) throws Exception { completionHandler.accept(Optional.ofNullable(value)); } @Override public void onFailure(@Nonnull Throwable throwable) throws Exception { completionHandler.accept(Optional.empty()); } }); }
/** * Adds a completion handler to this promise which only handles the failed completion of the promise. * <p> * If the promise is already completed, the handler is immediately invoked. * * @param failureHandler the handler to be notified once the promise is completed. A promise can notify more than * one handler. * @return <tt>this</tt> for fluent method chaining */ @Nonnull public Promise<V> onFailureCallback(@Nonnull final Callback<Throwable> failureHandler) { logErrors = false; return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { // Not used for failure callbacks } @Override public void onFailure(Throwable throwable) throws Exception { failureHandler.invoke(throwable); } }); }
/** * Chains this promise to the given one, by transforming the result value of this promise using the given mapper. * * @param promise the promise to be used as completion handler for this. * @param mapper the mapper to be used to convert the result of this promise to the value used to the given * promise. * @param <X> type of the value expected by the given promise. */ public <X> void mapChain(@Nonnull final Promise<X> promise, @Nonnull final Function<V, X> mapper) { onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { try { promise.success(mapper.apply(value)); } catch (Exception e) { promise.fail(e); } } @Override public void onFailure(Throwable throwable) { promise.fail(throwable); } }); }
/** * Adds a completion handler to this promise which only handles the successful completion of the promise. * <p> * If the promise is already completed, the handler is immediately invoked. * * @param successHandler the handler to be notified once the promise is completed. A promise can notify more than * one handler. * @return <tt>this</tt> for fluent method chaining */ @Nonnull public Promise<V> onSuccessCallback(@Nonnull final Callback<V> successHandler) { return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { try { successHandler.invoke(value); } catch (Exception t) { fail(t); } } @Override public void onFailure(Throwable throwable) { // Not used for success callbacks } }); }
/** * Adds a completion handler to this promise which only handles the failed completion of the promise. * <p> * If the promise is already completed, the handler is immediately invoked. * * @param failureHandler the handler to be notified once the promise is completed. A promise can notify more than * one handler. * @return <tt>this</tt> for fluent method chaining */ @Nonnull public Promise<V> onFailure(@Nonnull final Consumer<Throwable> failureHandler) { logErrors = false; return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { // will not be invoked } @Override public void onFailure(Throwable throwable) throws Exception { failureHandler.accept(throwable); } }); }
/** * Adds a completion handler to this promise which only handles the successful completion of the promise. * <p> * If the promise is already completed, the handler is immediately invoked. * * @param successHandler the handler to be notified once the promise is completed. A promise can notify more than * one handler. * @return <tt>this</tt> for fluent method chaining */ @Nonnull public Promise<V> onSuccess(@Nonnull final Consumer<V> successHandler) { return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { try { successHandler.accept(value); } catch (Exception t) { fail(t); } } @Override public void onFailure(Throwable throwable) { // Not used for success callbacks } }); }
/** * Forwards failures to the given promise, while sending successful value to the given successHandler. * * @param promise the promise to be supplied with any failure of this promise. * @param successHandler the handler used to process successfully computed values. * @param <X> type of promised value of the given promise. * @return <tt>this</tt> for fluent method chaining */ @Nonnull public <X> Promise<V> failChain(@Nonnull final Promise<X> promise, @Nonnull final Callback<V> successHandler) { return onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { try { successHandler.invoke(value); } catch (Exception e) { promise.fail(e); } } @Override public void onFailure(Throwable throwable) { promise.fail(throwable); } }); }
/** * Uses to result of this promise to generate a new promise using the given mapper. * * @param mapper the mapper to transform the promised value of this promise. * @param <X> the resulting type of the mapper * @return a new promise which will be either contain the mapped value or which fails if either this promise fails * or if the mapper throws an exception. */ @Nonnull public <X> Promise<X> flatMap(@Nonnull final Function<V, Promise<X>> mapper) { final Promise<X> result = new Promise<>(); onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { try { mapper.apply(value).chain(result); } catch (Exception throwable) { result.fail(throwable); } } @Override public void onFailure(Throwable throwable) { result.fail(throwable); } }); return result; }
for (Promise<V> promise : list) { final int currentIndex = index; promise.onComplete(new CompletionHandler<V>() { @Override public void onSuccess(@Nullable V value) throws Exception {
promisesOpen.incrementAndGet(); ((Promise<Object>) promise).onComplete(new CompletionHandler<Object>() { @Override public void onSuccess(@Nullable Object value) throws Exception {
Condition completed = lock.newCondition(); onComplete(new CompletionHandler<V>() { @Override public void onSuccess(@Nullable Object value) throws Exception {