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