001    /*
002     * Copyright (C) 2011 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.annotations.Beta;
020    import com.google.common.base.Preconditions;
021    
022    import java.util.concurrent.TimeUnit;
023    import java.util.concurrent.TimeoutException;
024    
025    /**
026     * A future which forwards all its method calls to another future. Subclasses
027     * should override one or more methods to modify the behavior of the backing 
028     * future as desired per the <a href=
029     * "http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
030     *
031     * <p>Most subclasses can simply extend {@link SimpleForwardingCheckedFuture}.
032     * 
033     * @param <V> The result type returned by this Future's {@code get} method
034     * @param <X> The type of the Exception thrown by the Future's 
035     *            {@code checkedGet} method
036     *
037     * @author Anthony Zana
038     * @since 9.0
039     */
040    @Beta
041    public abstract class ForwardingCheckedFuture<V, X extends Exception> 
042        extends ForwardingListenableFuture<V> implements CheckedFuture<V, X> {
043    
044      public V checkedGet() throws X {
045        return delegate().checkedGet();
046      }
047    
048      public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
049        return delegate().checkedGet(timeout, unit);
050      }
051    
052      
053      @Override
054      protected abstract CheckedFuture<V, X> delegate();
055    
056      // TODO(cpovirk): Use Standard Javadoc form for SimpleForwarding*
057      /**
058       * A simplified version of {@link ForwardingCheckedFuture} where subclasses
059       * can pass in an already constructed {@link CheckedFuture} as the delegate.
060       * 
061       * @since 9.0
062       */
063      @Beta
064      public abstract static class SimpleForwardingCheckedFuture<
065          V, X extends Exception> extends ForwardingCheckedFuture<V, X> {
066        private final CheckedFuture<V, X> delegate;
067    
068        protected SimpleForwardingCheckedFuture(CheckedFuture<V, X> delegate) {
069          this.delegate = Preconditions.checkNotNull(delegate);
070        }
071    
072        
073        @Override
074        protected final CheckedFuture<V, X> delegate() {
075          return delegate;
076        }
077      }
078    }