From 9257996b823297f71a25bdfcaa46d412406636f2 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sun, 12 May 2024 20:19:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20change=20`exceptionallyAsync/except?= =?UTF-8?q?ionallyCompose*`=20methods=20with=20more=20generic=20type(`Comp?= =?UTF-8?q?letionStage`)=20in=20`CompletableFutureUtils`=20=F0=9F=A7=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cffu/CompletableFutureUtils.java | 50 ++++++++++--------- .../cffu/CompletableFutureUtilsTest.java | 40 ++++++++++++--- 2 files changed, 59 insertions(+), 31 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 f84bfe71..ef300063 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -941,8 +941,8 @@ public static CompletableFuture applyToEitherSuccessAsync( * @see CompletionStage#whenComplete(BiConsumer) * @see java.util.stream.Stream#peek(Consumer) */ - public static > C peek( - C cf, BiConsumer action) { + public static > + C peek(C cf, BiConsumer action) { cf.whenComplete(action); return cf; } @@ -965,8 +965,8 @@ public static > C peek( * @see CompletionStage#whenCompleteAsync(BiConsumer) * @see java.util.stream.Stream#peek(Consumer) */ - public static > C peekAsync( - C cf, BiConsumer action) { + public static > + C peekAsync(C cf, BiConsumer action) { cf.whenCompleteAsync(action); return cf; } @@ -988,8 +988,8 @@ public static > C peekAsync( * @see CompletionStage#whenCompleteAsync(BiConsumer, Executor) * @see java.util.stream.Stream#peek(Consumer) */ - public static > C peekAsync( - C cf, BiConsumer action, Executor executor) { + public static > + C peekAsync(C cf, BiConsumer action, Executor executor) { cf.whenCompleteAsync(action, executor); return cf; } @@ -1109,8 +1109,8 @@ public static Executor delayedExecutor(long delay, TimeUnit unit, Executor execu * if given CompletionStage completed exceptionally * @return the new CompletableFuture */ - public static CompletableFuture exceptionallyAsync( - CompletableFuture cf, Function fn) { + public static > + C exceptionallyAsync(C cf, Function fn) { return exceptionallyAsync(cf, fn, AsyncPoolHolder.ASYNC_POOL); } @@ -1124,15 +1124,16 @@ public static CompletableFuture exceptionallyAsync( * @param executor the executor to use for asynchronous execution * @return the new CompletableFuture */ - public static CompletableFuture exceptionallyAsync( - CompletableFuture cf, Function fn, Executor executor) { + @SuppressWarnings("unchecked") + public static > + C exceptionallyAsync(C cf, Function fn, Executor executor) { if (IS_JAVA12_PLUS) { - return cf.exceptionallyAsync(fn, executor); + return (C) cf.exceptionallyAsync(fn, executor); } requireNonNull(fn, "fn is null"); requireNonNull(executor, "executor is null"); // below code is copied from CompletionStage#exceptionallyAsync - return cf.handle((r, ex) -> (ex == null) ? cf : + return (C) cf.handle((r, ex) -> (ex == null) ? cf : cf.handleAsync((r1, ex1) -> fn.apply(ex1), executor) ).thenCompose(x -> x); } @@ -1194,14 +1195,15 @@ C completeOnTimeout(C cf, @Nullable T value, long timeout, TimeUnit unit) { * if given CompletionStage completed exceptionally * @return the new CompletableFuture */ - public static CompletableFuture exceptionallyCompose( - CompletableFuture cf, Function> fn) { + @SuppressWarnings({"unchecked", "rawtypes"}) + public static > + C exceptionallyCompose(C cf, Function> fn) { if (IS_JAVA12_PLUS) { - return cf.exceptionallyCompose(fn); + return (C) cf.exceptionallyCompose((Function) fn); } requireNonNull(fn, "fn is null"); // below code is copied from CompletionStage.exceptionallyCompose - return cf.handle((r, ex) -> (ex == null) ? cf : fn.apply(ex)).thenCompose(x -> x); + return (C) cf.handle((r, ex) -> (ex == null) ? cf : fn.apply(ex)).thenCompose(x -> x); } /** @@ -1213,8 +1215,8 @@ public static CompletableFuture exceptionallyCompose( * if given CompletionStage completed exceptionally * @return the new CompletableFuture */ - public static CompletableFuture exceptionallyComposeAsync( - CompletableFuture cf, Function> fn) { + public static > + C exceptionallyComposeAsync(C cf, Function> fn) { return exceptionallyComposeAsync(cf, fn, AsyncPoolHolder.ASYNC_POOL); } @@ -1227,15 +1229,16 @@ public static CompletableFuture exceptionallyComposeAsync( * @param executor the executor to use for asynchronous execution * @return the new CompletableFuture */ - public static CompletableFuture exceptionallyComposeAsync( - CompletableFuture cf, Function> fn, Executor executor) { + @SuppressWarnings({"unchecked", "rawtypes"}) + public static > + C exceptionallyComposeAsync(C cf, Function> fn, Executor executor) { if (IS_JAVA12_PLUS) { - return cf.exceptionallyComposeAsync(fn, executor); + return (C) cf.exceptionallyComposeAsync((Function) fn, executor); } requireNonNull(fn, "fn is null"); requireNonNull(executor, "executor is null"); // below code is copied from CompletionStage.exceptionallyComposeAsync - return cf.handle((r, ex) -> (ex == null) ? cf : + return (C) cf.handle((r, ex) -> (ex == null) ? cf : cf.handleAsync((r1, ex1) -> fn.apply(ex1), executor).thenCompose(x -> x) ).thenCompose(x -> x); } @@ -1405,7 +1408,8 @@ public static CffuState state(CompletableFuture cf) { * @param supplier a function returning the value to be used to complete given CompletableFuture * @return the given CompletableFuture */ - public static > C completeAsync(C cf, Supplier supplier) { + public static > + C completeAsync(C cf, Supplier supplier) { return completeAsync(cf, supplier, AsyncPoolHolder.ASYNC_POOL); } 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 e25691f8..43c37fd3 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; import java.util.Arrays; import java.util.Collections; @@ -1029,27 +1031,49 @@ void test_completableFutureListToArray() { //////////////////////////////////////////////////////////////////////////////// @Test - @SuppressWarnings("UnnecessaryLocalVariable") - void checkTypeParameterDeclaration() throws Exception { + @SuppressWarnings({"UnnecessaryLocalVariable", "unused"}) + void checkTypeParameterDeclaration_peek_completeAsync() throws Exception { final CompletableFuture f = completedFuture(42); - final CompletableFuture fe = f; + final CompletableFuture fs = f; + final CompletableFuture fq = f; + final BiConsumer c = (v, ex) -> { }; CompletableFutureUtils.peek(fe, c).get(); CompletableFutureUtils.peekAsync(fe, c).get(); CompletableFutureUtils.peekAsync(fe, c, executorService).get(); - final CompletableFuture fs = f; final Supplier s = () -> 0; fs.complete(0); CompletableFutureUtils.completeAsync(fs, s).complete(1); CompletableFutureUtils.completeAsync(fs, s, executorService).complete(1); - CompletableFuture fq = f; - orTimeout(fq, 1, TimeUnit.MILLISECONDS); - orTimeout(fs, 1, TimeUnit.MILLISECONDS); - orTimeout(fe, 1, TimeUnit.MILLISECONDS); + CompletableFuture ffe = orTimeout(fe, 1, TimeUnit.MILLISECONDS); + CompletableFuture ffs = orTimeout(fs, 1, TimeUnit.MILLISECONDS); + CompletableFuture ffq = orTimeout(fq, 1, TimeUnit.MILLISECONDS); + } + + @Test + @EnabledForJreRange(min = JRE.JAVA_19) + @SuppressWarnings({"UnnecessaryLocalVariable", "unused", "RedundantThrows"}) + void checkTypeParameterDeclaration_resultNow() throws Exception { + final CompletableFuture f = completedFuture(42); + final CompletableFuture fe = f; + final CompletableFuture fs = f; + final CompletableFuture fq = f; + + Integer i = f.resultNow(); + Integer ii = resultNow(f); + + Integer ie = fe.resultNow(); + Integer iie = resultNow(fe); + + Object is = fs.resultNow(); + Object iis = resultNow(fs); + + Object iq = fq.resultNow(); + Object iiq = resultNow(fq); } ////////////////////////////////////////////////////////////////////////////////