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.annotations.Beta; 020 import com.google.common.base.Preconditions; 021 import com.google.common.collect.ForwardingObject; 022 023 import java.util.concurrent.ExecutionException; 024 import java.util.concurrent.Future; 025 import java.util.concurrent.TimeUnit; 026 import java.util.concurrent.TimeoutException; 027 028 /** 029 * A {@link Future} which forwards all its method calls to another future. 030 * Subclasses should override one or more methods to modify the behavior of 031 * the backing future as desired per the <a 032 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 033 * 034 * <p>Most subclasses can just use {@link SimpleForwardingFuture}. 035 * 036 * @author Sven Mawson 037 * @since 1 038 */ 039 public abstract class ForwardingFuture<V> extends ForwardingObject 040 implements Future<V> { 041 042 /** Constructor for use by subclasses. */ 043 protected ForwardingFuture() {} 044 045 @Override protected abstract Future<V> delegate(); 046 047 @Override 048 public boolean cancel(boolean mayInterruptIfRunning) { 049 return delegate().cancel(mayInterruptIfRunning); 050 } 051 052 @Override 053 public boolean isCancelled() { 054 return delegate().isCancelled(); 055 } 056 057 @Override 058 public boolean isDone() { 059 return delegate().isDone(); 060 } 061 062 @Override 063 public V get() throws InterruptedException, ExecutionException { 064 return delegate().get(); 065 } 066 067 @Override 068 public V get(long timeout, TimeUnit unit) 069 throws InterruptedException, ExecutionException, TimeoutException { 070 return delegate().get(timeout, unit); 071 } 072 073 // TODO(cpovirk): Use Standard Javadoc form for SimpleForwarding* 074 /** 075 * A simplified version of {@link ForwardingFuture} where subclasses 076 * can pass in an already constructed {@link Future} as the delegate. 077 * 078 * @since 9 079 */ 080 @Beta 081 public abstract static class SimpleForwardingFuture<V> 082 extends ForwardingFuture<V> { 083 private final Future<V> delegate; 084 085 protected SimpleForwardingFuture(Future<V> delegate) { 086 this.delegate = Preconditions.checkNotNull(delegate); 087 } 088 089 @Override 090 protected final Future<V> delegate() { 091 return delegate; 092 } 093 094 } 095 }