All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smallibs.concurrent.utils.CompletableFutureHelper Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
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