Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.util.concurrent;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.List;
An abstract ExecutorService that allows subclasses to wrap tasks before they are submitted to the underlying executor.

Note that task wrapping may occur even if the task is never executed.

For delegation without task-wrapping, see ForwardingExecutorService.

Author(s):
Chris Nokleberg
 
 abstract class WrappingExecutorService implements ExecutorService {
   private final ExecutorService delegate;
 
   protected WrappingExecutorService(ExecutorService delegate) {
     this. = checkNotNull(delegate);
   }

  
Wraps a task before it is submitted to the underlying Executor. Though specified in terms of Callable, this method is also applied to Runnable tasks.
 
   protected abstract <T> Callable<T> wrapTask(Callable<T> callable);

  
Wraps a task before it is submitted to the underlying Executor. This delegates to wrapTask(java.util.concurrent.Callable) by default, but should be overridden if there is a better implementation for runnables.
 
   protected Runnable wrapTask(Runnable command) {
     final Callable<Objectwrapped = wrapTask(
         Executors.callable(commandnull));
     return new Runnable() {
       @Override public void run() {
         try {
           wrapped.call();
         } catch (Exception e) {
           Throwables.propagate(e);
         }
       }
     };
   }

  
Wraps a collection of tasks.

Throws:
java.lang.NullPointerException if any element of tasks is null
 
   private final <T> ImmutableList<Callable<T>> wrapTasks(
       Collection<? extends Callable<T>> tasks) {
     ImmutableList.Builder<Callable<T>> builder = ImmutableList.builder();
     for (Callable<T> task : tasks) {
       builder.add(wrapTask(task));
     }
     return builder.build();
   }
 
   // These methods wrap before delegating.
   @Override
   public final void execute(Runnable command) {
     .execute(wrapTask(command));
   }
 
  public final <T> Future<T> submit(Callable<T> task) {
    return .submit(wrapTask(checkNotNull(task)));
  }
  public final Future<?> submit(Runnable task) {
    return .submit(wrapTask(task));
  }
  public final <T> Future<T> submit(Runnable task, T result) {
    return .submit(wrapTask(task), result);
  }
  public final <T> List<Future<T>> invokeAll(
      Collection<? extends Callable<T>> tasksthrows InterruptedException {
    return .invokeAll(wrapTasks(tasks));
  }
  public final <T> List<Future<T>> invokeAll(
      Collection<? extends Callable<T>> taskslong timeoutTimeUnit unit)
      throws InterruptedException {
    return .invokeAll(wrapTasks(tasks), timeoutunit);
  }
  public final <T> T invokeAny(Collection<? extends Callable<T>> tasks)
    return .invokeAny(wrapTasks(tasks));
  }
  public final <T> T invokeAny(
      Collection<? extends Callable<T>> taskslong timeoutTimeUnit unit)
    return .invokeAny(wrapTasks(tasks), timeoutunit);
  }
  // The remaining methods just delegate.
  public final void shutdown() {
  }
  public final List<RunnableshutdownNow() {
    return .shutdownNow();
  }
  public final boolean isShutdown() {
    return .isShutdown();
  }
  public final boolean isTerminated() {
    return .isTerminated();
  }
  public final boolean awaitTermination(long timeoutTimeUnit unit)
      throws InterruptedException {
    return .awaitTermination(timeoutunit);
  }
New to GrepCode? Check out our FAQ X