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

io.sphere.sdk.client.CompletableFutureUtils Maven / Gradle / Ivy

The newest version!
package io.sphere.sdk.client;

import java.util.concurrent.*;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;

final class CompletableFutureUtils {
    private CompletableFutureUtils() {
    }

    /**
     * Creates a {@link java.util.concurrent.CompletableFuture} from a {@link com.ning.http.client.ListenableFuture}.
     * @param listenableFuture the future of the ning library
     * @param executor the executor to run the future in
     * @param  Type of the value that will be returned.
     * @return the Java 8 future implementation
     */
    static  CompletableFuture wrap(final ListenableFuture listenableFuture, final Executor executor) {
        final CompletableFuture result = new CompletableFuture();
        final Runnable listener = () -> {
            try {
                final T value = listenableFuture.get();
                result.complete(value);
            } catch (final InterruptedException | ExecutionException e) {
                result.completeExceptionally(e.getCause());
            }
        };
        listenableFuture.addListener(listener, executor);
        return result;
    }

    public static CompletableFuture wrap(final ListenableFuture listenableFuture) {
        return wrap(listenableFuture, ForkJoinPool.commonPool());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy