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 039@ElementTypesAreNonnullByDefault 040public abstract class AbstractListeningExecutorService extends AbstractExecutorService 041 implements ListeningExecutorService { 042 043 /** @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) */ 044 @Override 045 protected final <T extends @Nullable Object> RunnableFuture<T> newTaskFor( 046 Runnable runnable, @ParametricNullness T value) { 047 return TrustedListenableFutureTask.create(runnable, value); 048 } 049 050 /** @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) */ 051 @Override 052 protected final <T extends @Nullable Object> RunnableFuture<T> newTaskFor(Callable<T> callable) { 053 return TrustedListenableFutureTask.create(callable); 054 } 055 056 @Override 057 public ListenableFuture<?> submit(Runnable task) { 058 return (ListenableFuture<?>) super.submit(task); 059 } 060 061 @Override 062 public <T extends @Nullable Object> ListenableFuture<T> submit( 063 Runnable task, @ParametricNullness T result) { 064 return (ListenableFuture<T>) super.submit(task, result); 065 } 066 067 @Override 068 public <T extends @Nullable Object> ListenableFuture<T> submit(Callable<T> task) { 069 return (ListenableFuture<T>) super.submit(task); 070 } 071}