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  import io.netty.util.internal.PromiseNotificationUtil;
19  import io.netty.util.internal.logging.InternalLogger;
20  import io.netty.util.internal.logging.InternalLoggerFactory;
21  
22  import static io.netty.util.internal.ObjectUtil.checkNotNull;
23  
24  /**
25   * {@link GenericFutureListener} implementation which takes other {@link Promise}s
26   * and notifies them on completion.
27   *
28   * @param <V> the type of value returned by the future
29   * @param <F> the type of future
30   */
31  public class PromiseNotifier<V, F extends Future<V>> implements GenericFutureListener<F> {
32  
33      private static final InternalLogger logger = InternalLoggerFactory.getInstance(PromiseNotifier.class);
34      private final Promise<? super V>[] promises;
35      private final boolean logNotifyFailure;
36  
37      /**
38       * Create a new instance.
39       *
40       * @param promises  the {@link Promise}s to notify once this {@link GenericFutureListener} is notified.
41       */
42      @SafeVarargs
43      public PromiseNotifier(Promise<? super V>... promises) {
44          this(true, promises);
45      }
46  
47      /**
48       * Create a new instance.
49       *
50       * @param logNotifyFailure {@code true} if logging should be done in case notification fails.
51       * @param promises  the {@link Promise}s to notify once this {@link GenericFutureListener} is notified.
52       */
53      @SafeVarargs
54      public PromiseNotifier(boolean logNotifyFailure, Promise<? super V>... promises) {
55          checkNotNull(promises, "promises");
56          for (Promise<? super V> promise: promises) {
57              if (promise == null) {
58                  throw new IllegalArgumentException("promises contains null Promise");
59              }
60          }
61          this.promises = promises.clone();
62          this.logNotifyFailure = logNotifyFailure;
63      }
64  
65      @Override
66      public void operationComplete(F future) throws Exception {
67          InternalLogger internalLogger = logNotifyFailure ? logger : null;
68          if (future.isSuccess()) {
69              V result = future.get();
70              for (Promise<? super V> p: promises) {
71                  PromiseNotificationUtil.trySuccess(p, result, internalLogger);
72              }
73          } else if (future.isCancelled()) {
74              for (Promise<? super V> p: promises) {
75                  PromiseNotificationUtil.tryCancel(p, internalLogger);
76              }
77          } else {
78              Throwable cause = future.cause();
79              for (Promise<? super V> p: promises) {
80                  PromiseNotificationUtil.tryFailure(p, cause, internalLogger);
81              }
82          }
83      }
84  }