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