Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 16, 2024
1 parent ec5604e commit 279fe8b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static <T> CompletableFuture<List<T>> mSupplyFastFailAsync(
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @param suppliers the suppliers returning the value to be used to complete the returned CompletableFuture
* @return the new CompletableFuture
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see CompletableFuture#supplyAsync(Supplier)
*/
@SafeVarargs
Expand Down Expand Up @@ -289,7 +289,7 @@ private static <T> CompletableFuture<? extends T>[] wrapSuppliers(
* <p>
* If you need the successful results of given stages in the given time, prefer below methods:
* <ul>
* <li>{@link #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])}
* <li>{@link #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])}
* <li>{@link #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[])}
* <li>{@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)}
* <li>{@link #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage)}
Expand All @@ -303,7 +303,7 @@ private static <T> CompletableFuture<? extends T>[] wrapSuppliers(
* @see #allResultsOf(CompletionStage[])
* @see #allTupleOf(CompletionStage, CompletionStage)
* @see #allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[])
* @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)
* @see #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage)
Expand Down Expand Up @@ -375,7 +375,7 @@ public static <T> CompletableFuture<List<T>> allResultsOf(CompletionStage<? exte
* <p>
* If you need the successful results of given stages in the given time, prefer below methods:
* <ul>
* <li>{@link #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])}
* <li>{@link #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])}
* <li>{@link #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[])}
* <li>{@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)}
* <li>{@link #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage)}
Expand All @@ -389,7 +389,7 @@ public static <T> CompletableFuture<List<T>> allResultsOf(CompletionStage<? exte
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allTupleOfFastFail(CompletionStage, CompletionStage)
* @see #allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[])
* @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)
* @see #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage)
Expand Down Expand Up @@ -467,16 +467,16 @@ public static <T> CompletableFuture<List<T>> allResultsOfFastFail(CompletionStag
* <p>
* If the given stage is successful, its 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 valueIfNotSuccess the value to return if not completed successfully
* @param cfs the stages
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
@SafeVarargs
public static <T> CompletableFuture<List<T>> mostResultsOfSuccess(
long timeout, TimeUnit unit, @Nullable T valueIfNotSuccess, CompletionStage<? extends T>... cfs) {
@Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, CompletionStage<? extends T>... cfs) {
return mostResultsOfSuccess(valueIfNotSuccess, AsyncPoolHolder.ASYNC_POOL, timeout, unit, cfs);
}

Expand Down Expand Up @@ -900,7 +900,7 @@ private static <T> T tupleOf0(Object... elements) {
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given two stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -919,7 +919,7 @@ public static <T1, T2> CompletableFuture<Tuple2<T1, T2>> mostTupleOfSuccess(
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given two stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -938,7 +938,7 @@ public static <T1, T2> CompletableFuture<Tuple2<T1, T2>> mostTupleOfSuccess(
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given three stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -958,7 +958,7 @@ public static <T1, T2, T3> CompletableFuture<Tuple3<T1, T2, T3>> mostTupleOfSucc
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given three stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -977,7 +977,7 @@ public static <T1, T2, T3> CompletableFuture<Tuple3<T1, T2, T3>> mostTupleOfSucc
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given four stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -998,7 +998,7 @@ public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1, T2, T3, T4>> mostTup
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given four stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -1018,7 +1018,7 @@ public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1, T2, T3, T4>> mostTup
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given five stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand All @@ -1039,7 +1039,7 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @return a new CompletableFuture that is completed when the given five stages complete
* @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see #getSuccessNow(CompletableFuture, Object)
*/
@Contract(pure = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,36 +392,36 @@ void test_mostOf() throws Exception {
final CompletableFuture<Integer> incomplete = createIncompleteFuture();

// 0 input cf
assertEquals(0, mostResultsOfSuccess(10, TimeUnit.MILLISECONDS, null).get().size());
assertEquals(0, mostResultsOfSuccess(null, 10, TimeUnit.MILLISECONDS).get().size());

// 1 input cf
assertEquals(Collections.singletonList(n), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, null, completed).get());
null, 10, TimeUnit.MILLISECONDS, completed).get());
assertEquals(Collections.singletonList(n), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, anotherN, completedStage).get());
anotherN, 10, TimeUnit.MILLISECONDS, completedStage).get());

assertEquals(Collections.singletonList(anotherN), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, anotherN, failed).get());
anotherN, 10, TimeUnit.MILLISECONDS, failed).get());
assertEquals(Collections.singletonList(anotherN), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, anotherN, cancelled).get());
anotherN, 10, TimeUnit.MILLISECONDS, cancelled).get());
assertEquals(Collections.singletonList(anotherN), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, anotherN, incomplete).get());
anotherN, 10, TimeUnit.MILLISECONDS, incomplete).get());

// more input cf
assertEquals(Arrays.asList(n, null, null, null), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, null, completed, failed, cancelled, incomplete
null, 10, TimeUnit.MILLISECONDS, completed, failed, cancelled, incomplete
).get());
assertEquals(Arrays.asList(n, anotherN, anotherN, anotherN), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, anotherN, completedStage, failed, cancelled, incomplete
anotherN, 10, TimeUnit.MILLISECONDS, completedStage, failed, cancelled, incomplete
).get());

assertEquals(Arrays.asList(anotherN, anotherN, anotherN), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, anotherN, failed, cancelled, incomplete
anotherN, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete
).get());

// do not wait for failed and cancelled
assertEquals(Arrays.asList(anotherN, anotherN), mostResultsOfSuccess(
10, TimeUnit.DAYS, anotherN, failed, cancelled
anotherN, 10, TimeUnit.DAYS, failed, cancelled
).get());
}

Expand All @@ -431,10 +431,10 @@ void test_mostOf_wontModifyInputCf() throws Exception {
final CompletableFuture<Integer> incomplete2 = createIncompleteFuture();

assertEquals(Collections.singletonList(null), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, null, incomplete
null, 10, TimeUnit.MILLISECONDS, incomplete
).get());
assertEquals(Arrays.asList(null, null), mostResultsOfSuccess(
10, TimeUnit.MILLISECONDS, null, incomplete, incomplete2
null, 10, TimeUnit.MILLISECONDS, incomplete, incomplete2
).get());

assertEquals(CffuState.RUNNING, state(incomplete));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fun <T> Array<out CompletionStage<out T>>.allResultsOfFastFailCompletableFuture(
fun <T> Collection<CompletionStage<out T>>.mostResultsOfSuccessCompletableFuture(
timeout: Long, unit: TimeUnit, valueIfNotSuccess: T
): CompletableFuture<List<T>> =
CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *toTypedArray())
CompletableFutureUtils.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *toTypedArray())

/**
* Returns a new CompletableFuture with the most results in the **same order** of
Expand All @@ -208,7 +208,7 @@ fun <T> Collection<CompletionStage<out T>>.mostResultsOfSuccessCompletableFuture
fun <T> Array<out CompletionStage<out T>>.mostResultsOfSuccessCompletableFuture(
timeout: Long, unit: TimeUnit, valueIfNotSuccess: T
): CompletableFuture<List<T>> =
CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *this)
CompletableFutureUtils.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *this)

/**
* Returns a new CompletableFuture with the most results in the **same order** of
Expand Down

0 comments on commit 279fe8b

Please sign in to comment.