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