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.errorprone.annotations.CanIgnoreReturnValue;
19 import org.checkerframework.checker.nullness.qual.Nullable;
20 
21 /**
22  * A {@link ListenableFuture} whose result can be set by a {@link #set(Object)}, {@link
23  * #setException(Throwable)} or {@link #setFuture(ListenableFuture)} call. It can also, like any
24  * other {@code Future}, be {@linkplain #cancel cancelled}.
25  *
26  * <p>{@code SettableFuture} is the recommended {@code ListenableFuture} implementation when your
27  * task cannot be implemented with {@link ListeningExecutorService}, the various {@link Futures}
28  * utility methods, or {@link ListenableFutureTask}. Those APIs have less opportunity for developer
29  * error. If your needs are more complex than {@code SettableFuture} supports, use {@link
30  * AbstractFuture}, which offers an extensible version of the API.
31  *
32  * @author Sven Mawson
33  * @since 9.0 (in 1.0 as {@code ValueFuture})
34  */
35 @GwtCompatible
36 public final class SettableFuture<V> extends AbstractFuture.TrustedFuture<V> {
37   /**
38    * Creates a new {@code SettableFuture} that can be completed or cancelled by a later method call.
39    */
create()40   public static <V> SettableFuture<V> create() {
41     return new SettableFuture<V>();
42   }
43 
44   @CanIgnoreReturnValue
45   @Override
set(@ullable V value)46   public boolean set(@Nullable V value) {
47     return super.set(value);
48   }
49 
50   @CanIgnoreReturnValue
51   @Override
setException(Throwable throwable)52   public boolean setException(Throwable throwable) {
53     return super.setException(throwable);
54   }
55 
56   @CanIgnoreReturnValue
57   @Override
setFuture(ListenableFuture<? extends V> future)58   public boolean setFuture(ListenableFuture<? extends V> future) {
59     return super.setFuture(future);
60   }
61 
SettableFuture()62   private SettableFuture() {}
63 }
64