15
15
*/
16
16
package rx .subscriptions ;
17
17
18
+ import rx .Subscription ;
19
+ import rx .exceptions .Exceptions ;
20
+
18
21
import java .util .ArrayList ;
19
22
import java .util .Arrays ;
20
23
import java .util .Collection ;
21
24
import java .util .HashSet ;
22
25
import java .util .List ;
23
26
import java .util .Set ;
24
27
25
- import rx .Subscription ;
26
- import rx .exceptions .*;
27
-
28
28
/**
29
29
* Subscription that represents a group of Subscriptions that are unsubscribed together.
30
30
* <p>
31
31
* All methods of this class are thread-safe.
32
32
*/
33
33
public final class CompositeSubscription implements Subscription {
34
-
34
+
35
35
private Set <Subscription > subscriptions ;
36
36
private volatile boolean unsubscribed ;
37
-
37
+
38
38
public CompositeSubscription () {
39
39
}
40
-
40
+
41
41
public CompositeSubscription (final Subscription ... subscriptions ) {
42
42
this .subscriptions = new HashSet <Subscription >(Arrays .asList (subscriptions ));
43
43
}
44
-
44
+
45
45
@ Override
46
46
public boolean isUnsubscribed () {
47
47
return unsubscribed ;
48
48
}
49
-
49
+
50
50
/**
51
51
* Adds a new {@link Subscription} to this {@code CompositeSubscription} if the
52
52
* {@code CompositeSubscription} is not yet unsubscribed. If the {@code CompositeSubscription} <em>is</em>
53
53
* unsubscribed, {@code add} will indicate this by explicitly unsubscribing the new {@code Subscription} as
54
54
* well.
55
55
*
56
56
* @param s
57
- * the {@link Subscription} to add
57
+ * the {@link Subscription} to add
58
58
*/
59
59
public void add (final Subscription s ) {
60
60
if (s .isUnsubscribed ()) {
@@ -74,13 +74,47 @@ public void add(final Subscription s) {
74
74
// call after leaving the synchronized block so we're not holding a lock while executing this
75
75
s .unsubscribe ();
76
76
}
77
-
77
+
78
+ /**
79
+ * Adds collection of {@link Subscription} to this {@code CompositeSubscription} if the
80
+ * {@code CompositeSubscription} is not yet unsubscribed. If the {@code CompositeSubscription} <em>is</em>
81
+ * unsubscribed, {@code addAll} will indicate this by explicitly unsubscribing all {@code Subscription} in collection as
82
+ * well.
83
+ *
84
+ * @param subscriptions
85
+ * the collection of {@link Subscription} to add
86
+ */
87
+ public void addAll (final Subscription ... subscriptions ) {
88
+ List <Subscription > subscriptionList = new ArrayList <Subscription >(4 );
89
+ for (Subscription s : subscriptions ) {
90
+ if (!s .isUnsubscribed ()) {
91
+ subscriptionList .add (s );
92
+ }
93
+ }
94
+
95
+ if (!unsubscribed ) {
96
+ synchronized (this ) {
97
+ if (!unsubscribed ) {
98
+ if (this .subscriptions == null ) {
99
+ this .subscriptions = new HashSet <Subscription >(4 );
100
+ }
101
+ this .subscriptions .addAll (subscriptionList );
102
+ return ;
103
+ }
104
+ }
105
+ }
106
+
107
+ for (Subscription s : subscriptionList ) {
108
+ s .unsubscribe ();
109
+ }
110
+ }
111
+
78
112
/**
79
113
* Removes a {@link Subscription} from this {@code CompositeSubscription}, and unsubscribes the
80
114
* {@link Subscription}.
81
115
*
82
116
* @param s
83
- * the {@link Subscription} to remove
117
+ * the {@link Subscription} to remove
84
118
*/
85
119
public void remove (final Subscription s ) {
86
120
if (!unsubscribed ) {
@@ -97,7 +131,7 @@ public void remove(final Subscription s) {
97
131
}
98
132
}
99
133
}
100
-
134
+
101
135
/**
102
136
* Unsubscribes any subscriptions that are currently part of this {@code CompositeSubscription} and remove
103
137
* them from the {@code CompositeSubscription} so that the {@code CompositeSubscription} is empty and
@@ -117,7 +151,7 @@ public void clear() {
117
151
unsubscribeFromAll (unsubscribe );
118
152
}
119
153
}
120
-
154
+
121
155
/**
122
156
* Unsubscribes itself and all inner subscriptions.
123
157
* <p>After call of this method, new {@code Subscription}s added to {@link CompositeSubscription}
@@ -139,7 +173,7 @@ public void unsubscribe() {
139
173
unsubscribeFromAll (unsubscribe );
140
174
}
141
175
}
142
-
176
+
143
177
private static void unsubscribeFromAll (Collection <Subscription > subscriptions ) {
144
178
if (subscriptions == null ) {
145
179
return ;
@@ -157,7 +191,7 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
157
191
}
158
192
Exceptions .throwIfAny (es );
159
193
}
160
-
194
+
161
195
/**
162
196
* Returns true if this composite is not unsubscribed and contains subscriptions.
163
197
*
0 commit comments