001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtIncompatible;
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020import com.google.errorprone.annotations.CheckReturnValue;
021import java.util.concurrent.AbstractExecutorService;
022import java.util.concurrent.Callable;
023import java.util.concurrent.RunnableFuture;
024import org.checkerframework.checker.nullness.qual.Nullable;
025
026/**
027 * Abstract {@link ListeningExecutorService} implementation that creates {@link ListenableFuture}
028 * instances for each {@link Runnable} and {@link Callable} submitted to it. These tasks are run
029 * with the abstract {@link #execute execute(Runnable)} method.
030 *
031 * <p>In addition to {@link #execute}, subclasses must implement all methods related to shutdown and
032 * termination.
033 *
034 * @author Chris Povirk
035 * @since 14.0
036 */
037@CheckReturnValue
038@Beta
039@GwtIncompatible
040@ElementTypesAreNonnullByDefault
041public abstract class AbstractListeningExecutorService extends AbstractExecutorService
042    implements ListeningExecutorService {
043
044  /**
045   * @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0)
046   */
047  @CanIgnoreReturnValue // TODO(kak): consider removing this
048  @Override
049  protected final <T extends @Nullable Object> RunnableFuture<T> newTaskFor(
050      Runnable runnable, @ParametricNullness T value) {
051    return TrustedListenableFutureTask.create(runnable, value);
052  }
053
054  /**
055   * @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0)
056   */
057  @CanIgnoreReturnValue // TODO(kak): consider removing this
058  @Override
059  protected final <T extends @Nullable Object> RunnableFuture<T> newTaskFor(Callable<T> callable) {
060    return TrustedListenableFutureTask.create(callable);
061  }
062
063  @CanIgnoreReturnValue // TODO(kak): consider removing this
064  @Override
065  public ListenableFuture<?> submit(Runnable task) {
066    return (ListenableFuture<?>) super.submit(task);
067  }
068
069  @CanIgnoreReturnValue // TODO(kak): consider removing this
070  @Override
071  public <T extends @Nullable Object> ListenableFuture<T> submit(
072      Runnable task, @ParametricNullness T result) {
073    return (ListenableFuture<T>) super.submit(task, result);
074  }
075
076  @CanIgnoreReturnValue // TODO(kak): consider removing this
077  @Override
078  public <T extends @Nullable Object> ListenableFuture<T> submit(Callable<T> task) {
079    return (ListenableFuture<T>) super.submit(task);
080  }
081}