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    *   https://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 io.netty.util.internal.ObjectUtil;
20  
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  /**
25   * @deprecated Use {@link PromiseCombiner#PromiseCombiner(EventExecutor)}.
26   *
27   * {@link GenericFutureListener} implementation which consolidates multiple {@link Future}s
28   * into one, by listening to individual {@link Future}s and producing an aggregated result
29   * (success/failure) when all {@link Future}s have completed.
30   *
31   * @param <V> the type of value returned by the {@link Future}
32   * @param <F> the type of {@link Future}
33   */
34  @Deprecated
35  public class PromiseAggregator<V, F extends Future<V>> implements GenericFutureListener<F> {
36  
37      private final Promise<?> aggregatePromise;
38      private final boolean failPending;
39      private Set<Promise<V>> pendingPromises;
40  
41      /**
42       * Creates a new instance.
43       *
44       * @param aggregatePromise  the {@link Promise} to notify
45       * @param failPending  {@code true} to fail pending promises, false to leave them unaffected
46       */
47      public PromiseAggregator(Promise<Void> aggregatePromise, boolean failPending) {
48          this.aggregatePromise = ObjectUtil.checkNotNull(aggregatePromise, "aggregatePromise");
49          this.failPending = failPending;
50      }
51  
52      /**
53       * See {@link PromiseAggregator#PromiseAggregator(Promise, boolean)}.
54       * Defaults {@code failPending} to true.
55       */
56      public PromiseAggregator(Promise<Void> aggregatePromise) {
57          this(aggregatePromise, true);
58      }
59  
60      /**
61       * Add the given {@link Promise}s to the aggregator.
62       */
63      @SafeVarargs
64      public final PromiseAggregator<V, F> add(Promise<V>... promises) {
65          ObjectUtil.checkNotNull(promises, "promises");
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 }