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