Migrate common/util to use JDT annotations
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / AsyncNotifyingListeningExecutorService.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util.concurrent;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.util.concurrent.AbstractListeningExecutorService;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.List;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.Executor;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * An {@link com.google.common.util.concurrent.ListeningExecutorService} implementation that also allows
26  * for an {@link Executor} to be specified on construction that is used to execute {@link ListenableFuture} callback
27  * Runnables, registered via {@link com.google.common.util.concurrent.Futures#addCallback} or
28  * {@link ListenableFuture#addListener} directly, asynchronously when a task that is run on this executor completes.
29  * This is useful when you want to guarantee listener callback executions are off-loaded onto another thread to avoid
30  * blocking the thread that completed the task, as a common use case is to pass an executor that runs tasks in the same
31  * thread as the caller (i.e. {@code MoreExecutors#sameThreadExecutor}) to {@link ListenableFuture#addListener}.
32  *
33  * <p>
34  * Most commonly, this class would be used in lieu of {@code MoreExecutors#listeningDecorator} when the underlying
35  * delegate Executor is single-threaded, in which case, you may not want ListenableFuture callbacks to block the single
36  * thread.
37  *
38  * <p>
39  * Note: the Executor specified on construction does not replace the Executor specified
40  * in {@link ListenableFuture#addListener}. The latter Executor is still used however, if it is detected that
41  * the listener Runnable would execute in the thread that completed the task, the listener is executed on Executor
42  * specified on construction.
43  *
44  * @author Thomas Pantelis
45  * @see AsyncNotifyingListenableFutureTask
46  */
47 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
48     private final @NonNull ExecutorService delegate;
49     private final @Nullable Executor listenableFutureExecutor;
50
51     /**
52      * Constructor.
53      *
54      * @param delegate the back-end ExecutorService.
55      * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
56      *     If null, no executor is used.
57      */
58     public AsyncNotifyingListeningExecutorService(final @NonNull ExecutorService delegate,
59             @Nullable final Executor listenableFutureExecutor) {
60         this.delegate = requireNonNull(delegate);
61         this.listenableFutureExecutor = listenableFutureExecutor;
62     }
63
64     /**
65      * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
66      *
67      * @param task the Runnable to execute
68      */
69     private <T> @NonNull AsyncNotifyingListenableFutureTask<T> newFutureTask(final @NonNull Runnable task,
70             final T result) {
71         return AsyncNotifyingListenableFutureTask.create(task, result, listenableFutureExecutor);
72     }
73
74     /**
75      * Returns the delegate ExecutorService.
76      */
77     protected @NonNull ExecutorService getDelegate() {
78         return delegate;
79     }
80
81     @Override
82     public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException {
83         return delegate.awaitTermination(timeout, unit);
84     }
85
86     @Override
87     public boolean isShutdown() {
88         return delegate.isShutdown();
89     }
90
91     @Override
92     public boolean isTerminated() {
93         return delegate.isTerminated();
94     }
95
96     @Override
97     public void shutdown() {
98         delegate.shutdown();
99     }
100
101     @Override
102     public List<Runnable> shutdownNow() {
103         return delegate.shutdownNow();
104     }
105
106     @Override
107     public void execute(final Runnable command) {
108         delegate.execute(command);
109     }
110
111     @Override
112     public <T> ListenableFuture<T> submit(final Callable<T> task) {
113         final AsyncNotifyingListenableFutureTask<T> futureTask = AsyncNotifyingListenableFutureTask.create(
114             requireNonNull(task), listenableFutureExecutor);
115         delegate.execute(futureTask);
116         return futureTask;
117     }
118
119     @Override
120     public ListenableFuture<?> submit(final Runnable task) {
121         final AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask(requireNonNull(task), null);
122         delegate.execute(futureTask);
123         return futureTask;
124     }
125
126     @Override
127     public <T> ListenableFuture<T> submit(final Runnable task, final T result) {
128         final AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask(requireNonNull(task), result);
129         delegate.execute(futureTask);
130         return futureTask;
131     }
132
133     protected @NonNull ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
134         return toStringHelper;
135     }
136
137     @Override
138     public final String toString() {
139         return addToStringAttributes(MoreObjects.toStringHelper(this).add("delegate", delegate)).toString();
140     }
141 }