Skip to content

Commit

Permalink
refactor: CffuFactory use new0() instead of dummy(), less insta…
Browse files Browse the repository at this point in the history
…nce creation and delegation ⚡️
  • Loading branch information
oldratlee committed May 24, 2024
1 parent db4bd22 commit 4b33e95
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,6 @@ public <T> CompletionStage<T> failedStage(Throwable ex) {
// - supplyAsync*
////////////////////////////////////////////////////////////////////////////////

/**
* a completed Cffu with the value {@code null}
*/
@Contract(pure = true)
private Cffu<Void> dummy() {
return completedFuture(null);
}

/**
* Returns a new Cffu that is asynchronously completed by a task running
* in the {@link #defaultExecutor()} after it runs the given action.
Expand All @@ -174,7 +166,7 @@ private Cffu<Void> dummy() {
* @see CompletableFuture#runAsync(Runnable)
*/
public Cffu<Void> runAsync(Runnable action) {
return dummy().thenRunAsync(action);
return runAsync(action, defaultExecutor);
}

/**
Expand All @@ -187,7 +179,7 @@ public Cffu<Void> runAsync(Runnable action) {
* @see CompletableFuture#runAsync(Runnable, Executor)
*/
public Cffu<Void> runAsync(Runnable action, Executor executor) {
return dummy().thenRunAsync(action, executor);
return new0(CompletableFuture.runAsync(action, executor));
}

/**
Expand All @@ -203,7 +195,7 @@ public Cffu<Void> runAsync(Runnable action, Executor executor) {
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`")
@SuppressWarnings("BoundedWildcard")
public <T> Cffu<T> supplyAsync(Supplier<T> supplier) {
return dummy().thenApplyAsync(unused -> supplier.get());
return supplyAsync(supplier, defaultExecutor);
}

/**
Expand All @@ -219,7 +211,7 @@ public <T> Cffu<T> supplyAsync(Supplier<T> supplier) {
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`")
@SuppressWarnings("BoundedWildcard")
public <T> Cffu<T> supplyAsync(Supplier<T> supplier, Executor executor) {
return dummy().thenApplyAsync(unused -> supplier.get(), executor);
return new0(CompletableFuture.supplyAsync(supplier, executor));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 4b33e95

Please sign in to comment.