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      
045      @Override
046      protected abstract Future<V> delegate();
047    
048      public boolean cancel(boolean mayInterruptIfRunning) {
049        return delegate().cancel(mayInterruptIfRunning);
050      }
051    
052      public boolean isCancelled() {
053        return delegate().isCancelled();
054      }
055    
056      public boolean isDone() {
057        return delegate().isDone();
058      }
059    
060      public V get() throws InterruptedException, ExecutionException {
061        return delegate().get();
062      }
063    
064      public V get(long timeout, TimeUnit unit)
065          throws InterruptedException, ExecutionException, TimeoutException {
066        return delegate().get(timeout, unit);
067      }
068    
069      /*
070       * TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and
071       * constructor
072       */
073      /**
074       * A simplified version of {@link ForwardingFuture} where subclasses
075       * can pass in an already constructed {@link Future} as the delegate.
076       * 
077       * @since 9.0
078       */
079      public abstract static class SimpleForwardingFuture<V> 
080          extends ForwardingFuture<V> {
081        private final Future<V> delegate;
082    
083        protected SimpleForwardingFuture(Future<V> delegate) {
084          this.delegate = Preconditions.checkNotNull(delegate);
085        }
086    
087        
088        @Override
089        protected final Future<V> delegate() {
090          return delegate;
091        }
092        
093      }
094    }