Skip to content

Commit

Permalink
refactor: extract safeHandle0 methods to LLCF 🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Dec 15, 2024
1 parent 9e1f914 commit ac1d0fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.function.*;

import static io.foldright.cffu.Delayer.atCfDelayerThread;
import static io.foldright.cffu.ExceptionReporter.reportUncaughtException;
import static io.foldright.cffu.InternalCommonUtils.*;
import static io.foldright.cffu.LLCF.*;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -3061,15 +3060,10 @@ C completeOnTimeout(C cfThis, @Nullable T value, long timeout, TimeUnit unit) {
private static <C extends CompletableFuture<?>> C hopExecutorIfAtCfDelayerThread(C cf, Executor executor) {
CompletableFuture<Object> ret = newIncompleteFuture(cf);

cf.whenComplete((v, ex) -> {
try {
if (!atCfDelayerThread()) completeCf(ret, v, ex);
else screenExecutor(executor).execute(() -> completeCf(ret, v, ex));
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException("handle of executor hop", e);
}
});
safeHandle0(cf, (v, ex) -> {
if (!atCfDelayerThread()) completeCf(ret, v, ex);
else screenExecutor(executor).execute(() -> completeCf(ret, v, ex));
}, "handle of executor hop");

return (C) ret;
}
Expand Down Expand Up @@ -3252,15 +3246,7 @@ C peek(C cfThis, BiConsumer<? super T, ? super Throwable> action) {
requireNonNull(cfThis, "cfThis is null");
requireNonNull(action, "action is null");

cfThis.whenComplete((v, ex) -> {
try {
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException("the action of peek", e);
}
});
return cfThis;
return safeHandle0(cfThis, action, "the action of peek");
}

/**
Expand Down Expand Up @@ -3320,15 +3306,7 @@ C peekAsync(C cfThis, BiConsumer<? super T, ? super Throwable> action, Executor
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

cfThis.whenCompleteAsync((v, ex) -> {
try {
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException("the action of peekAsync", e);
}
}, executor);
return cfThis;
return safeHandleAsync0(cfThis, action, "the action of peekAsync", executor);
}

// endregion
Expand Down
31 changes: 31 additions & 0 deletions cffu-core/src/main/java/io/foldright/cffu/LLCF.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.foldright.cffu;

import org.jetbrains.annotations.Contract;

import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

import static io.foldright.cffu.ExceptionReporter.reportUncaughtException;
import static io.foldright.cffu.InternalCommonUtils.mapArray;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.completedFuture;
Expand Down Expand Up @@ -111,6 +115,33 @@ static <T> CompletableFuture<T> toNonMinCf0(CompletionStage<? extends T> s) {
return isMinStageCf(f) ? f.toCompletableFuture() : f;
}

@Contract("_, _, _ -> param1")
static <T, C extends CompletionStage<? extends T>>
C safeHandle0(C cfThis, BiConsumer<? super T, ? super Throwable> action, String where) {
cfThis.whenComplete((v, ex) -> {
try {
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException(where, e);
}
});
return cfThis;
}

@Contract("_, _, _, _ -> param1")
static <T, C extends CompletionStage<? extends T>>
C safeHandleAsync0(C cfThis, BiConsumer<? super T, ? super Throwable> action, String where, Executor executor) {
cfThis.whenCompleteAsync((v, ex) -> {
try {
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException(where, e);
}
}, executor);
return cfThis;
}
/**
* Converts CompletionStage to a non-minimal-stage CompletableFuture copy. This method is type safe.
* <p>
Expand Down

0 comments on commit ac1d0fe

Please sign in to comment.