Skip to content

Commit

Permalink
refactor: change parameter type to CompletableFuture of internal he…
Browse files Browse the repository at this point in the history
…lper method `isMinStageCf` 🧬

safer because only subclass of `CompletableFuture` can be `minimal stage`
  • Loading branch information
oldratlee committed Jun 3, 2024
1 parent a7b09ff commit 3d833c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
8 changes: 3 additions & 5 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,9 @@ public <T> Cffu<T> newIncompleteCffu() {
@Contract(pure = true)
public <T> Cffu<T> toCffu(CompletionStage<T> stage) {
requireNonNull(stage, "stage is null");

if (CompletableFutureUtils.isMinStageCf(stage)) {
return createMin((CompletableFuture<T>) stage);
} else if (stage instanceof CompletableFuture) {
return create((CompletableFuture<T>) stage);
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ private static <T> void fill(CompletionStage<? extends T>[] css,
* IGNORE the compile-time type check.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static <T> CompletableFuture<T> f_cast(CompletableFuture<?> f) {
return (CompletableFuture) f;
private static <T> CompletableFuture<T> f_cast(CompletableFuture<?> cf) {
return (CompletableFuture) cf;
}

/**
Expand Down Expand Up @@ -393,8 +393,8 @@ private static <T> CompletableFuture<T> toNonMinCfCopy(CompletionStage<? extends
return isMinStageCf(f) ? f.toCompletableFuture() : copy(f);
}

static <T> boolean isMinStageCf(CompletionStage<? extends T> s) {
return "java.util.concurrent.CompletableFuture$MinimalStage".equals(s.getClass().getName());
static boolean isMinStageCf(CompletableFuture<?> cf) {
return "java.util.concurrent.CompletableFuture$MinimalStage".equals(cf.getClass().getName());
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1480,8 +1480,7 @@ C exceptionallyAsync(C cf, Function<Throwable, ? extends T> fn, Executor executo
* @return a new CompletableFuture
* @see #cffuOrTimeout(CompletableFuture, Executor, long, TimeUnit)
*/
public static <C extends CompletableFuture<?>> C cffuOrTimeout(
C cf, long timeout, TimeUnit unit) {
public static <C extends CompletableFuture<?>> C cffuOrTimeout(C cf, long timeout, TimeUnit unit) {
return cffuOrTimeout(cf, AsyncPoolHolder.ASYNC_POOL, timeout, unit);
}

Expand Down Expand Up @@ -1900,8 +1899,7 @@ public static CffuState state(Future<?> cf) {
* @param supplier a function returning the value to be used to complete given CompletableFuture
* @return the given CompletableFuture
*/
public static <T, C extends CompletableFuture<? super T>>
C completeAsync(C cf, Supplier<? extends T> supplier) {
public static <T, C extends CompletableFuture<? super T>> C completeAsync(C cf, Supplier<? extends T> supplier) {
return completeAsync(cf, supplier, AsyncPoolHolder.ASYNC_POOL);
}

Expand Down

0 comments on commit 3d833c6

Please sign in to comment.