1/* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.util.concurrent; 18 19import com.google.common.base.Preconditions; 20import com.google.common.collect.ForwardingObject; 21 22import java.util.concurrent.ExecutionException; 23import java.util.concurrent.Future; 24import java.util.concurrent.TimeUnit; 25import java.util.concurrent.TimeoutException; 26 27/** 28 * A {@link Future} which forwards all its method calls to another future. 29 * Subclasses should override one or more methods to modify the behavior of 30 * the backing future as desired per the <a 31 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 32 * 33 * <p>Most subclasses can just use {@link SimpleForwardingFuture}. 34 * 35 * @author Sven Mawson 36 * @since 1.0 37 */ 38public abstract class ForwardingFuture<V> extends ForwardingObject 39 implements Future<V> { 40 41 /** Constructor for use by subclasses. */ 42 protected ForwardingFuture() {} 43 44 @Override protected abstract Future<V> delegate(); 45 46 @Override 47 public boolean cancel(boolean mayInterruptIfRunning) { 48 return delegate().cancel(mayInterruptIfRunning); 49 } 50 51 @Override 52 public boolean isCancelled() { 53 return delegate().isCancelled(); 54 } 55 56 @Override 57 public boolean isDone() { 58 return delegate().isDone(); 59 } 60 61 @Override 62 public V get() throws InterruptedException, ExecutionException { 63 return delegate().get(); 64 } 65 66 @Override 67 public V get(long timeout, TimeUnit unit) 68 throws InterruptedException, ExecutionException, TimeoutException { 69 return delegate().get(timeout, unit); 70 } 71 72 /* 73 * TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and 74 * constructor 75 */ 76 /** 77 * A simplified version of {@link ForwardingFuture} where subclasses 78 * can pass in an already constructed {@link Future} as the delegate. 79 * 80 * @since 9.0 81 */ 82 public abstract static class SimpleForwardingFuture<V> 83 extends ForwardingFuture<V> { 84 private final Future<V> delegate; 85 86 protected SimpleForwardingFuture(Future<V> delegate) { 87 this.delegate = Preconditions.checkNotNull(delegate); 88 } 89 90 @Override 91 protected final Future<V> delegate() { 92 return delegate; 93 } 94 95 } 96} 97