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.GwtCompatible; 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; 025import org.jspecify.annotations.Nullable; 026 027/** 028 * A {@link Future} which forwards all its method calls to another future. Subclasses should 029 * override one or more methods to modify the behavior of the backing future as desired per the <a 030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 031 * 032 * <p><b>Warning:</b> The methods of {@code ForwardingFuture} forward <b>indiscriminately</b> to the 033 * methods of the delegate. For example, overriding {@link #get(long, TimeUnit)} alone <b>will 034 * not</b> change the behavior of {@link #get()}, which can lead to unexpected behavior. In this 035 * case, you should override {@code get()} as well. 036 * 037 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 038 * default} methods. Instead, it inherits their default implementations. When those implementations 039 * invoke methods, they invoke methods on the {@code ForwardingFuture}. 040 * 041 * <p>Most subclasses can just use {@link SimpleForwardingFuture}. 042 * 043 * @author Sven Mawson 044 * @since 1.0 045 */ 046@GwtCompatible 047public abstract class ForwardingFuture<V extends @Nullable Object> extends ForwardingObject 048 implements Future<V> { 049 /** Constructor for use by subclasses. */ 050 protected ForwardingFuture() {} 051 052 @Override 053 protected abstract Future<? extends V> delegate(); 054 055 @Override 056 @CanIgnoreReturnValue 057 public boolean cancel(boolean mayInterruptIfRunning) { 058 return delegate().cancel(mayInterruptIfRunning); 059 } 060 061 @Override 062 public boolean isCancelled() { 063 return delegate().isCancelled(); 064 } 065 066 @Override 067 public boolean isDone() { 068 return delegate().isDone(); 069 } 070 071 @Override 072 @CanIgnoreReturnValue 073 @ParametricNullness 074 public V get() throws InterruptedException, ExecutionException { 075 return delegate().get(); 076 } 077 078 @Override 079 @CanIgnoreReturnValue 080 @ParametricNullness 081 public V get(long timeout, TimeUnit unit) 082 throws InterruptedException, ExecutionException, TimeoutException { 083 return delegate().get(timeout, unit); 084 } 085 086 // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor 087 /** 088 * A simplified version of {@link ForwardingFuture} where subclasses can pass in an already 089 * constructed {@link Future} as the delegate. 090 * 091 * @since 9.0 092 */ 093 public abstract static class SimpleForwardingFuture<V extends @Nullable Object> 094 extends ForwardingFuture<V> { 095 private final Future<V> delegate; 096 097 /** Constructor for use by subclasses. */ 098 protected SimpleForwardingFuture(Future<V> delegate) { 099 this.delegate = Preconditions.checkNotNull(delegate); 100 } 101 102 @Override 103 protected final Future<V> delegate() { 104 return delegate; 105 } 106 } 107}