From 503dec1e44a2f06c590637c1f0baf0ba2a3458e8 Mon Sep 17 00:00:00 2001 From: huhaosumail Date: Tue, 25 Jun 2024 17:56:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85tupleMSupplyMostSuccessAsync?= =?UTF-8?q?=E7=9A=84=E7=9B=B8=E5=85=B3=E9=BB=98=E8=AE=A4=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cffu/CompletableFutureUtils.java | 186 ++++++++++++++++-- .../cffu/CompletableFutureUtilsTest.java | 4 + 2 files changed, 171 insertions(+), 19 deletions(-) diff --git a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java index 622ef804..85b4cca5 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -429,12 +429,44 @@ public static CompletableFuture> /** * Returns a new CompletableFuture that is asynchronously completed - * by tasks running in the given Executor with the values obtained by calling the given Suppliers + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) * in the same order of the given Suppliers arguments. *

- * This method is the same as {@link #tupleMSupplyAsync(Executor, Supplier, Supplier)} except for the most-success behavior. + * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. * - * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @return the new CompletableFuture + */ + public static CompletableFuture> tupleMSupplyMostSuccessAsync( + long timeout, TimeUnit unit, + Supplier supplier1, Supplier supplier2) { + return tupleMSupplyMostSuccessAsync(AsyncPoolHolder.ASYNC_POOL, timeout, unit, supplier1, supplier2); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) + * in the same order of the given Suppliers arguments. + *

+ * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. + * + * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type * @return the new CompletableFuture */ public static CompletableFuture> tupleMSupplyMostSuccessAsync( @@ -449,13 +481,48 @@ public static CompletableFuture> tupleMSupplyMostSuccess /** * Returns a new CompletableFuture that is asynchronously completed - * by tasks running in the given Executor with the values obtained by calling the given Suppliers + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) * in the same order of the given Suppliers arguments. *

- * This method is the same as {@link #tupleMSupplyAsync(Executor, Supplier, Supplier, Supplier)} - * except for the most-success behavior. + * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. * - * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @return the new CompletableFuture + */ + public static CompletableFuture> tupleMSupplyMostSuccessAsync( + long timeout, TimeUnit unit, + Supplier supplier1, Supplier supplier2, Supplier supplier3) { + return tupleMSupplyMostSuccessAsync(AsyncPoolHolder.ASYNC_POOL, timeout, unit, supplier1, supplier2, supplier3); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) + * in the same order of the given Suppliers arguments. + *

+ * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. + * + * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type * @return the new CompletableFuture */ public static CompletableFuture> tupleMSupplyMostSuccessAsync( @@ -470,13 +537,52 @@ public static CompletableFuture> tupleMSupplyMos /** * Returns a new CompletableFuture that is asynchronously completed - * by tasks running in the given Executor with the values obtained by calling the given Suppliers + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) * in the same order of the given Suppliers arguments. *

- * This method is the same as {@link #tupleMSupplyAsync(Executor, Supplier, Supplier, Supplier, Supplier)} - * except for the most-success behavior. + * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. * - * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier4 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @return the new CompletableFuture + */ + public static CompletableFuture> tupleMSupplyMostSuccessAsync( + long timeout, TimeUnit unit, Supplier supplier1, + Supplier supplier2, Supplier supplier3, Supplier supplier4) { + return tupleMSupplyMostSuccessAsync(AsyncPoolHolder.ASYNC_POOL, timeout, unit, supplier1, supplier2, supplier3, supplier4); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) + * in the same order of the given Suppliers arguments. + *

+ * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. + * + * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier4 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type * @return the new CompletableFuture */ public static CompletableFuture> tupleMSupplyMostSuccessAsync( @@ -491,13 +597,57 @@ public static CompletableFuture> tupleMS /** * Returns a new CompletableFuture that is asynchronously completed - * by tasks running in the given Executor with the values obtained by calling the given Suppliers + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) * in the same order of the given Suppliers arguments. *

- * This method is the same as {@link #tupleMSupplyAsync(Executor, Supplier, Supplier, Supplier, Supplier, Supplier)} - * except for the most-success behavior. + * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. * - * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier4 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier5 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @return the new CompletableFuture + */ + public static CompletableFuture> tupleMSupplyMostSuccessAsync( + long timeout, TimeUnit unit, Supplier supplier1, + Supplier supplier2, Supplier supplier3, + Supplier supplier4, Supplier supplier5) { + return tupleMSupplyMostSuccessAsync(AsyncPoolHolder.ASYNC_POOL, timeout, unit, supplier1, supplier2, supplier3, supplier4, supplier5); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the most values obtained by calling the given Suppliers + * in the given time({@code timeout}, aka as many results as possible in the given time) + * in the same order of the given Suppliers arguments. + *

+ * If the given supplier is successful in the given time, the return result is the completed value; + * Otherwise the given valueIfNotSuccess. + * + * @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 supplier1 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier4 the supplier returning the value to be used to complete the returned CompletableFuture + * @param supplier5 the supplier returning the value to be used to complete the returned CompletableFuture + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type + * @param the supplier return type * @return the new CompletableFuture */ public static CompletableFuture> tupleMSupplyMostSuccessAsync( @@ -901,8 +1051,7 @@ private static List arrayList(T... elements) { * Returns a cf array whose elements do the result collection. */ private static CompletableFuture[] createResultSetterCfs(CompletionStage[] css, T[] result) { - @SuppressWarnings("unchecked") - final CompletableFuture[] resultSetterCfs = new CompletableFuture[result.length]; + @SuppressWarnings("unchecked") final CompletableFuture[] resultSetterCfs = new CompletableFuture[result.length]; for (int i = 0; i < result.length; i++) { final int index = i; resultSetterCfs[index] = f_toCf(css[index]).thenAccept(v -> result[index] = v); @@ -3279,8 +3428,7 @@ public static CompletableFuture[] toCompletableFutureArray(CompletionStag @Contract(pure = true) public static CompletableFuture[] completableFutureListToArray(List> cfList) { requireNonNull(cfList, "cfList is null"); - @SuppressWarnings("unchecked") - final CompletableFuture[] a = new CompletableFuture[cfList.size()]; + @SuppressWarnings("unchecked") final CompletableFuture[] a = new CompletableFuture[cfList.size()]; return cfList.toArray(a); } diff --git a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java index 9ecf9a45..9590ac62 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -809,12 +809,16 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception { sleep(10); return n + n; }; + assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get()); assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get()); + assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); }