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
017package com.google.common.util.concurrent;
018
019import com.google.common.annotations.Beta;
020import com.google.common.base.Preconditions;
021
022import java.util.concurrent.TimeUnit;
023import 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
041public abstract class ForwardingCheckedFuture<V, X extends Exception> 
042    extends ForwardingListenableFuture<V> implements CheckedFuture<V, X> {
043
044  @Override
045  public V checkedGet() throws X {
046    return delegate().checkedGet();
047  }
048
049  @Override
050  public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
051    return delegate().checkedGet(timeout, unit);
052  }
053
054  @Override
055  protected abstract CheckedFuture<V, X> delegate();
056
057  // TODO(cpovirk): Use Standard Javadoc form for SimpleForwarding*
058  /**
059   * A simplified version of {@link ForwardingCheckedFuture} where subclasses
060   * can pass in an already constructed {@link CheckedFuture} as the delegate.
061   * 
062   * @since 9.0
063   */
064  @Beta
065  public abstract static class SimpleForwardingCheckedFuture<
066      V, X extends Exception> extends ForwardingCheckedFuture<V, X> {
067    private final CheckedFuture<V, X> delegate;
068
069    protected SimpleForwardingCheckedFuture(CheckedFuture<V, X> delegate) {
070      this.delegate = Preconditions.checkNotNull(delegate);
071    }
072
073    @Override
074    protected final CheckedFuture<V, X> delegate() {
075      return delegate;
076    }
077  }
078}