Skip to content

Commit

Permalink
refactor: move completeCf method to LLCF class 🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Dec 15, 2024
1 parent 4afb620 commit e8d1c32
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3061,18 +3061,13 @@ private static <C extends CompletableFuture<?>> C hopExecutorIfAtCfDelayerThread
CompletableFuture<Object> ret = newIncompleteFuture(cf);

peek0(cf, (v, ex) -> {
if (!atCfDelayerThread()) completeCf(ret, v, ex);
else screenExecutor(executor).execute(() -> completeCf(ret, v, ex));
if (!atCfDelayerThread()) completeCf0(ret, v, ex);
else screenExecutor(executor).execute(() -> completeCf0(ret, v, ex));
}, "handle of executor hop");

return (C) ret;
}

private static void completeCf(CompletableFuture<Object> cf, Object value, @Nullable Throwable ex) {
if (ex == null) cf.complete(value);
else cf.completeExceptionally(ex);
}

// endregion
////////////////////////////////////////////////////////////
// region## Advanced Methods of CompletionStage(compose* and handle-like methods)
Expand Down
19 changes: 17 additions & 2 deletions cffu-core/src/main/java/io/foldright/cffu/LLCF.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.Nullable;
import org.jetbrains.annotations.Contract;

import java.util.ArrayList;
Expand Down Expand Up @@ -127,6 +128,14 @@ static <T> CompletableFuture<T> toNonMinCfCopy0(CompletionStage<? extends T> s)
return isMinStageCf(f) ? f.toCompletableFuture() : IS_JAVA9_PLUS ? f.copy() : f.thenApply(x -> x);
}

public static boolean isMinStageCf(CompletableFuture<?> cf) {
return cf.getClass().equals(MIN_STAGE_CLASS);
}

////////////////////////////////////////////////////////////////////////////////
// region# Low level operations of CompletableFuture
////////////////////////////////////////////////////////////////////////////////

/**
* Peeks the result by executing the given action when the given stage completes, returns the given stage.
* The uncaught exceptions thrown by the action are reported.
Expand Down Expand Up @@ -167,8 +176,14 @@ C peekAsync0(C cfThis, BiConsumer<? super T, ? super Throwable> action, String w
return cfThis;
}

public static boolean isMinStageCf(CompletableFuture<?> cf) {
return cf.getClass().equals(MIN_STAGE_CLASS);
/**
* Completes the given CompletableFuture with the exception(if non-null), otherwise with the value.
* In general, you should NEVER use this method in application codes, use {@link CompletableFuture#complete(Object)}
* or {@link CompletableFuture#completeExceptionally(Throwable)} instead.
*/
static <T> boolean completeCf0(CompletableFuture<? super T> cf, T value, @Nullable Throwable ex) {
if (ex == null) return cf.complete(value);
else return cf.completeExceptionally(ex);
}

// endregion
Expand Down
31 changes: 31 additions & 0 deletions cffu-core/src/test/java/io/foldright/cffu/LLCFTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.foldright.cffu

import io.foldright.test_utils.n
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.booleans.shouldBeFalse
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.future.shouldBeCompleted
import io.kotest.matchers.future.shouldCompleteExceptionallyWith
import io.kotest.matchers.ints.shouldBeExactly
import java.util.concurrent.CompletableFuture

class LLCFTest : FunSpec({
test("completeCf0 - success") {
val cf = CompletableFuture<Int>()
LLCF.completeCf0(cf, n, null).shouldBeTrue()
cf.shouldBeCompleted()
cf.get() shouldBeExactly n


LLCF.completeCf0(cf, n, null).shouldBeFalse()
}

test("completeCf0 - exceptionally") {
val cf = CompletableFuture<Int>()
val ex = RuntimeException("foo")
LLCF.completeCf0(cf, n, ex).shouldBeTrue()
cf.shouldCompleteExceptionallyWith(ex)

LLCF.completeCf0(cf, n, ex).shouldBeFalse()
}
})

0 comments on commit e8d1c32

Please sign in to comment.