Skip to content

Commit

Permalink
fix/refactor/cleanup/adding M* methods 🔧 🧪 :
Browse files Browse the repository at this point in the history
- add `Multi-Actions(M*) Methods` with `Executor` parameter in `CffuFactory`
- fix wrong default executor of `tupleM*` methods in `CffuFactory`

- refactor `thenTupleMApplyMostSuccessAsync` methods of `CompletableFutureUtils`

- add missing `@SafeVarargs` annotation
- improve javadoc
- improve code format
  • Loading branch information
oldratlee committed Jun 26, 2024
1 parent cb16f5b commit 0f5e9ee
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 149 deletions.
70 changes: 40 additions & 30 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Cffu<Void> thenRunAsync(Runnable action, Executor executor) {

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the CompletableFuture's default asynchronous execution facility,
* is executed using {@link #defaultExecutor()},
* with the values obtained by calling the given Functions
* (with the given stage's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
Expand All @@ -225,9 +225,10 @@ public Cffu<Void> thenRunAsync(Runnable action, Executor executor) {
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyFastFailAsync(
@SafeVarargs
public final <U> Cffu<List<U>> thenMApplyFastFailAsync(
CompletionStage<? extends T> cf, Function<? super T, ? extends U>... fns) {
return thenMApplyFastFailAsync(cf,fac.defaultExecutor(),fns);
return thenMApplyFastFailAsync(cf, fac.defaultExecutor(), fns);
}

/**
Expand All @@ -244,14 +245,15 @@ public <T, U> Cffu<List<U>> thenMApplyFastFailAsync(
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyFastFailAsync(
@SafeVarargs
public final <U> Cffu<List<U>> thenMApplyFastFailAsync(
CompletionStage<? extends T> cf, Executor executor, Function<? super T, ? extends U>... fns) {
return reset0(CompletableFutureUtils.thenMApplyFastFailAsync(cf, executor, fns));
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the CompletableFuture's default asynchronous execution facility,
* is executed using {@link #defaultExecutor()},
* with the most values obtained by calling the given Functions
* (with the given stage's result as the argument to the given functions)
* in the given time({@code timeout}, aka as many results as possible in the given time)
Expand All @@ -267,10 +269,11 @@ public <T, U> Cffu<List<U>> thenMApplyFastFailAsync(
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyMostSuccessAsync(
@SafeVarargs
public final <U> Cffu<List<U>> thenMApplyMostSuccessAsync(
CompletionStage<? extends T> cf, @Nullable U valueIfNotSuccess,
long timeout, TimeUnit unit, Function<? super T, ? extends U>... fns) {
return thenMApplyMostSuccessAsync(cf, valueIfNotSuccess, fac.defaultExecutor(),timeout, unit, fns);
return thenMApplyMostSuccessAsync(cf, valueIfNotSuccess, fac.defaultExecutor(), timeout, unit, fns);
}

/**
Expand All @@ -291,15 +294,16 @@ public <T, U> Cffu<List<U>> thenMApplyMostSuccessAsync(
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyMostSuccessAsync(
@SafeVarargs
public final <U> Cffu<List<U>> thenMApplyMostSuccessAsync(
CompletionStage<? extends T> cf, @Nullable U valueIfNotSuccess,
Executor executor, long timeout, TimeUnit unit, Function<? super T, ? extends U>... fns) {
return reset0(CompletableFutureUtils.thenMApplyMostSuccessAsync(cf, valueIfNotSuccess, executor, timeout, unit, fns));
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the CompletableFuture's default asynchronous execution facility,
* is executed using {@link #defaultExecutor()},
* with the values obtained by calling the given Functions
* (with the given stage's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
Expand All @@ -308,9 +312,10 @@ public <T, U> Cffu<List<U>> thenMApplyMostSuccessAsync(
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyAsync(
@SafeVarargs
public final <U> Cffu<List<U>> thenMApplyAsync(
CompletionStage<? extends T> cf, Function<? super T, ? extends U>... fns) {
return thenMApplyAsync(cf,fac.defaultExecutor(), fns);
return thenMApplyAsync(cf, fac.defaultExecutor(), fns);
}

/**
Expand All @@ -323,23 +328,24 @@ public <T, U> Cffu<List<U>> thenMApplyAsync(
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyAsync(
@SafeVarargs
public final <U> Cffu<List<U>> thenMApplyAsync(
CompletionStage<? extends T> cf, Executor executor, Function<? super T, ? extends U>... fns) {
return reset0(CompletableFutureUtils.thenMApplyAsync(cf, executor, fns));
}


/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the CompletableFuture's default asynchronous execution facility,
* is executed using {@link #defaultExecutor()},
* with the given stage's result as the argument to the given actions.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
*/
public <T> Cffu<Void> thenMAcceptAsync(
@SafeVarargs
public final Cffu<Void> thenMAcceptAsync(
CompletionStage<? extends T> cf, Consumer<? super T>... actions) {
return thenMAcceptAsync(cf,fac.defaultExecutor(), actions);
return thenMAcceptAsync(cf, fac.defaultExecutor(), actions);
}

/**
Expand All @@ -349,14 +355,15 @@ public <T> Cffu<Void> thenMAcceptAsync(
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
*/
public <T> Cffu<Void> thenMAcceptAsync(
@SafeVarargs
public final Cffu<Void> thenMAcceptAsync(
CompletionStage<? extends T> cf, Executor executor, Consumer<? super T>... actions) {
return reset0(CompletableFutureUtils.thenMAcceptAsync(cf, executor, actions));
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the CompletableFuture's default asynchronous execution facility,
* is executed using {@link #defaultExecutor()},
* with the given stage's result as the argument to the given actions.
* <p>
* This method is the same as {@link #thenMAcceptAsync(CompletionStage, Consumer[])}
Expand All @@ -365,9 +372,10 @@ public <T> Cffu<Void> thenMAcceptAsync(
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
*/
public <T> Cffu<Void> thenMAcceptFastFailAsync(
@SafeVarargs
public final Cffu<Void> thenMAcceptFastFailAsync(
CompletionStage<? extends T> cf, Consumer<? super T>... actions) {
return thenMAcceptFastFailAsync(cf,fac.defaultExecutor(), actions);
return thenMAcceptFastFailAsync(cf, fac.defaultExecutor(), actions);
}

/**
Expand All @@ -380,15 +388,16 @@ public <T> Cffu<Void> thenMAcceptFastFailAsync(
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
*/
public <T> Cffu<Void> thenMAcceptFastFailAsync(
@SafeVarargs
public final Cffu<Void> thenMAcceptFastFailAsync(
CompletionStage<? extends T> cf, Executor executor, Consumer<? super T>... actions) {
return reset0(CompletableFutureUtils.thenMAcceptFastFailAsync(cf, executor, actions));
}


/**
* Returns a new Cffu that, when the given stage completes normally,
* executes the given actions using the CompletableFuture's default asynchronous execution facility.
* executes using {@link #defaultExecutor()},
* <p>
* This method is the same as {@link #thenMRunAsync(CompletionStage, Runnable...)}
* except for the fast-fail behavior.
Expand All @@ -398,7 +407,7 @@ public <T> Cffu<Void> thenMAcceptFastFailAsync(
* @see CompletableFuture#thenRunAsync(Runnable)
*/
public Cffu<Void> thenMRunFastFailAsync(CompletionStage<?> cf, Runnable... actions) {
return thenMRunFastFailAsync(cf,fac.defaultExecutor(), actions);
return thenMRunFastFailAsync(cf, fac.defaultExecutor(), actions);
}

/**
Expand All @@ -419,14 +428,14 @@ public Cffu<Void> thenMRunFastFailAsync(

/**
* Returns a new Cffu that, when the given stage completes normally,
* executes the given actions using the CompletableFuture's default asynchronous execution facility.
* executes the given actions using {@link #defaultExecutor()}.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
* @see CompletableFuture#thenRunAsync(Runnable)
*/
public Cffu<Void> thenMRunAsync(CompletionStage<?> cf, Runnable... actions) {
return thenMRunAsync(cf,fac.defaultExecutor(), actions);
return thenMRunAsync(cf, fac.defaultExecutor(), actions);
}

/**
Expand All @@ -441,7 +450,6 @@ public Cffu<Void> thenMRunAsync(CompletionStage<?> cf, Executor executor, Runnab
return reset0(CompletableFutureUtils.thenMRunAsync(cf, executor, actions));
}


// endregion
////////////////////////////////////////////////////////////
// region## Then-Tuple-Multi-Actions(thenTupleM*) Methods
Expand Down Expand Up @@ -583,7 +591,6 @@ public <U1, U2, U3, U4, U5> Cffu<Tuple5<U1, U2, U3, U4, U5>> thenTupleMApplyFast
return reset0(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, executor, fn1, fn2, fn3, fn4, fn5));
}


/**
* Returns a new Cffu that, when this Cffu completes normally, is executed using the {@link #defaultExecutor()},
* with the values obtained by calling the given Functions
Expand Down Expand Up @@ -612,7 +619,8 @@ public <U1, U2> Cffu<Tuple2<U1, U2>> thenTupleMApplyMostSuccessAsync(
* @return the new Cffu
*/
public <U1, U2> Cffu<Tuple2<U1, U2>> thenTupleMApplyMostSuccessAsync(
Executor executor, long timeout, TimeUnit unit, Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2) {
Executor executor, long timeout, TimeUnit unit,
Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2) {
return reset0(CompletableFutureUtils.thenTupleMApplyMostSuccessAsync(cf, executor, timeout, unit, fn1, fn2));
}

Expand Down Expand Up @@ -662,7 +670,8 @@ public <U1, U2, U3> Cffu<Tuple3<U1, U2, U3>> thenTupleMApplyMostSuccessAsync(
* @return the new Cffu
*/
public <U1, U2, U3, U4> Cffu<Tuple4<U1, U2, U3, U4>> thenTupleMApplyMostSuccessAsync(
long timeout, TimeUnit unit, Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2,
long timeout, TimeUnit unit,
Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2,
Function<? super T, ? extends U3> fn3, Function<? super T, ? extends U4> fn4) {
return thenTupleMApplyMostSuccessAsync(fac.defaultExecutor(), timeout, unit, fn1, fn2, fn3, fn4);
}
Expand All @@ -679,7 +688,8 @@ public <U1, U2, U3, U4> Cffu<Tuple4<U1, U2, U3, U4>> thenTupleMApplyMostSuccessA
* @return the new Cffu
*/
public <U1, U2, U3, U4> Cffu<Tuple4<U1, U2, U3, U4>> thenTupleMApplyMostSuccessAsync(
Executor executor, long timeout, TimeUnit unit, Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2,
Executor executor, long timeout, TimeUnit unit,
Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2,
Function<? super T, ? extends U3> fn3, Function<? super T, ? extends U4> fn4) {
return reset0(CompletableFutureUtils.thenTupleMApplyMostSuccessAsync(cf, executor, timeout, unit, fn1, fn2, fn3, fn4));
}
Expand Down
Loading

0 comments on commit 0f5e9ee

Please sign in to comment.