Migrate common/util to use JDT annotations
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / AsyncNotifyingListeningExecutorService.java
index 48b2a5a8ccfb0088078d911fc5b8346e1f52b13f..1be75009131ec3dc623bb6be7393ad6e2d692f88 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.util.concurrent;
 
 import static java.util.Objects.requireNonNull;
@@ -19,43 +18,35 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 
 /**
- * An {@link com.google.common.util.concurrent.ListeningExecutorService}
- * implementation that also allows for an {@link Executor} to be specified on
- * construction that is used to execute {@link ListenableFuture} callback
- * Runnables, registered via
- * {@link com.google.common.util.concurrent.Futures#addCallback} or
- * {@link ListenableFuture#addListener} directly, asynchronously when a task
- * that is run on this executor completes. This is useful when you want to
- * guarantee listener callback executions are off-loaded onto another thread to
- * avoid blocking the thread that completed the task, as a common use case is to
- * pass an executor that runs tasks in the same thread as the caller (ie
- * <code>MoreExecutors#sameThreadExecutor</code>}) to
- * {@link ListenableFuture#addListener}.
+ * An {@link com.google.common.util.concurrent.ListeningExecutorService} implementation that also allows
+ * for an {@link Executor} to be specified on construction that is used to execute {@link ListenableFuture} callback
+ * Runnables, registered via {@link com.google.common.util.concurrent.Futures#addCallback} or
+ * {@link ListenableFuture#addListener} directly, asynchronously when a task that is run on this executor completes.
+ * This is useful when you want to guarantee listener callback executions are off-loaded onto another thread to avoid
+ * blocking the thread that completed the task, as a common use case is to pass an executor that runs tasks in the same
+ * thread as the caller (i.e. {@code MoreExecutors#sameThreadExecutor}) to {@link ListenableFuture#addListener}.
  *
  * <p>
- * Most commonly, this class would be used in lieu of
- * <code>MoreExecutors#listeningDecorator</code> when the underlying delegate
- * Executor is single-threaded, in which case, you may not want ListenableFuture
- * callbacks to block the single thread.
+ * Most commonly, this class would be used in lieu of {@code MoreExecutors#listeningDecorator} when the underlying
+ * delegate Executor is single-threaded, in which case, you may not want ListenableFuture callbacks to block the single
+ * thread.
  *
  * <p>
- * Note: the Executor specified on construction does not replace the Executor
- * specified in {@link ListenableFuture#addListener}. The latter Executor is
- * still used however, if it is detected that the listener Runnable would
- * execute in the thread that completed the task, the listener is executed on
- * Executor specified on construction.
+ * Note: the Executor specified on construction does not replace the Executor specified
+ * in {@link ListenableFuture#addListener}. The latter Executor is still used however, if it is detected that
+ * the listener Runnable would execute in the thread that completed the task, the listener is executed on Executor
+ * specified on construction.
  *
  * @author Thomas Pantelis
  * @see AsyncNotifyingListenableFutureTask
  */
 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
-
-    private final ExecutorService delegate;
-    private final Executor listenableFutureExecutor;
+    private final @NonNull ExecutorService delegate;
+    private final @Nullable Executor listenableFutureExecutor;
 
     /**
      * Constructor.
@@ -64,39 +55,31 @@ public class AsyncNotifyingListeningExecutorService extends AbstractListeningExe
      * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
      *     If null, no executor is used.
      */
-    public AsyncNotifyingListeningExecutorService(final ExecutorService delegate,
+    public AsyncNotifyingListeningExecutorService(final @NonNull ExecutorService delegate,
             @Nullable final Executor listenableFutureExecutor) {
         this.delegate = requireNonNull(delegate);
         this.listenableFutureExecutor = listenableFutureExecutor;
     }
 
-    /**
-     * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
-     *
-     * @param task the Callable to execute
-     */
-    private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask(final Callable<T> task) {
-        return AsyncNotifyingListenableFutureTask.create(task, listenableFutureExecutor);
-    }
-
     /**
      * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
      *
      * @param task the Runnable to execute
      */
-    private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask(final Runnable task, final T result) {
+    private <T> @NonNull AsyncNotifyingListenableFutureTask<T> newFutureTask(final @NonNull Runnable task,
+            final T result) {
         return AsyncNotifyingListenableFutureTask.create(task, result, listenableFutureExecutor);
     }
 
     /**
      * Returns the delegate ExecutorService.
      */
-    protected ExecutorService getDelegate() {
+    protected @NonNull ExecutorService getDelegate() {
         return delegate;
     }
 
     @Override
-    public boolean awaitTermination(final long timeout, @Nonnull final TimeUnit unit) throws InterruptedException {
+    public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException {
         return delegate.awaitTermination(timeout, unit);
     }
 
@@ -115,48 +98,44 @@ public class AsyncNotifyingListeningExecutorService extends AbstractListeningExe
         delegate.shutdown();
     }
 
-    @Nonnull
     @Override
     public List<Runnable> shutdownNow() {
         return delegate.shutdownNow();
     }
 
     @Override
-    public void execute(@Nonnull final Runnable command) {
+    public void execute(final Runnable command) {
         delegate.execute(command);
     }
 
-    @Nonnull
     @Override
     public <T> ListenableFuture<T> submit(final Callable<T> task) {
-        AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask(task);
+        final AsyncNotifyingListenableFutureTask<T> futureTask = AsyncNotifyingListenableFutureTask.create(
+            requireNonNull(task), listenableFutureExecutor);
         delegate.execute(futureTask);
         return futureTask;
     }
 
-    @Nonnull
     @Override
     public ListenableFuture<?> submit(final Runnable task) {
-        AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask(task, null);
+        final AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask(requireNonNull(task), null);
         delegate.execute(futureTask);
         return futureTask;
     }
 
-    @Nonnull
     @Override
     public <T> ListenableFuture<T> submit(final Runnable task, final T result) {
-        AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask(task, result);
+        final AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask(requireNonNull(task), result);
         delegate.execute(futureTask);
         return futureTask;
     }
 
-    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+    protected @NonNull ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
         return toStringHelper;
     }
 
     @Override
     public final String toString() {
-        return addToStringAttributes(MoreObjects.toStringHelper(this)
-                .add("delegate", delegate)).toString();
+        return addToStringAttributes(MoreObjects.toStringHelper(this).add("delegate", delegate)).toString();
     }
 }