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 java.util.concurrent.AbstractExecutorService;
021import java.util.concurrent.Callable;
022import java.util.concurrent.RunnableFuture;
023import org.checkerframework.checker.nullness.qual.Nullable;
024
025/**
026 * Abstract {@link ListeningExecutorService} implementation that creates {@link ListenableFuture}
027 * instances for each {@link Runnable} and {@link Callable} submitted to it. These tasks are run
028 * with the abstract {@link #execute execute(Runnable)} method.
029 *
030 * <p>In addition to {@link #execute}, subclasses must implement all methods related to shutdown and
031 * termination.
032 *
033 * @author Chris Povirk
034 * @since 14.0
035 */
036@Beta
037@CanIgnoreReturnValue
038@GwtIncompatible
039public abstract class AbstractListeningExecutorService extends AbstractExecutorService
040    implements ListeningExecutorService {
041
042  /** @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) */
043  @Override
044  protected final <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
045    return TrustedListenableFutureTask.create(runnable, value);
046  }
047
048  /** @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) */
049  @Override
050  protected final <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
051    return TrustedListenableFutureTask.create(callable);
052  }
053
054  @Override
055  public ListenableFuture<?> submit(Runnable task) {
056    return (ListenableFuture<?>) super.submit(task);
057  }
058
059  @Override
060  public <T> ListenableFuture<T> submit(Runnable task, @Nullable T result) {
061    return (ListenableFuture<T>) super.submit(task, result);
062  }
063
064  @Override
065  public <T> ListenableFuture<T> submit(Callable<T> task) {
066    return (ListenableFuture<T>) super.submit(task);
067  }
068}