Propagate @Nonnull and @Nullable 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
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
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 javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23
24 /**
25  * An {@link com.google.common.util.concurrent.ListeningExecutorService}
26  * implementation that also allows for an {@link Executor} to be specified on
27  * construction that is used to execute {@link ListenableFuture} callback
28  * Runnables, registered via
29  * {@link com.google.common.util.concurrent.Futures#addCallback} or
30  * {@link ListenableFuture#addListener} directly, asynchronously when a task
31  * that is run on this executor completes. This is useful when you want to
32  * guarantee listener callback executions are off-loaded onto another thread to
33  * avoid blocking the thread that completed the task, as a common use case is to
34  * pass an executor that runs tasks in the same thread as the caller (ie
35  * <code>MoreExecutors#sameThreadExecutor</code>}) to
36  * {@link ListenableFuture#addListener}.
37  *
38  * <p>
39  * Most commonly, this class would be used in lieu of
40  * <code>MoreExecutors#listeningDecorator</code> when the underlying delegate
41  * Executor is single-threaded, in which case, you may not want ListenableFuture
42  * callbacks to block the single thread.
43  *
44  * <p>
45  * Note: the Executor specified on construction does not replace the Executor
46  * specified in {@link ListenableFuture#addListener}. The latter Executor is
47  * still used however, if it is detected that the listener Runnable would
48  * execute in the thread that completed the task, the listener is executed on
49  * Executor specified on construction.
50  *
51  * @author Thomas Pantelis
52  * @see AsyncNotifyingListenableFutureTask
53  */
54 public class AsyncNotifyingListeningExecutorService extends AbstractListeningExecutorService {
55
56     private final ExecutorService delegate;
57     private final Executor listenableFutureExecutor;
58
59     /**
60      * Constructor.
61      *
62      * @param delegate the back-end ExecutorService.
63      * @param listenableFutureExecutor the executor used to run listener callbacks asynchronously.
64      *     If null, no executor is used.
65      */
66     public AsyncNotifyingListeningExecutorService( final ExecutorService delegate,
67             @Nullable final Executor listenableFutureExecutor ) {
68         this.delegate = Preconditions.checkNotNull( delegate );
69         this.listenableFutureExecutor = listenableFutureExecutor;
70     }
71
72     /**
73      * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
74      *
75      * @param task the Callable to execute
76      */
77     private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Callable<T> task ) {
78         return AsyncNotifyingListenableFutureTask.create( task, listenableFutureExecutor );
79     }
80
81     /**
82      * Creates an {@link AsyncNotifyingListenableFutureTask} instance with the listener Executor.
83      *
84      * @param task the Runnable to execute
85      */
86     private <T> AsyncNotifyingListenableFutureTask<T> newFutureTask( final Runnable task, final T result ) {
87         return AsyncNotifyingListenableFutureTask.create( task, result, listenableFutureExecutor );
88     }
89
90     /**
91      * Returns the delegate ExecutorService.
92      */
93     protected ExecutorService getDelegate() {
94         return delegate;
95     }
96
97     @Override
98     public boolean awaitTermination( final long timeout, @Nonnull final TimeUnit unit ) throws InterruptedException {
99         return delegate.awaitTermination( timeout, unit );
100     }
101
102     @Override
103     public boolean isShutdown() {
104         return delegate.isShutdown();
105     }
106
107     @Override
108     public boolean isTerminated() {
109         return delegate.isTerminated();
110     }
111
112     @Override
113     public void shutdown() {
114         delegate.shutdown();
115     }
116
117     @Nonnull
118     @Override
119     public List<Runnable> shutdownNow() {
120         return delegate.shutdownNow();
121     }
122
123     @Override
124     public void execute( @Nonnull final Runnable command ) {
125         delegate.execute( command );
126     }
127
128     @Nonnull
129     @Override
130     public <T> ListenableFuture<T> submit( final Callable<T> task ) {
131         AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task );
132         delegate.execute( futureTask );
133         return futureTask;
134     }
135
136     @Nonnull
137     @Override
138     public ListenableFuture<?> submit( final Runnable task ) {
139         AsyncNotifyingListenableFutureTask<Void> futureTask = newFutureTask( task, null );
140         delegate.execute( futureTask );
141         return futureTask;
142     }
143
144     @Nonnull
145     @Override
146     public <T> ListenableFuture<T> submit( final Runnable task, final T result ) {
147         AsyncNotifyingListenableFutureTask<T> futureTask = newFutureTask( task, result );
148         delegate.execute( futureTask );
149         return futureTask;
150     }
151
152     protected ToStringHelper addToStringAttributes( final ToStringHelper toStringHelper ) {
153         return toStringHelper;
154     }
155
156     @Override
157     public final String toString() {
158         return addToStringAttributes( MoreObjects.toStringHelper( this )
159                 .add( "delegate", delegate ) ).toString();
160     }
161 }