    001package com.hammurapi.common.concurrent;
    002
    003import java.util.concurrent.Callable;
    004import java.util.concurrent.ExecutionException;
    005import java.util.concurrent.Future;
    006import java.util.concurrent.RunnableFuture;
    007import java.util.concurrent.TimeUnit;
    008import java.util.concurrent.TimeoutException;
    009
    010public class DelegatingRunnableFuture<V> implements RunnableFuture<V> {
    011
    012        private Future<V> target;
    013
    014        private Runnable command;
    015
    016        private Callable<V> task;
    017
    018        public DelegatingRunnableFuture(Runnable command, V value) {
    019                this.command = command;
    020                this.value = value;
    021        }
    022
    023        public Runnable getCommand() {
    024                return command;
    025        }
    026
    027        public DelegatingRunnableFuture(Callable<V> task) {
    028                this.task = task;
    029        }
    030
    031        public Callable<V> getTask() {
    032                return task;
    033        }
    034
    035        public boolean cancel(boolean mayInterruptIfRunning) {
    036                return target.cancel(mayInterruptIfRunning);
    037        }
    038
    039        public boolean isCancelled() {
    040                return target.isCancelled();
    041        }
    042
    043        public boolean isDone() {
    044                return target.isDone();
    045        }
    046
    047        public V get() throws InterruptedException, ExecutionException {
    048                return target.get();
    049        }
    050
    051        public V get(long timeout, TimeUnit unit) throws InterruptedException,
    052                        ExecutionException, TimeoutException {
    053                return target.get(timeout, unit);
    054        }
    055
    056        @Override
    057        public void run() {
    058                if (target instanceof Runnable) {
    059                        ((Runnable) target).run();
    060                } else {
    061                        throw new UnsupportedOperationException("Target is not runnable");
    062                }
    063        }
    064
    065        public void setTarget(Future<V> target) {
    066                this.target = target;
    067        }
    068
    069        private V value;
    070
    071        public V getValue() {
    072                return value;
    073        }
    074}