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