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 java.util.LinkedHashSet;
20 import java.util.Set;
21
22
23
24
25
26
27
28
29
30
31
32 @Deprecated
33 public class PromiseAggregator<V, F extends Future<V>> implements GenericFutureListener<F> {
34
35 private final Promise<?> aggregatePromise;
36 private final boolean failPending;
37 private Set<Promise<V>> pendingPromises;
38
39
40
41
42
43
44
45 public PromiseAggregator(Promise<Void> aggregatePromise, boolean failPending) {
46 if (aggregatePromise == null) {
47 throw new NullPointerException("aggregatePromise");
48 }
49 this.aggregatePromise = aggregatePromise;
50 this.failPending = failPending;
51 }
52
53
54
55
56
57 public PromiseAggregator(Promise<Void> aggregatePromise) {
58 this(aggregatePromise, true);
59 }
60
61
62
63
64 @SafeVarargs
65 public final PromiseAggregator<V, F> add(Promise<V>... promises) {
66 if (promises == null) {
67 throw new NullPointerException("promises");
68 }
69 if (promises.length == 0) {
70 return this;
71 }
72 synchronized (this) {
73 if (pendingPromises == null) {
74 int size;
75 if (promises.length > 1) {
76 size = promises.length;
77 } else {
78 size = 2;
79 }
80 pendingPromises = new LinkedHashSet<Promise<V>>(size);
81 }
82 for (Promise<V> p : promises) {
83 if (p == null) {
84 continue;
85 }
86 pendingPromises.add(p);
87 p.addListener(this);
88 }
89 }
90 return this;
91 }
92
93 @Override
94 public synchronized void operationComplete(F future) throws Exception {
95 if (pendingPromises == null) {
96 aggregatePromise.setSuccess(null);
97 } else {
98 pendingPromises.remove(future);
99 if (!future.isSuccess()) {
100 Throwable cause = future.cause();
101 aggregatePromise.setFailure(cause);
102 if (failPending) {
103 for (Promise<V> pendingFuture : pendingPromises) {
104 pendingFuture.setFailure(cause);
105 }
106 }
107 } else {
108 if (pendingPromises.isEmpty()) {
109 aggregatePromise.setSuccess(null);
110 }
111 }
112 }
113 }
114
115 }