001    /*
002     * Copyright (C) 2008 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 java.util.concurrent.Callable;
022    import java.util.concurrent.Executor;
023    import java.util.concurrent.FutureTask;
024    
025    import javax.annotation.Nullable;
026    
027    /**
028     * A {@link FutureTask} that also implements the {@link ListenableFuture}
029     * interface.  Subclasses must make sure to call {@code super.done()} if they
030     * also override the {@link #done()} method, otherwise the listeners will not
031     * be called.
032     *
033     * @author Sven Mawson
034     * @since 1.0
035     */
036    public final class ListenableFutureTask<V> extends FutureTask<V>
037        implements ListenableFuture<V> {
038    
039      // The execution list to hold our listeners.
040      private final ExecutionList executionList = new ExecutionList();
041    
042      /**
043       * Creates a {@code ListenableFutureTask} that will upon running, execute the
044       * given {@code Callable}.
045       *
046       * @param callable the callable task
047       * @since 10.0
048       */
049      public static <V> ListenableFutureTask<V> create(Callable<V> callable) {
050        return new ListenableFutureTask<V>(callable);
051      }
052    
053      /**
054       * Creates a {@code ListenableFutureTask} that will upon running, execute the
055       * given {@code Runnable}, and arrange that {@code get} will return the
056       * given result on successful completion.
057       *
058       * @param runnable the runnable task
059       * @param result the result to return on successful completion. If you don't
060       *     need a particular result, consider using constructions of the form:
061       *     {@code ListenableFuture<?> f = ListenableFutureTask.create(runnable,
062       *     null)}
063       * @since 10.0
064       */
065      public static <V> ListenableFutureTask<V> create(
066          Runnable runnable, @Nullable V result) {
067        return new ListenableFutureTask<V>(runnable, result);
068      }
069    
070      /**
071       * <b>Deprecated.</b> Use {@link #create(Callable)} instead. This method will be
072       * removed from Guava in Guava release 11.0.
073       *
074       * Creates a {@code ListenableFutureTask} that will upon running, execute the
075       * given {@code Callable}.
076       *
077       * @param callable the callable task
078       */
079      @Beta @Deprecated public
080      ListenableFutureTask(Callable<V> callable) {
081        super(callable);
082      }
083    
084      /**
085       * <b>Deprecated. Use {@link #create(Runnable, Object)} instead. This method
086       * will be removed from Guava in Guava release 11.0.</b>
087       *
088       * Creates a {@code ListenableFutureTask} that will upon running, execute the
089       * given {@code Runnable}, and arrange that {@code get} will return the
090       * given result on successful completion.
091       *
092       * @param runnable the runnable task
093       * @param result the result to return on successful completion. If
094       * you don't need a particular result, consider using
095       * constructions of the form:
096       * {@code ListenableFuture<?> f =
097       *     ListenableFutureTask.create(runnable, null)}
098       */
099      @Beta @Deprecated public
100      ListenableFutureTask(Runnable runnable, @Nullable V result) {
101        super(runnable, result);
102      }
103    
104      @Override
105      public void addListener(Runnable listener, Executor exec) {
106        executionList.add(listener, exec);
107      }
108    
109      @Override
110      protected void done() {
111        executionList.execute();
112      }
113    }