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.GwtIncompatible;
018import com.google.common.annotations.J2ktIncompatible;
019import com.google.common.collect.ForwardingObject;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import com.google.errorprone.annotations.CheckReturnValue;
022import java.util.Collection;
023import java.util.List;
024import java.util.concurrent.Callable;
025import java.util.concurrent.ExecutionException;
026import java.util.concurrent.ExecutorService;
027import java.util.concurrent.Future;
028import java.util.concurrent.TimeUnit;
029import java.util.concurrent.TimeoutException;
030import org.checkerframework.checker.nullness.qual.Nullable;
031
032/**
033 * An executor service which forwards all its method calls to another executor service. Subclasses
034 * should override one or more methods to modify the behavior of the backing executor service as
035 * desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
036 *
037 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
038 * default} methods. Instead, it inherits their default implementations. When those implementations
039 * invoke methods, they invoke methods on the {@code ForwardingExecutorService}.
040 *
041 * @author Kurt Alfred Kluever
042 * @since 10.0
043 */
044@J2ktIncompatible
045@GwtIncompatible
046@ElementTypesAreNonnullByDefault
047public abstract class ForwardingExecutorService extends ForwardingObject
048    implements ExecutorService {
049  /** Constructor for use by subclasses. */
050  protected ForwardingExecutorService() {}
051
052  @Override
053  protected abstract ExecutorService delegate();
054
055  @CheckReturnValue
056  @Override
057  public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
058    return delegate().awaitTermination(timeout, unit);
059  }
060
061  @Override
062  public <T extends @Nullable Object> List<Future<T>> invokeAll(
063      Collection<? extends Callable<T>> tasks) throws InterruptedException {
064    return delegate().invokeAll(tasks);
065  }
066
067  @Override
068  public <T extends @Nullable Object> List<Future<T>> invokeAll(
069      Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
070      throws InterruptedException {
071    return delegate().invokeAll(tasks, timeout, unit);
072  }
073
074  @Override
075  public <T extends @Nullable Object> T invokeAny(Collection<? extends Callable<T>> tasks)
076      throws InterruptedException, ExecutionException {
077    return delegate().invokeAny(tasks);
078  }
079
080  @Override
081  public <T extends @Nullable Object> T invokeAny(
082      Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
083      throws InterruptedException, ExecutionException, TimeoutException {
084    return delegate().invokeAny(tasks, timeout, unit);
085  }
086
087  @Override
088  public boolean isShutdown() {
089    return delegate().isShutdown();
090  }
091
092  @Override
093  public boolean isTerminated() {
094    return delegate().isTerminated();
095  }
096
097  @Override
098  public void shutdown() {
099    delegate().shutdown();
100  }
101
102  @Override
103  @CanIgnoreReturnValue
104  public List<Runnable> shutdownNow() {
105    return delegate().shutdownNow();
106  }
107
108  @Override
109  public void execute(Runnable command) {
110    delegate().execute(command);
111  }
112
113  @Override
114  public <T extends @Nullable Object> Future<T> submit(Callable<T> task) {
115    return delegate().submit(task);
116  }
117
118  @Override
119  public Future<?> submit(Runnable task) {
120    return delegate().submit(task);
121  }
122
123  @Override
124  public <T extends @Nullable Object> Future<T> submit(
125      Runnable task, @ParametricNullness T result) {
126    return delegate().submit(task, result);
127  }
128}