From e1378820ea1efe47422a33a62529ad14ddcd6a7b 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 +++++-------------- 2 files changed, 7 insertions(+), 19 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 7a3a3fe9..9fef6493 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 83ad58c8..402940ff 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()); }