View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util.concurrent;
17  
18  /**
19   * {@link GenericFutureListener} implementation which takes other {@link Future}s
20   * and notifies them on completion.
21   *
22   * @param V the type of value returned by the future
23   * @param F the type of future
24   */
25  public class PromiseNotifier<V, F extends Future<V>> implements GenericFutureListener<F> {
26  
27      private final Promise<? super V>[] promises;
28  
29      /**
30       * Create a new instance.
31       *
32       * @param promises  the {@link Promise}s to notify once this {@link GenericFutureListener} is notified.
33       */
34      @SafeVarargs
35      public PromiseNotifier(Promise<? super V>... promises) {
36          if (promises == null) {
37              throw new NullPointerException("promises");
38          }
39          for (Promise<? super V> promise: promises) {
40              if (promise == null) {
41                  throw new IllegalArgumentException("promises contains null Promise");
42              }
43          }
44          this.promises = promises.clone();
45      }
46  
47      @Override
48      public void operationComplete(F future) throws Exception {
49          if (future.isSuccess()) {
50              V result = future.get();
51              for (Promise<? super V> p: promises) {
52                  p.setSuccess(result);
53              }
54              return;
55          }
56  
57          Throwable cause = future.cause();
58          for (Promise<? super V> p: promises) {
59              p.setFailure(cause);
60          }
61      }
62  
63  }