1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28
29
30
31
32
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
43
44
45
46
47 public PromiseAggregator(Promise<Void> aggregatePromise, boolean failPending) {
48 this.aggregatePromise = ObjectUtil.checkNotNull(aggregatePromise, "aggregatePromise");
49 this.failPending = failPending;
50 }
51
52
53
54
55
56 public PromiseAggregator(Promise<Void> aggregatePromise) {
57 this(aggregatePromise, true);
58 }
59
60
61
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 }