Example usage for com.google.common.util.concurrent ForwardingFuture ForwardingFuture

List of usage examples for com.google.common.util.concurrent ForwardingFuture ForwardingFuture

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ForwardingFuture ForwardingFuture.

Prototype

protected ForwardingFuture() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.java

public static Future<?> startProcessing(MessageProducer messageProducer, MessageConsumer messageConsumer,
        ExecutorService executorService) {
    ConcurrentMessageProcessor reader = new ConcurrentMessageProcessor(messageProducer, messageConsumer);
    Future<?> result = executorService.submit(reader);
    return new ForwardingFuture<Object>() {

        @SuppressWarnings("unchecked")
        @Override//from w  ww.  java 2 s .c  o  m
        protected Future<Object> delegate() {
            return (Future<Object>) result;
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            if (mayInterruptIfRunning && messageProducer instanceof Closeable) {
                try {
                    ((Closeable) messageProducer).close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return super.cancel(mayInterruptIfRunning);
        }
    };
}

From source file:com.google.cloud.dataflow.sdk.runners.worker.WindmillStateReader.java

private <T> Future<T> wrappedFuture(final Future<T> future) {
    // If the underlying lookup is already complete, we don't need to create the wrapper.
    if (future.isDone()) {
        return future;
    }//from w w w.j av a 2 s .c  o m

    return new ForwardingFuture<T>() {
        @Override
        protected Future<T> delegate() {
            return future;
        }

        @Override
        public T get() throws InterruptedException, ExecutionException {
            if (!future.isDone()) {
                startBatchAndBlock();
            }
            return super.get();
        }

        @Override
        public T get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            if (!future.isDone()) {
                startBatchAndBlock();
            }
            return super.get(timeout, unit);
        }
    };
}