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 java.util.concurrent.Executor; 020import org.jspecify.annotations.Nullable; 021 022/** 023 * A {@link ListenableFuture} which forwards all its method calls to another future. Subclasses 024 * should override one or more methods to modify the behavior of the backing future as desired per 025 * the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 026 * 027 * <p><b>Warning:</b> The methods of {@code ForwardingListenableFuture} forward 028 * <b>indiscriminately</b> to the methods of the delegate. For example, overriding {@link #get(long, 029 * TimeUnit)} alone <b>will not</b> change the behavior of {@link #get()}, which can lead to 030 * unexpected behavior. In this case, you should override {@code get()} as well. 031 * 032 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 033 * default} methods. Instead, it inherits their default implementations. When those implementations 034 * invoke methods, they invoke methods on the {@code ForwardingListenableFuture}. 035 * 036 * <p>Most subclasses can just use {@link SimpleForwardingListenableFuture}. 037 * 038 * @author Shardul Deo 039 * @since 4.0 040 */ 041@GwtCompatible 042public abstract class ForwardingListenableFuture<V extends @Nullable Object> 043 extends ForwardingFuture<V> implements ListenableFuture<V> { 044 045 /** Constructor for use by subclasses. */ 046 protected ForwardingListenableFuture() {} 047 048 @Override 049 protected abstract ListenableFuture<? extends V> delegate(); 050 051 @Override 052 public void addListener(Runnable listener, Executor exec) { 053 delegate().addListener(listener, exec); 054 } 055 056 // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor 057 /** 058 * A simplified version of {@link ForwardingListenableFuture} where subclasses can pass in an 059 * already constructed {@link ListenableFuture} as the delegate. 060 * 061 * @since 9.0 062 */ 063 public abstract static class SimpleForwardingListenableFuture<V extends @Nullable Object> 064 extends ForwardingListenableFuture<V> { 065 private final ListenableFuture<V> delegate; 066 067 /** Constructor for use by subclasses. */ 068 protected SimpleForwardingListenableFuture(ListenableFuture<V> delegate) { 069 this.delegate = Preconditions.checkNotNull(delegate); 070 } 071 072 @Override 073 protected final ListenableFuture<V> delegate() { 074 return delegate; 075 } 076 } 077}