001    /*
002     * Copyright (C) 2009 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.util.concurrent;
018    
019    import com.google.common.base.Preconditions;
020    import com.google.common.collect.ForwardingObject;
021    
022    import java.util.concurrent.ExecutionException;
023    import java.util.concurrent.Future;
024    import java.util.concurrent.TimeUnit;
025    import java.util.concurrent.TimeoutException;
026    
027    /**
028     * A {@link Future} which forwards all its method calls to another future.
029     * Subclasses should override one or more methods to modify the behavior of
030     * the backing future as desired per the <a
031     * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
032     *
033     * <p>Most subclasses can just use {@link SimpleForwardingFuture}.
034     * 
035     * @author Sven Mawson
036     * @since 1.0
037     */
038    public abstract class ForwardingFuture<V> extends ForwardingObject
039        implements Future<V> {
040    
041      /** Constructor for use by subclasses. */
042      protected ForwardingFuture() {}
043    
044      @Override protected abstract Future<V> delegate();
045    
046      @Override
047      public boolean cancel(boolean mayInterruptIfRunning) {
048        return delegate().cancel(mayInterruptIfRunning);
049      }
050    
051      @Override
052      public boolean isCancelled() {
053        return delegate().isCancelled();
054      }
055    
056      @Override
057      public boolean isDone() {
058        return delegate().isDone();
059      }
060    
061      @Override
062      public V get() throws InterruptedException, ExecutionException {
063        return delegate().get();
064      }
065    
066      @Override
067      public V get(long timeout, TimeUnit unit)
068          throws InterruptedException, ExecutionException, TimeoutException {
069        return delegate().get(timeout, unit);
070      }
071    
072      /*
073       * TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and
074       * constructor
075       */
076      /**
077       * A simplified version of {@link ForwardingFuture} where subclasses
078       * can pass in an already constructed {@link Future} as the delegate.
079       * 
080       * @since 9.0
081       */
082      public abstract static class SimpleForwardingFuture<V> 
083          extends ForwardingFuture<V> {
084        private final Future<V> delegate;
085    
086        protected SimpleForwardingFuture(Future<V> delegate) {
087          this.delegate = Preconditions.checkNotNull(delegate);
088        }
089    
090        @Override
091        protected final Future<V> delegate() {
092          return delegate;
093        }
094        
095      }
096    }