Home | History | Annotate | Download | only in concurrent
      1 /*
      2  * Copyright (C) 2009 Google Inc.
      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 /**
     20  * A simple ListenableFuture that holds a value or an exception.
     21  *
     22  * @author Sven Mawson
     23  * @since 2009.09.15 <b>tentative</b>
     24  */
     25 public class ValueFuture<V> extends AbstractListenableFuture<V> {
     26 
     27   /**
     28    * Creates a new {@code ValueFuture} in the default state.
     29    */
     30   public static <T> ValueFuture<T> create() {
     31     return new ValueFuture<T>();
     32   }
     33 
     34   /**
     35    * Explicit private constructor, use the {@link #create} factory method to
     36    * create instances of {@code ValueFuture}.
     37    */
     38   private ValueFuture() {}
     39 
     40   /**
     41    * Sets the value of this future.  This method will return {@code true} if
     42    * the value was successfully set, or {@code false} if the future has already
     43    * been set or cancelled.
     44    *
     45    * @param newValue the value the future should hold.
     46    * @return true if the value was successfully set.
     47    */
     48   @Override
     49   public boolean set(V newValue) {
     50     return super.set(newValue);
     51   }
     52 
     53   /**
     54    * Sets the future to having failed with the given exception.  This exception
     55    * will be wrapped in an ExecutionException and thrown from the get methods.
     56    * This method will return {@code true} if the exception was successfully set,
     57    * or {@code false} if the future has already been set or cancelled.
     58    *
     59    * @param t the exception the future should hold.
     60    * @return true if the exception was successfully set.
     61    */
     62   @Override
     63   public boolean setException(Throwable t) {
     64     return super.setException(t);
     65   }
     66 
     67   /**
     68    * {@inheritDoc}
     69    *
     70    * <p>A ValueFuture is never considered in the running state, so the
     71    * {@code mayInterruptIfRunning} argument is ignored.
     72    */
     73   @Override
     74   public boolean cancel(boolean mayInterruptIfRunning) {
     75     return super.cancel();
     76   }
     77 }
     78