Skip to content

Commit

Permalink
implement ManagedCompletableFuture supplyAsync and thenCombine
Browse files Browse the repository at this point in the history
  • Loading branch information
aubi authored and breakponchito committed May 13, 2022
1 parent 7ab8afc commit 39b723b
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -77,7 +78,26 @@ public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn) {
}

public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier, ManagedExecutorService executor) {
return CompletableFuture.supplyAsync(supplier, executor);
ManagedCompletableFuture<T> managedFuture = new ManagedCompletableFuture<>(executor);
executor.execute(() -> {
try {
managedFuture.complete(supplier.get());
} catch (Exception e) {
managedFuture.completeExceptionally(e);
}
});
return managedFuture;
}

@Override
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) {
// FIXME: implement correctly
return super.thenApplyAsync(fn, executor);
}

@Override
public <U, V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) {
return super.thenCombine(other, executor.getContextService().contextualFunction(fn));
}

public static <U> CompletableFuture<U> completedFuture(U value, ManagedExecutorService executor) {
Expand Down

0 comments on commit 39b723b

Please sign in to comment.