From e314fc0038ab588a39412da72c7b8cc705852870 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 27 Jul 2024 23:01:10 +0800 Subject: [PATCH] refactor: reorder `Cffu.cffuUnwrap` and revise its javadoc; add QA annotations to `CompletableFutureUtils.unwrapCfException`; uniform local var name --- .../src/main/java/io/foldright/cffu/Cffu.java | 34 +++++++++---------- .../cffu/CompletableFutureUtils.java | 27 ++++++++------- .../cffu/CompletableFutureUtilsTest.java | 20 +++++------ 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java index 214f55ea..cc2e8785 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -2844,23 +2844,6 @@ public boolean isMinimalStage() { // region# Inspection Methods //////////////////////////////////////////////////////////////////////////////// - /** - * Returns the underlying CompletableFuture. - *

- * {@link CffuFactory#toCffu(CompletionStage)} is inverse operation to this method. - * {@link CffuFactory#cffuArrayUnwrap(Cffu[])} is the batch operation to this method. - * - * @return the underlying CompletableFuture - * @see CffuFactory#toCffu(CompletionStage) - * @see CffuFactory#cffuArrayUnwrap(Cffu[]) - * @see #toCompletableFuture() - */ - @Contract(pure = true) - @SuppressFBWarnings("EI_EXPOSE_REP") - public CompletableFuture cffuUnwrap() { - return cf; - } - /** * Returns the estimated number of Cffus whose completions are awaiting completion of this Cffu. * This method is designed for use in monitoring system state, not for synchronization control. @@ -2916,6 +2899,23 @@ public void obtrudeException(Throwable ex) { cf.obtrudeException(ex); } + /** + * Returns the underlying CompletableFuture; + * In general, should use {@link #toCompletableFuture()} instead of this method. + *

+ * {@link CffuFactory#cffuArrayUnwrap(Cffu[])} is the batch operation to this method. + * + * @return the underlying CompletableFuture + * @see #toCompletableFuture() + * @see CffuFactory#cffuArrayUnwrap(Cffu[]) + * @see #toCompletableFuture() + */ + @Contract(pure = true) + @SuppressFBWarnings("EI_EXPOSE_REP") + public CompletableFuture cffuUnwrap() { + return cf; + } + /** * Returns a new incomplete Cffu with CompletableFuture of the type to be returned by a CompletionStage method. *

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 c53a5d43..3f377bff 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -3575,7 +3575,8 @@ public static CompletableFuture runAfterEitherSuccessAsync( C catching(C cfThis, Class exceptionType, Function fallback) { requireNonNull(cfThis, "cfThis is null"); requireNonNull(fallback, "fallback is null"); - return (C) cfThis.handle((r, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) + + return (C) cfThis.handle((v, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) ? cfThis : completedFuture(fallback.apply((X) ex)) ).thenCompose(x -> x); } @@ -3617,8 +3618,9 @@ C catchingAsync(C cfThis, Class exceptionType, Function (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) - ? cfThis : cfThis.handleAsync((r1, ex1) -> fallback.apply((X) ex1), executor) + + return (C) cfThis.handle((v, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) + ? cfThis : cfThis.handleAsync((v1, ex1) -> fallback.apply((X) ex1), executor) ).thenCompose(x -> x); } @@ -3657,8 +3659,8 @@ C exceptionallyAsync(C cfThis, Function fn, Executor exe return (C) cfThis.exceptionallyAsync(fn, executor); } // below code is copied from CompletionStage#exceptionallyAsync - return (C) cfThis.handle((r, ex) -> (ex == null) ? cfThis : - cfThis.handleAsync((r1, ex1) -> fn.apply(ex1), executor) + return (C) cfThis.handle((v, ex) -> (ex == null) ? cfThis : + cfThis.handleAsync((v1, ex1) -> fn.apply(ex1), executor) ).thenCompose(x -> x); } @@ -3871,7 +3873,7 @@ private static void completeCf(CompletableFuture cf, Object value, @Null C catchingCompose(C cfThis, Class exceptionType, Function> fallback) { requireNonNull(cfThis, "cfThis is null"); requireNonNull(fallback, "fallback is null"); - return (C) cfThis.handle((r, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) + return (C) cfThis.handle((v, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) ? cfThis : fallback.apply((X) ex) ).thenCompose(x -> x); } @@ -3913,8 +3915,8 @@ public static > C c requireNonNull(cfThis, "cfThis is null"); requireNonNull(fallback, "fallback is null"); requireNonNull(executor, "executor is null"); - return (C) cfThis.handle((r, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) - ? cfThis : cfThis.handleAsync((r1, ex1) -> fallback.apply((X) ex1), executor).thenCompose(x -> x) + return (C) cfThis.handle((v, ex) -> (ex == null || !exceptionType.isAssignableFrom(ex.getClass())) + ? cfThis : cfThis.handleAsync((v1, ex1) -> fallback.apply((X) ex1), executor).thenCompose(x -> x) ).thenCompose(x -> x); } @@ -3935,7 +3937,7 @@ C exceptionallyCompose(C cfThis, Function (ex == null) ? cfThis : fn.apply(ex)).thenCompose(x -> x); + return (C) cfThis.handle((v, ex) -> (ex == null) ? cfThis : fn.apply(ex)).thenCompose(x -> x); } /** @@ -3971,8 +3973,8 @@ C exceptionallyComposeAsync(C cfThis, Function (ex == null) ? cfThis : - cfThis.handleAsync((r1, ex1) -> fn.apply(ex1), executor).thenCompose(x -> x) + return (C) cfThis.handle((v, ex) -> (ex == null) ? cfThis : + cfThis.handleAsync((v1, ex1) -> fn.apply(ex1), executor).thenCompose(x -> x) ).thenCompose(x -> x); } @@ -4454,7 +4456,8 @@ public static CompletableFuture[] completableFutureListToArray(List results = IntStream.range(0, 10).boxed().collect(Collectors.toList()); assertEquals(results, results.stream().map(i -> - orTimeout(createIncompleteFuture(), 100, TimeUnit.MILLISECONDS).handle((r1, ex1) -> { - assertInstanceOf(TimeoutException.class, ex1); + orTimeout(createIncompleteFuture(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + assertInstanceOf(TimeoutException.class, ex); assertTrue(Delayer.atCfDelayerThread()); return i; }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuOrTimeout(createIncompleteFuture(), 100, TimeUnit.MILLISECONDS).handle((r, ex) -> { + cffuOrTimeout(createIncompleteFuture(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertFalse(Delayer.atCfDelayerThread()); assertNotSame(testThread, currentThread()); @@ -1504,7 +1504,7 @@ void test_safeBehavior_orTimeout() { }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuOrTimeout(createIncompleteFuture(), executorService, 100, TimeUnit.MILLISECONDS).handle((r, ex) -> { + cffuOrTimeout(createIncompleteFuture(), executorService, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertFalse(Delayer.atCfDelayerThread()); assertTrue(TestThreadPoolManager.isRunInExecutor(executorService)); @@ -1519,27 +1519,27 @@ void test_safeBehavior_completeOnTimeout() { final List results = IntStream.range(0, 10).boxed().collect(Collectors.toList()); assertEquals(results, results.stream().map(i -> - completeOnTimeout(createIncompleteFuture(), i, 100, TimeUnit.MILLISECONDS).handle((r, ex) -> { + completeOnTimeout(createIncompleteFuture(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertTrue(Delayer.atCfDelayerThread()); - return r; + return v; }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuCompleteOnTimeout(createIncompleteFuture(), i, 100, TimeUnit.MILLISECONDS).handle((r, ex) -> { + cffuCompleteOnTimeout(createIncompleteFuture(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertFalse(Delayer.atCfDelayerThread()); assertNotSame(testThread, currentThread()); - return r; + return v; }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuCompleteOnTimeout(createIncompleteFuture(), i, executorService, 100, TimeUnit.MILLISECONDS).handle((r, ex) -> { + cffuCompleteOnTimeout(createIncompleteFuture(), i, executorService, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertFalse(Delayer.atCfDelayerThread()); assertTrue(TestThreadPoolManager.isRunInExecutor(executorService)); - return r; + return v; }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); }