Coverage Summary for Class: WrappingExecutorService (com.google.common.util.concurrent)

Class Method, % Line, %
WrappingExecutorService 0% (0/16) 0% (0/22)
WrappingExecutorService$1 0% (0/2) 0% (0/6)
Total 0% (0/18) 0% (0/28)


1 /* 2  * Copyright (C) 2011 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.util.concurrent; 16  17 import static com.google.common.base.Preconditions.checkNotNull; 18 import static com.google.common.base.Throwables.throwIfUnchecked; 19  20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.collect.ImmutableList; 22 import com.google.errorprone.annotations.CanIgnoreReturnValue; 23 import java.util.Collection; 24 import java.util.List; 25 import java.util.concurrent.Callable; 26 import java.util.concurrent.ExecutionException; 27 import java.util.concurrent.ExecutorService; 28 import java.util.concurrent.Executors; 29 import java.util.concurrent.Future; 30 import java.util.concurrent.TimeUnit; 31 import java.util.concurrent.TimeoutException; 32 import org.checkerframework.checker.nullness.qual.Nullable; 33  34 /** 35  * An abstract {@code ExecutorService} that allows subclasses to {@linkplain #wrapTask(Callable) 36  * wrap} tasks before they are submitted to the underlying executor. 37  * 38  * <p>Note that task wrapping may occur even if the task is never executed. 39  * 40  * <p>For delegation without task-wrapping, see {@link ForwardingExecutorService}. 41  * 42  * @author Chris Nokleberg 43  */ 44 @CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict. 45 @GwtIncompatible 46 @ElementTypesAreNonnullByDefault 47 abstract class WrappingExecutorService implements ExecutorService { 48  private final ExecutorService delegate; 49  50  protected WrappingExecutorService(ExecutorService delegate) { 51  this.delegate = checkNotNull(delegate); 52  } 53  54  /** 55  * Wraps a {@code Callable} for submission to the underlying executor. This method is also applied 56  * to any {@code Runnable} passed to the default implementation of {@link #wrapTask(Runnable)}. 57  */ 58  protected abstract <T extends @Nullable Object> Callable<T> wrapTask(Callable<T> callable); 59  60  /** 61  * Wraps a {@code Runnable} for submission to the underlying executor. The default implementation 62  * delegates to {@link #wrapTask(Callable)}. 63  */ 64  protected Runnable wrapTask(Runnable command) { 65  final Callable<Object> wrapped = wrapTask(Executors.callable(command, null)); 66  return new Runnable() { 67  @Override 68  public void run() { 69  try { 70  wrapped.call(); 71  } catch (Exception e) { 72  throwIfUnchecked(e); 73  throw new RuntimeException(e); 74  } 75  } 76  }; 77  } 78  79  /** 80  * Wraps a collection of tasks. 81  * 82  * @throws NullPointerException if any element of {@code tasks} is null 83  */ 84  private <T extends @Nullable Object> ImmutableList<Callable<T>> wrapTasks( 85  Collection<? extends Callable<T>> tasks) { 86  ImmutableList.Builder<Callable<T>> builder = ImmutableList.builder(); 87  for (Callable<T> task : tasks) { 88  builder.add(wrapTask(task)); 89  } 90  return builder.build(); 91  } 92  93  // These methods wrap before delegating. 94  @Override 95  public final void execute(Runnable command) { 96  delegate.execute(wrapTask(command)); 97  } 98  99  @Override 100  public final <T extends @Nullable Object> Future<T> submit(Callable<T> task) { 101  return delegate.submit(wrapTask(checkNotNull(task))); 102  } 103  104  @Override 105  public final Future<?> submit(Runnable task) { 106  return delegate.submit(wrapTask(task)); 107  } 108  109  @Override 110  public final <T extends @Nullable Object> Future<T> submit( 111  Runnable task, @ParametricNullness T result) { 112  return delegate.submit(wrapTask(task), result); 113  } 114  115  @Override 116  public final <T extends @Nullable Object> List<Future<T>> invokeAll( 117  Collection<? extends Callable<T>> tasks) throws InterruptedException { 118  return delegate.invokeAll(wrapTasks(tasks)); 119  } 120  121  @Override 122  public final <T extends @Nullable Object> List<Future<T>> invokeAll( 123  Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 124  throws InterruptedException { 125  return delegate.invokeAll(wrapTasks(tasks), timeout, unit); 126  } 127  128  @Override 129  public final <T extends @Nullable Object> T invokeAny(Collection<? extends Callable<T>> tasks) 130  throws InterruptedException, ExecutionException { 131  return delegate.invokeAny(wrapTasks(tasks)); 132  } 133  134  @Override 135  public final <T extends @Nullable Object> T invokeAny( 136  Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 137  throws InterruptedException, ExecutionException, TimeoutException { 138  return delegate.invokeAny(wrapTasks(tasks), timeout, unit); 139  } 140  141  // The remaining methods just delegate. 142  143  @Override 144  public final void shutdown() { 145  delegate.shutdown(); 146  } 147  148  @Override 149  public final List<Runnable> shutdownNow() { 150  return delegate.shutdownNow(); 151  } 152  153  @Override 154  public final boolean isShutdown() { 155  return delegate.isShutdown(); 156  } 157  158  @Override 159  public final boolean isTerminated() { 160  return delegate.isTerminated(); 161  } 162  163  @Override 164  public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { 165  return delegate.awaitTermination(timeout, unit); 166  } 167 }