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  
17  package io.netty.util.concurrent;
18  
19  import java.util.LinkedHashSet;
20  import java.util.Set;
21  
22  /**
23   * {@link GenericFutureListener} implementation which consolidates multiple {@link Future}s
24   * into one, by listening to individual {@link Future}s and producing an aggregated result
25   * (success/failure) when all {@link Future}s have completed.
26   *
27   * @param V the type of value returned by the {@link Future}
28   * @param F the type of {@link Future}
29   */
30  public class PromiseAggregator<V, F extends Future<V>> implements GenericFutureListener<F> {
31  
32      private final Promise<?> aggregatePromise;
33      private final boolean failPending;
34      private Set<Promise<V>> pendingPromises;
35  
36      /**
37       * Creates a new instance.
38       *
39       * @param aggregatePromise  the {@link Promise} to notify
40       * @param failPending  {@code true} to fail pending promises, false to leave them unaffected
41       */
42      public PromiseAggregator(Promise<Void> aggregatePromise, boolean failPending) {
43          if (aggregatePromise == null) {
44              throw new NullPointerException("aggregatePromise");
45          }
46          this.aggregatePromise = aggregatePromise;
47          this.failPending = failPending;
48      }
49  
50      /**
51       * See {@link PromiseAggregator#PromiseAggregator(Promise, boolean)}.
52       * Defaults {@code failPending} to true.
53       */
54      public PromiseAggregator(Promise<Void> aggregatePromise) {
55          this(aggregatePromise, true);
56      }
57  
58      /**
59       * Add the given {@link Promise}s to the aggregator.
60       */
61      @SafeVarargs
62      public final PromiseAggregator<V, F> add(Promise<V>... promises) {
63          if (promises == null) {
64              throw new NullPointerException("promises");
65          }
66          if (promises.length == 0) {
67              return this;
68          }
69          synchronized (this) {
70              if (pendingPromises == null) {
71                  int size;
72                  if (promises.length > 1) {
73                      size = promises.length;
74                  } else {
75                      size = 2;
76                  }
77                  pendingPromises = new LinkedHashSet<Promise<V>>(size);
78              }
79              for (Promise<V> p : promises) {
80                  if (p == null) {
81                      continue;
82                  }
83                  pendingPromises.add(p);
84                  p.addListener(this);
85              }
86          }
87          return this;
88      }
89  
90      @Override
91      public synchronized void operationComplete(F future) throws Exception {
92          if (pendingPromises == null) {
93              aggregatePromise.setSuccess(null);
94          } else {
95              pendingPromises.remove(future);
96              if (!future.isSuccess()) {
97                  Throwable cause = future.cause();
98                  aggregatePromise.setFailure(cause);
99                  if (failPending) {
100                     for (Promise<V> pendingFuture : pendingPromises) {
101                         pendingFuture.setFailure(cause);
102                     }
103                 }
104             } else {
105                 if (pendingPromises.isEmpty()) {
106                     aggregatePromise.setSuccess(null);
107                 }
108             }
109         }
110     }
111 
112 }