/** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null * @since 0.1.0 */ default <V> Function4<A, B, C, D, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (A a, B b, C c, D d) -> after.apply(apply(a, b, c, d)); } }
/** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null * @since 0.1.0 */ default <V> Function4<A, B, C, D, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (A a, B b, C c, D d) -> after.apply(apply(a, b, c, d)); } }
/** * Combines multiple stages by applying a function. * * @param a the first stage. * @param b the second stage. * @param c the third stage. * @param d the fourth stage. * @param function the combining function. * @param <R> the type of the combining function's return value. * @param <A> the type of the first stage's value. * @param <B> the type of the second stage's value. * @param <C> the type of the third stage's value. * @param <D> the type of the fourth stage's value. * @return a stage that completes into the return value of the supplied function. * @since 0.1.0 */ public static <R, A, B, C, D> CompletionStage<R> combine( CompletionStage<A> a, CompletionStage<B> b, CompletionStage<C> c, CompletionStage<D> d, Function4<A, B, C, D, R> function) { final CompletableFuture<A> af = a.toCompletableFuture(); final CompletableFuture<B> bf = b.toCompletableFuture(); final CompletableFuture<C> cf = c.toCompletableFuture(); final CompletableFuture<D> df = d.toCompletableFuture(); return CompletableFuture.allOf(af, bf, cf, df) .thenApply(ignored -> function.apply(af.join(), bf.join(), cf.join(), df.join())); }
/** * Combines multiple stages by applying a function. * * @param a the first stage. * @param b the second stage. * @param c the third stage. * @param d the fourth stage. * @param function the combining function. * @param <R> the type of the combining function's return value. * @param <A> the type of the first stage's value. * @param <B> the type of the second stage's value. * @param <C> the type of the third stage's value. * @param <D> the type of the fourth stage's value. * @return a stage that completes into the return value of the supplied function. * @since 0.1.0 */ public static <R, A, B, C, D> CompletionStage<R> combine( CompletionStage<A> a, CompletionStage<B> b, CompletionStage<C> c, CompletionStage<D> d, Function4<A, B, C, D, R> function) { final CompletableFuture<A> af = a.toCompletableFuture(); final CompletableFuture<B> bf = b.toCompletableFuture(); final CompletableFuture<C> cf = c.toCompletableFuture(); final CompletableFuture<D> df = d.toCompletableFuture(); return CompletableFuture.allOf(af, bf, cf, df) .thenApply(ignored -> function.apply(af.join(), bf.join(), cf.join(), df.join())); }
/** * Composes multiple stages into another stage using a function. * * @param a the first stage. * @param b the second stage. * @param c the third stage. * @param d the fourth stage. * @param function the combining function. * @param <R> the type of the composed {@link CompletionStage}. * @param <A> the type of the first stage's value. * @param <B> the type of the second stage's value. * @param <C> the type of the third stage's value. * @param <D> the type of the fourth stage's value. * @return a stage that is composed from the input stages using the function. * @throws UnsupportedOperationException if any of the {@link CompletionStage}s * do not interoperate with CompletableFuture */ public static <R, A, B, C, D> CompletionStage<R> combineFutures( CompletionStage<A> a, CompletionStage<B> b, CompletionStage<C> c, CompletionStage<D> d, Function4<A, B, C, D, CompletionStage<R>> function) { final CompletableFuture<A> af = a.toCompletableFuture(); final CompletableFuture<B> bf = b.toCompletableFuture(); final CompletableFuture<C> cf = c.toCompletableFuture(); final CompletableFuture<D> df = d.toCompletableFuture(); return CompletableFuture.allOf(af, bf, cf, df) .thenCompose(ignored -> function.apply(af.join(), bf.join(), cf.join(), df.join())); }
/** * Composes multiple stages into another stage using a function. * * @param a the first stage. * @param b the second stage. * @param c the third stage. * @param d the fourth stage. * @param function the combining function. * @param <R> the type of the composed {@link CompletionStage}. * @param <A> the type of the first stage's value. * @param <B> the type of the second stage's value. * @param <C> the type of the third stage's value. * @param <D> the type of the fourth stage's value. * @return a stage that is composed from the input stages using the function. * @throws UnsupportedOperationException if any of the {@link CompletionStage}s * do not interoperate with CompletableFuture */ public static <R, A, B, C, D> CompletionStage<R> combineFutures( CompletionStage<A> a, CompletionStage<B> b, CompletionStage<C> c, CompletionStage<D> d, Function4<A, B, C, D, CompletionStage<R>> function) { final CompletableFuture<A> af = a.toCompletableFuture(); final CompletableFuture<B> bf = b.toCompletableFuture(); final CompletableFuture<C> cf = c.toCompletableFuture(); final CompletableFuture<D> df = d.toCompletableFuture(); return CompletableFuture.allOf(af, bf, cf, df) .thenCompose(ignored -> function.apply(af.join(), bf.join(), cf.join(), df.join())); }