001 /*
002 * Copyright (C) 2009 Google Inc.
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
017 package com.google.common.util.concurrent;
018
019 import com.google.common.annotations.Beta;
020
021 import javax.annotation.Nullable;
022
023 /**
024 * A simple ListenableFuture that holds a value or an exception.
025 *
026 * @author Sven Mawson
027 * @since 1
028 */
029 @Beta
030 public class ValueFuture<V> extends AbstractListenableFuture<V> {
031
032 /**
033 * Creates a new {@code ValueFuture} in the default state.
034 */
035 public static <T> ValueFuture<T> create() {
036 return new ValueFuture<T>();
037 }
038
039 /**
040 * Explicit private constructor, use the {@link #create} factory method to
041 * create instances of {@code ValueFuture}.
042 */
043 private ValueFuture() {}
044
045 /**
046 * Sets the value of this future. This method will return {@code true} if
047 * the value was successfully set, or {@code false} if the future has already
048 * been set or cancelled.
049 *
050 * @param newValue the value the future should hold.
051 * @return true if the value was successfully set.
052 */
053 @Override
054 public boolean set(@Nullable V newValue) {
055 return super.set(newValue);
056 }
057
058 /**
059 * Sets the future to having failed with the given exception. This exception
060 * will be wrapped in an ExecutionException and thrown from the get methods.
061 * This method will return {@code true} if the exception was successfully set,
062 * or {@code false} if the future has already been set or cancelled.
063 *
064 * @param t the exception the future should hold.
065 * @return true if the exception was successfully set.
066 */
067 @Override
068 public boolean setException(Throwable t) {
069 return super.setException(t);
070 }
071
072 /**
073 * {@inheritDoc}
074 *
075 * <p>A ValueFuture is never considered in the running state, so the
076 * {@code mayInterruptIfRunning} argument is ignored.
077 */
078 @Override
079 public boolean cancel(boolean mayInterruptIfRunning) {
080 return super.cancel();
081 }
082 }