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.jspecify.annotations.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
036public final class SettableFuture<V extends @Nullable Object>
037    extends AbstractFuture.TrustedFuture<V> {
038  /**
039   * Creates a new {@code SettableFuture} that can be completed or cancelled by a later method call.
040   */
041  public static <V extends @Nullable Object> SettableFuture<V> create() {
042    return new SettableFuture<>();
043  }
044
045  @CanIgnoreReturnValue
046  @Override
047  public boolean set(@ParametricNullness V value) {
048    return super.set(value);
049  }
050
051  @CanIgnoreReturnValue
052  @Override
053  public boolean setException(Throwable throwable) {
054    return super.setException(throwable);
055  }
056
057  @CanIgnoreReturnValue
058  @Override
059  public boolean setFuture(ListenableFuture<? extends V> future) {
060    return super.setFuture(future);
061  }
062
063  private SettableFuture() {}
064}