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