From 1ba59e20a6dc80f2feb6f94d1a7bcd9877175b07 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sun, 2 Jun 2024 20:18:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20ensure=20type=20safety=20of=20`Cffu?= =?UTF-8?q?`;=20if=20return=20type=20is=20`Cffu`,=20the=20result=20is=20no?= =?UTF-8?q?t=20a=20minimal=20stage=20=F0=9F=A6=BA=20=F0=9F=A7=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this type safety strategy is consistent with `CompletableFuture`: if return type is `CompletableFuture`, the result is not a minimal stage --- .../src/main/java/io/foldright/cffu/Cffu.java | 2 +- .../java/io/foldright/cffu/CffuFactory.java | 24 +++++-------------- .../io/foldright/cffu/CffuFactoryTest.java | 10 ++++---- .../test/java/io/foldright/cffu/CffuTest.java | 3 ++- 4 files changed, 14 insertions(+), 25 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 7a3a3fe94..9fef6493b 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -46,7 +46,7 @@ private Cffu reset0(CompletableFuture cf) { } @Contract(pure = true) - private Cffu resetToMin(CompletableFuture cf) { + private CompletionStage resetToMin(CompletableFuture cf) { return new Cffu<>(fac, true, cf); } diff --git a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java index 83ad58c81..402940ff1 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -73,7 +73,7 @@ private Cffu create(CompletableFuture cf) { } @Contract(pure = true) - private Cffu createMin(CompletableFuture cf) { + private CompletionStage createMin(CompletableFuture cf) { return new Cffu<>(this, true, cf); } @@ -245,32 +245,20 @@ public Cffu newIncompleteCffu() { } /** - * Wrap an existed {@link CompletableFuture} / {@link CompletionStage} / {@link Cffu} to {@link Cffu}. - * For {@link CompletableFuture} class instances, - * {@link Cffu#cffuUnwrap()} is the inverse operation to this method. - *

- * NOTE, keep input stage unchanged if possible when wrap:
- *

    - *
  1. if input stage is a {@link Cffu}, re-wrapped with the config of - * this {@link CffuFactory} by {@link Cffu#resetCffuFactory(CffuFactory)}. - *
  2. if input stage is a CompletableFuture, wrap it by setting it as the underlying cf of returned cffu. - *
  3. otherwise use input {@code stage.toCompletableFuture} as the underlying cf of returned cffu. - *
+ * Returns a Cffu maintaining the same completion properties as this stage and this {@code CffuFactory} config. + * If this stage is already a Cffu and have the same {@code CffuFactory}, this method may return this stage itself. * * @throws NullPointerException if the given stage is null * @see #toCffuArray(CompletionStage[]) * @see CompletionStage#toCompletableFuture() - * @see Cffu#cffuUnwrap() * @see Cffu#resetCffuFactory(CffuFactory) */ @Contract(pure = true) public Cffu toCffu(CompletionStage stage) { requireNonNull(stage, "stage is null"); - if (stage instanceof CompletableFuture) { - final CompletableFuture cf = (CompletableFuture) stage; - return CompletableFutureUtils.isMinStageCf(cf) ? createMin(cf) : create(cf); - } else if (stage instanceof Cffu) { - return ((Cffu) stage).resetCffuFactory(this); + if (stage instanceof Cffu) { + Cffu f = ((Cffu) stage); + if (f.cffuFactory() == this && !f.isMinimalStage()) return f; } return create(stage.toCompletableFuture()); } diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java index f5d501c61..3dfa535c1 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -143,12 +143,12 @@ void test_toCffu__for_factoryMethods_of_Java9() { shouldNotBeMinimalStage(cf1); Cffu cf2 = cffuFactory.toCffu(CompletableFuture.completedStage(n)); - assertTrue(cf2.isMinimalStage()); - shouldBeMinimalStage(cf2); + assertFalse(cf2.isMinimalStage()); + shouldNotBeMinimalStage(cf2); Cffu cf3 = cffuFactory.toCffu(CompletableFuture.failedStage(rte)); - assertTrue(cf3.isMinimalStage()); - shouldBeMinimalStage(cf3); + assertFalse(cf3.isMinimalStage()); + shouldNotBeMinimalStage(cf3); } @Test @@ -157,7 +157,7 @@ void test_toCffuArray() throws Exception { Cffu[] cffus = cffuFactory.toCffuArray(CompletableFuture.completedStage(n), completedFuture(n)); assertEquals(n, cffus[1].get()); - shouldBeMinimalStage(cffus[0]); + shouldNotBeMinimalStage(cffus[0]); shouldNotBeMinimalStage(cffus[1]); } diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java index 50b79d237..3a1dd4568 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java @@ -338,7 +338,8 @@ void test_cffuUnwrap_9_completedStage() { CompletionStage stage = CompletableFuture.completedStage(n); Cffu cffu = cffuFactory.toCffu(stage); - assertSame(stage, cffu.cffuUnwrap()); + assertNotSame(stage, cffu.cffuUnwrap()); + assertEquals(n, cffu.join()); } @Test