001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import com.google.common.annotations.GwtIncompatible; 018import com.google.common.base.Preconditions; 019import com.google.common.collect.ForwardingObject; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.concurrent.ExecutionException; 022import java.util.concurrent.Future; 023import java.util.concurrent.TimeUnit; 024import java.util.concurrent.TimeoutException; 025 026/** 027 * A {@link Future} which forwards all its method calls to another future. Subclasses should 028 * override one or more methods to modify the behavior of the backing future as desired per the <a 029 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 030 * 031 * <p>Most subclasses can just use {@link SimpleForwardingFuture}. 032 * 033 * @author Sven Mawson 034 * @since 1.0 035 */ 036@CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict. 037@GwtIncompatible 038public abstract class ForwardingFuture<V> extends ForwardingObject implements Future<V> { 039 /** Constructor for use by subclasses. */ 040 protected ForwardingFuture() {} 041 042 @Override 043 protected abstract Future<? extends V> delegate(); 044 045 @Override 046 public boolean cancel(boolean mayInterruptIfRunning) { 047 return delegate().cancel(mayInterruptIfRunning); 048 } 049 050 @Override 051 public boolean isCancelled() { 052 return delegate().isCancelled(); 053 } 054 055 @Override 056 public boolean isDone() { 057 return delegate().isDone(); 058 } 059 060 @Override 061 public V get() throws InterruptedException, ExecutionException { 062 return delegate().get(); 063 } 064 065 @Override 066 public V get(long timeout, TimeUnit unit) 067 throws InterruptedException, ExecutionException, TimeoutException { 068 return delegate().get(timeout, unit); 069 } 070 071 // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor 072 /** 073 * A simplified version of {@link ForwardingFuture} where subclasses can pass in an already 074 * constructed {@link Future} as the delegate. 075 * 076 * @since 9.0 077 */ 078 public abstract static class SimpleForwardingFuture<V> extends ForwardingFuture<V> { 079 private final Future<V> delegate; 080 081 protected SimpleForwardingFuture(Future<V> delegate) { 082 this.delegate = Preconditions.checkNotNull(delegate); 083 } 084 085 @Override 086 protected final Future<V> delegate() { 087 return delegate; 088 } 089 } 090}