001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.util.concurrent; 018 019import javax.annotation.Nullable; 020 021/** 022 * A {@link ListenableFuture} whose result may be set by a {@link #set(Object)} 023 * or {@link #setException(Throwable)} call. It may also be cancelled. 024 * 025 * @author Sven Mawson 026 * @since 9.0 (in 1.0 as {@code ValueFuture}) 027 */ 028public final class SettableFuture<V> extends AbstractFuture<V> { 029 030 /** 031 * Creates a new {@code SettableFuture} in the default state. 032 */ 033 public static <V> SettableFuture<V> create() { 034 return new SettableFuture<V>(); 035 } 036 037 /** 038 * Explicit private constructor, use the {@link #create} factory method to 039 * create instances of {@code SettableFuture}. 040 */ 041 private SettableFuture() {} 042 043 /** 044 * Sets the value of this future. This method will return {@code true} if 045 * the value was successfully set, or {@code false} if the future has already 046 * been set or cancelled. 047 * 048 * @param value the value the future should hold. 049 * @return true if the value was successfully set. 050 */ 051 @Override 052 public boolean set(@Nullable V value) { 053 return super.set(value); 054 } 055 056 /** 057 * Sets the future to having failed with the given exception. This exception 058 * will be wrapped in an {@code ExecutionException} and thrown from the {@code 059 * get} methods. This method will return {@code true} if the exception was 060 * successfully set, or {@code false} if the future has already been set or 061 * cancelled. 062 * 063 * @param throwable the exception the future should hold. 064 * @return true if the exception was successfully set. 065 */ 066 @Override 067 public boolean setException(Throwable throwable) { 068 return super.setException(throwable); 069 } 070}