Skip to content

Commit

Permalink
Cffu新增Then-Multi-Actions(thenM*)/`thenTupleMApplyMostSuccessAsync…
Browse files Browse the repository at this point in the history
…`实现及相关单元测试
  • Loading branch information
huhaosumail authored and oldratlee committed Jun 26, 2024
1 parent 368c04b commit cf09707
Show file tree
Hide file tree
Showing 2 changed files with 473 additions and 6 deletions.
369 changes: 368 additions & 1 deletion cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Contract;

import java.util.List;
import java.util.concurrent.*;
import java.util.function.*;

Expand Down Expand Up @@ -210,7 +211,236 @@ public Cffu<Void> thenRunAsync(Runnable action, Executor executor) {
// region## Then-Multi-Actions(thenM*) Methods
////////////////////////////////////////////////////////////

// TODO: TO BE implemented!!
/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the CompletableFuture's default asynchronous execution facility,
* 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.
* <p>
* This method is the same as {@link #thenMApplyAsync(CompletionStage, Function[])}
* except for the fast-fail behavior.
*
* @param fns the functions to use to compute the values of the returned Cffu
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyFastFailAsync(
CompletionStage<? extends T> cf, Function<? super T, ? extends U>... fns) {
return thenMApplyFastFailAsync(cf,fac.defaultExecutor(),fns);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the given Executor, 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.
* <p>
* This method is the same as {@link #thenMApplyAsync(CompletionStage, Executor, Function[])}
* except for the fast-fail behavior.
*
* @param fns the functions to use to compute the values of the returned Cffu
* @param executor executor
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, 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,
* 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)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* If the given function is successful in the given time, the return result is the completed value;
* Otherwise the given valueIfNotSuccess.
*
* @param valueIfNotSuccess the value to return if not completed successfully
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @param fns the functions to use to compute the values of the returned Cffu
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, 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);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the given Executor, 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)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* If the given function is successful in the given time, the return result is the completed value;
* Otherwise the given valueIfNotSuccess.
*
* @param valueIfNotSuccess the value to return if not completed successfully
* @param executor the executor to use for asynchronous execution
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @param fns the functions to use to compute the values of the returned Cffu
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, 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,
* 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.
*
* @param fns the functions to use to compute the values of the returned Cffu
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, U> Cffu<List<U>> thenMApplyAsync(
CompletionStage<? extends T> cf, Function<? super T, ? extends U>... fns) {
return thenMApplyAsync(cf,fac.defaultExecutor(), fns);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the given Executor, 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.
*
* @param fns the functions to use to compute the values of the returned Cffu
* @param <U> the functions' return type
* @return the new Cffu
*/
public <T, 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,
* 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(
CompletionStage<? extends T> cf, Consumer<? super T>... actions) {
return thenMAcceptAsync(cf,fac.defaultExecutor(), actions);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the given Executor, 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(
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,
* with the given stage's result as the argument to the given actions.
* <p>
* This method is the same as {@link #thenMAcceptAsync(CompletionStage, Consumer[])}
* except for the fast-fail behavior.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
*/
public <T> Cffu<Void> thenMAcceptFastFailAsync(
CompletionStage<? extends T> cf, Consumer<? super T>... actions) {
return thenMAcceptFastFailAsync(cf,fac.defaultExecutor(), actions);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* is executed using the given Executor, with the given stage's result as the argument to the given actions.
* <p>
* This method is the same as {@link #thenMAcceptAsync(CompletionStage, Executor, Consumer[])}
* except for the fast-fail behavior.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
*/
public <T> 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.
* <p>
* This method is the same as {@link #thenMRunAsync(CompletionStage, Runnable...)}
* except for the fast-fail behavior.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
* @see CompletableFuture#thenRunAsync(Runnable)
*/
public Cffu<Void> thenMRunFastFailAsync(CompletionStage<?> cf, Runnable... actions) {
return thenMRunFastFailAsync(cf,fac.defaultExecutor(), actions);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* executes the given actions using the given Executor.
* <p>
* This method is the same as {@link #thenMRunAsync(CompletionStage, Executor, Runnable...)}
* except for the fast-fail behavior.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
* @see CompletableFuture#thenRunAsync(Runnable, Executor)
*/
public Cffu<Void> thenMRunFastFailAsync(
CompletionStage<?> cf, Executor executor, Runnable... actions) {
return reset0(CompletableFutureUtils.thenMRunFastFailAsync(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.
*
* @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);
}

/**
* Returns a new Cffu that, when the given stage completes normally,
* executes the given actions using the given Executor.
*
* @param actions the actions to perform before completing the returned Cffu
* @return the new Cffu
* @see CompletableFuture#thenRunAsync(Runnable, Executor)
*/
public Cffu<Void> thenMRunAsync(CompletionStage<?> cf, Executor executor, Runnable... actions) {
return reset0(CompletableFutureUtils.thenMRunAsync(cf, executor, actions));
}


// endregion
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -353,6 +583,143 @@ 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
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Function, Function)}
* except for the fast-fail behavior.
*
* @return the new Cffu
*/
public <U1, U2> Cffu<Tuple2<U1, U2>> thenTupleMApplyMostSuccessAsync(
long timeout, TimeUnit unit, Function<? super T, ? extends U1> fn1, Function<? super T, ? extends U2> fn2) {
return thenTupleMApplyMostSuccessAsync(fac.defaultExecutor(), timeout, unit, fn1, fn2);
}

/**
* Returns a new Cffu that, when this Cffu completes normally, is executed using the supplied Executor,
* with the values obtained by calling the given Functions
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Executor, Function, Function)}
* except for the fast-fail behavior.
*
* @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) {
return reset0(CompletableFutureUtils.thenTupleMApplyMostSuccessAsync(cf, executor, timeout, unit, fn1, fn2));
}

/**
* 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
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Function, Function, Function)}
* except for the fast-fail behavior.
*
* @return the new Cffu
*/
public <U1, U2, U3> Cffu<Tuple3<U1, U2, U3>> thenTupleMApplyMostSuccessAsync(
long timeout, TimeUnit unit, Function<? super T, ? extends U1> fn1,
Function<? super T, ? extends U2> fn2, Function<? super T, ? extends U3> fn3) {
return thenTupleMApplyMostSuccessAsync(fac.defaultExecutor(), timeout, unit, fn1, fn2, fn3);
}

/**
* Returns a new Cffu that, when this Cffu completes normally, is executed using the supplied Executor,
* with the values obtained by calling the given Functions
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Executor, Function, Function, Function)}
* except for the fast-fail behavior.
*
* @return the new Cffu
*/
public <U1, U2, U3> Cffu<Tuple3<U1, U2, U3>> thenTupleMApplyMostSuccessAsync(
Executor executor, long timeout, TimeUnit unit, Function<? super T, ? extends U1> fn1,
Function<? super T, ? extends U2> fn2, Function<? super T, ? extends U3> fn3) {
return reset0(CompletableFutureUtils.thenTupleMApplyMostSuccessAsync(cf, executor, timeout, unit, fn1, fn2, fn3));
}

/**
* 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
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Function, Function, Function, Function)}
* except for the fast-fail behavior.
*
* @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,
Function<? super T, ? extends U3> fn3, Function<? super T, ? extends U4> fn4) {
return thenTupleMApplyMostSuccessAsync(fac.defaultExecutor(), timeout, unit, fn1, fn2, fn3, fn4);
}

/**
* Returns a new Cffu that, when this Cffu completes normally, is executed using the supplied Executor,
* with the values obtained by calling the given Functions
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Executor, Function, Function, Function, Function)}
* except for the fast-fail behavior.
*
* @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,
Function<? super T, ? extends U3> fn3, Function<? super T, ? extends U4> fn4) {
return reset0(CompletableFutureUtils.thenTupleMApplyMostSuccessAsync(cf, executor, timeout, unit, fn1, fn2, fn3, fn4));
}

/**
* 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
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Function, Function, Function, Function, Function)}
* except for the fast-fail behavior.
*
* @return the new Cffu
*/
public <U1, U2, U3, U4, U5> Cffu<Tuple5<U1, U2, U3, U4, U5>> thenTupleMApplyMostSuccessAsync(
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, Function<? super T, ? extends U5> fn5) {
return thenTupleMApplyMostSuccessAsync(fac.defaultExecutor(), timeout, unit, fn1, fn2, fn3, fn4, fn5);
}

/**
* Returns a new Cffu that, when this Cffu completes normally, is executed using the supplied Executor,
* with the values obtained by calling the given Functions
* (with this Cffu's result as the argument to the given functions)
* in the <strong>same order</strong> of the given Functions arguments.
* <p>
* This method is the same as {@link #thenTupleMApplyAsync(Executor, Function, Function, Function, Function, Function)}
* except for the fast-fail behavior.
*
* @return the new Cffu
*/
public <U1, U2, U3, U4, U5> Cffu<Tuple5<U1, U2, U3, U4, U5>> thenTupleMApplyMostSuccessAsync(
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, Function<? super T, ? extends U5> fn5) {
return reset0(CompletableFutureUtils.thenTupleMApplyMostSuccessAsync(cf, executor, timeout, unit, 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
Loading

0 comments on commit cf09707

Please sign in to comment.