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.errorprone.annotations.CanIgnoreReturnValue; 019import org.checkerframework.checker.nullness.qual.Nullable; 020 021/** 022 * A {@link ListenableFuture} whose result can be set by a {@link #set(Object)}, {@link 023 * #setException(Throwable)} or {@link #setFuture(ListenableFuture)} call. It can also, like any 024 * other {@code Future}, be {@linkplain #cancel cancelled}. 025 * 026 * <p>{@code SettableFuture} is the recommended {@code ListenableFuture} implementation when your 027 * task cannot be implemented with {@link ListeningExecutorService}, the various {@link Futures} 028 * utility methods, or {@link ListenableFutureTask}. Those APIs have less opportunity for developer 029 * error. If your needs are more complex than {@code SettableFuture} supports, use {@link 030 * AbstractFuture}, which offers an extensible version of the API. 031 * 032 * @author Sven Mawson 033 * @since 9.0 (in 1.0 as {@code ValueFuture}) 034 */ 035@GwtCompatible 036@ElementTypesAreNonnullByDefault 037public final class SettableFuture<V extends @Nullable Object> 038 extends AbstractFuture.TrustedFuture<V> { 039 /** 040 * Creates a new {@code SettableFuture} that can be completed or cancelled by a later method call. 041 */ 042 public static <V extends @Nullable Object> SettableFuture<V> create() { 043 return new SettableFuture<V>(); 044 } 045 046 @CanIgnoreReturnValue 047 @Override 048 public boolean set(@ParametricNullness V value) { 049 return super.set(value); 050 } 051 052 @CanIgnoreReturnValue 053 @Override 054 public boolean setException(Throwable throwable) { 055 return super.setException(throwable); 056 } 057 058 @CanIgnoreReturnValue 059 @Override 060 public boolean setFuture(ListenableFuture<? extends V> future) { 061 return super.setFuture(future); 062 } 063 064 private SettableFuture() {} 065}