1 /* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import javax.annotation.Nullable; 20 21 /** 22 * A {@link ListenableFuture} whose result may be set by a {@link #set(Object)} 23 * or {@link #setException(Throwable)} call. It may also be cancelled. 24 * 25 * @author Sven Mawson 26 * @since 9.0 (in 1.0 as {@code ValueFuture}) 27 */ 28 public final class SettableFuture<V> extends AbstractFuture<V> { 29 30 /** 31 * Creates a new {@code SettableFuture} in the default state. 32 */ 33 public static <V> SettableFuture<V> create() { 34 return new SettableFuture<V>(); 35 } 36 37 /** 38 * Explicit private constructor, use the {@link #create} factory method to 39 * create instances of {@code SettableFuture}. 40 */ 41 private SettableFuture() {} 42 43 /** 44 * Sets the value of this future. This method will return {@code true} if 45 * the value was successfully set, or {@code false} if the future has already 46 * been set or cancelled. 47 * 48 * @param value the value the future should hold. 49 * @return true if the value was successfully set. 50 */ 51 @Override 52 public boolean set(@Nullable V value) { 53 return super.set(value); 54 } 55 56 /** 57 * Sets the future to having failed with the given exception. This exception 58 * will be wrapped in an {@code ExecutionException} and thrown from the {@code 59 * get} methods. This method will return {@code true} if the exception was 60 * successfully set, or {@code false} if the future has already been set or 61 * cancelled. 62 * 63 * @param throwable the exception the future should hold. 64 * @return true if the exception was successfully set. 65 */ 66 @Override 67 public boolean setException(Throwable throwable) { 68 return super.setException(throwable); 69 } 70 } 71