View Javadoc
1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.util.concurrent;
16  
17  import com.google.common.annotations.Beta;
18  import com.google.common.annotations.GwtIncompatible;
19  import com.google.errorprone.annotations.CanIgnoreReturnValue;
20  import java.util.concurrent.AbstractExecutorService;
21  import java.util.concurrent.Callable;
22  import java.util.concurrent.RunnableFuture;
23  import javax.annotation.Nullable;
24  
25  /**
26   * Abstract {@link ListeningExecutorService} implementation that creates {@link ListenableFuture}
27   * instances for each {@link Runnable} and {@link Callable} submitted to it. These tasks are run
28   * with the abstract {@link #execute execute(Runnable)} method.
29   *
30   * <p>In addition to {@link #execute}, subclasses must implement all methods related to shutdown and
31   * termination.
32   *
33   * @author Chris Povirk
34   * @since 14.0
35   */
36  @Beta
37  @CanIgnoreReturnValue
38  @GwtIncompatible
39  public abstract class AbstractListeningExecutorService extends AbstractExecutorService
40      implements ListeningExecutorService {
41  
42    /** @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) */
43    @Override
44    protected final <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
45      return TrustedListenableFutureTask.create(runnable, value);
46    }
47  
48    /** @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) */
49    @Override
50    protected final <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
51      return TrustedListenableFutureTask.create(callable);
52    }
53  
54    @Override
55    public ListenableFuture<?> submit(Runnable task) {
56      return (ListenableFuture<?>) super.submit(task);
57    }
58  
59    @Override
60    public <T> ListenableFuture<T> submit(Runnable task, @Nullable T result) {
61      return (ListenableFuture<T>) super.submit(task, result);
62    }
63  
64    @Override
65    public <T> ListenableFuture<T> submit(Callable<T> task) {
66      return (ListenableFuture<T>) super.submit(task);
67    }
68  }