Skip to content

Commit

Permalink
refactor: ensure type safety of Cffu; if return type is Cffu, the…
Browse files Browse the repository at this point in the history
… result is not a minimal stage 🦺 🧬

this type safety strategy is consistent with `CompletableFuture`:
if return type is `CompletableFuture`, the result is not a minimal stage
  • Loading branch information
oldratlee committed Jun 7, 2024
1 parent b0efc8e commit e137882
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private <U> Cffu<U> reset0(CompletableFuture<U> cf) {
}

@Contract(pure = true)
private <U> Cffu<U> resetToMin(CompletableFuture<U> cf) {
private <U> CompletionStage<U> resetToMin(CompletableFuture<U> cf) {
return new Cffu<>(fac, true, cf);
}

Expand Down
24 changes: 6 additions & 18 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private <T> Cffu<T> create(CompletableFuture<T> cf) {
}

@Contract(pure = true)
private <T> Cffu<T> createMin(CompletableFuture<T> cf) {
private <T> CompletionStage<T> createMin(CompletableFuture<T> cf) {
return new Cffu<>(this, true, cf);
}

Expand Down Expand Up @@ -245,32 +245,20 @@ public <T> Cffu<T> 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.
* <p>
* <strong>NOTE</strong>, keep input stage unchanged if possible when wrap:<br>
* <ol>
* <li>if input stage is a {@link Cffu}, re-wrapped with the config of
* this {@link CffuFactory} by {@link Cffu#resetCffuFactory(CffuFactory)}.
* <li>if input stage is a CompletableFuture, wrap it by setting it as the underlying cf of returned cffu.
* <li>otherwise use input {@code stage.toCompletableFuture} as the underlying cf of returned cffu.
* </ol>
* 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 <T> Cffu<T> toCffu(CompletionStage<T> stage) {
requireNonNull(stage, "stage is null");
if (stage instanceof CompletableFuture) {
final CompletableFuture<T> cf = (CompletableFuture<T>) stage;
return CompletableFutureUtils.isMinStageCf(cf) ? createMin(cf) : create(cf);
} else if (stage instanceof Cffu) {
return ((Cffu<T>) stage).resetCffuFactory(this);
if (stage instanceof Cffu) {
Cffu<T> f = ((Cffu<T>) stage);
if (f.cffuFactory() == this && !f.isMinimalStage()) return f;
}
return create(stage.toCompletableFuture());
}
Expand Down

0 comments on commit e137882

Please sign in to comment.