
org.smallibs.concurrent.utils.CompletableFutureHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hpas Show documentation
Show all versions of hpas Show documentation
Functional ADT And Asynchronous library in Java
package org.smallibs.concurrent.utils;
import org.smallibs.concurrent.promise.Promise;
import org.smallibs.concurrent.promise.impl.SolvablePromise;
import org.smallibs.data.Try;
import java.util.concurrent.CompletableFuture;
public interface CompletableFutureHelper {
static CompletableFuture completableFuture(Promise promise) {
final CompletableFuture future = new CompletableFuture<>();
promise.onComplete(tTry ->
tTry.onSuccess(future::complete).onFailure(future::completeExceptionally)
);
return future;
}
static Promise promise(CompletableFuture completableFuture) {
final SolvablePromise promise = new SolvablePromise<>();
completableFuture.whenComplete((v, e) -> {
if (e != null) {
promise.solve(Try.failure(e));
} else {
promise.solve(Try.success(v));
}
});
return promise;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy