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()); }