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 javax.annotation.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 is not a good fit for a {@link ListeningExecutorService} task. If your needs are more
029 * complex than {@code SettableFuture} supports, use {@link AbstractFuture}, which offers an
030 * 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  @Beta
057  @CanIgnoreReturnValue
058  @Override
059  public boolean setFuture(ListenableFuture<? extends V> future) {
060    return super.setFuture(future);
061  }
062
063  private SettableFuture() {}
064}