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 1ba59e2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 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
10 changes: 5 additions & 5 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ void test_toCffu__for_factoryMethods_of_Java9() {
shouldNotBeMinimalStage(cf1);

Cffu<Integer> cf2 = cffuFactory.toCffu(CompletableFuture.completedStage(n));
assertTrue(cf2.isMinimalStage());
shouldBeMinimalStage(cf2);
assertFalse(cf2.isMinimalStage());
shouldNotBeMinimalStage(cf2);

Cffu<Object> cf3 = cffuFactory.toCffu(CompletableFuture.failedStage(rte));
assertTrue(cf3.isMinimalStage());
shouldBeMinimalStage(cf3);
assertFalse(cf3.isMinimalStage());
shouldNotBeMinimalStage(cf3);
}

@Test
Expand All @@ -157,7 +157,7 @@ void test_toCffuArray() throws Exception {
Cffu<Integer>[] cffus = cffuFactory.toCffuArray(CompletableFuture.completedStage(n), completedFuture(n));
assertEquals(n, cffus[1].get());

shouldBeMinimalStage(cffus[0]);
shouldNotBeMinimalStage(cffus[0]);
shouldNotBeMinimalStage(cffus[1]);
}

Expand Down
3 changes: 2 additions & 1 deletion cffu-core/src/test/java/io/foldright/cffu/CffuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ void test_cffuUnwrap_9_completedStage() {
CompletionStage<Integer> stage = CompletableFuture.completedStage(n);
Cffu<Integer> cffu = cffuFactory.toCffu(stage);

assertSame(stage, cffu.cffuUnwrap());
assertNotSame(stage, cffu.cffuUnwrap());
assertEquals(n, cffu.join());
}

@Test
Expand Down

0 comments on commit 1ba59e2

Please sign in to comment.