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

com.yahoo.parsec.clients.ParsecCompletableFuture Maven / Gradle / Ivy

There is a newer version: 1.0.24
Show newest version
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.

package com.yahoo.parsec.clients;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.ning.http.client.extra.ListenableFutureAdapter;

import java.util.concurrent.*;

/**
 * Simple {@link CompletableFuture} implementation that wraps {@link Future}.
 *
 * @param  Response type
 */
public class ParsecCompletableFuture extends CompletableFuture {
    /**
     * Future.
     */
    private Future future;

    /**
     * Constructor.
     *
     * @param future future
     */
    public ParsecCompletableFuture(final Future future) {
        if (future instanceof com.ning.http.client.ListenableFuture) {
            this.future = ListenableFutureAdapter.asGuavaFuture((com.ning.http.client.ListenableFuture) future);
        } else {
            this.future = future;
        }

        if (this.future instanceof ListenableFuture) {
            Futures.addCallback((ListenableFuture) this.future, new FutureCallback() {
                @Override
                public void onSuccess(T result) {
                    complete(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    completeExceptionally(t);
                }
            });
        }
    }

    /**
     * Cancel.
     *
     * @param mayInterruptIfRunning mayInterruptIfRunning
     * @return cancel was success or not
     */
    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    /**
     * Get.
     *
     * @return T
     * @throws InterruptedException Interrupted exception
     * @throws ExecutionException Execution exception
     */
    @Override
    public T get() throws InterruptedException, ExecutionException {
        return future.get();
    }

    /**
     * Get.
     *
     * @param timeout timeout
     * @param unit unit
     * @return T
     * @throws InterruptedException Interrupted exception
     * @throws TimeoutException Timeout exception
     * @throws ExecutionException Execution exception
     */
    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, ExecutionException {
        return future.get(timeout, unit);
    }

    /**
     * Is cancelled.
     *
     * @return boolean
     */
    @Override
    public boolean isCancelled() {
        return future.isCancelled();
    }

    /**
     * Is done.
     *
     * @return boolean
     */
    @Override
    public boolean isDone() {
        return future.isDone();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy