17
17
18
18
import io .reactivex .exceptions .Exceptions ;
19
19
import io .reactivex .functions .Predicate ;
20
+ import io .reactivex .internal .fuseable .ConditionalSubscriber ;
20
21
import io .reactivex .internal .subscriptions .SubscriptionHelper ;
21
22
import io .reactivex .parallel .ParallelFlowable ;
22
23
import io .reactivex .plugins .RxJavaPlugins ;
@@ -48,7 +49,12 @@ public void subscribe(Subscriber<? super T>[] subscribers) {
48
49
Subscriber <? super T >[] parents = new Subscriber [n ];
49
50
50
51
for (int i = 0 ; i < n ; i ++) {
51
- parents [i ] = new ParallelFilterSubscriber <T >(subscribers [i ], predicate );
52
+ Subscriber <? super T > a = subscribers [i ];
53
+ if (a instanceof ConditionalSubscriber ) {
54
+ parents [i ] = new ParallelFilterConditionalSubscriber <T >((ConditionalSubscriber <? super T >)a , predicate );
55
+ } else {
56
+ parents [i ] = new ParallelFilterSubscriber <T >(a , predicate );
57
+ }
52
58
}
53
59
54
60
source .subscribe (parents );
@@ -59,31 +65,44 @@ public int parallelism() {
59
65
return source .parallelism ();
60
66
}
61
67
62
- static final class ParallelFilterSubscriber <T > implements Subscriber <T >, Subscription {
63
-
64
- final Subscriber <? super T > actual ;
65
-
68
+ abstract static class BaseFilterSubscriber <T > implements ConditionalSubscriber <T >, Subscription {
66
69
final Predicate <? super T > predicate ;
67
70
68
71
Subscription s ;
69
72
70
73
boolean done ;
71
74
72
- ParallelFilterSubscriber (Subscriber <? super T > actual , Predicate <? super T > predicate ) {
73
- this .actual = actual ;
75
+ BaseFilterSubscriber (Predicate <? super T > predicate ) {
74
76
this .predicate = predicate ;
75
77
}
76
78
77
79
@ Override
78
- public void request (long n ) {
80
+ public final void request (long n ) {
79
81
s .request (n );
80
82
}
81
83
82
84
@ Override
83
- public void cancel () {
85
+ public final void cancel () {
84
86
s .cancel ();
85
87
}
86
88
89
+ @ Override
90
+ public final void onNext (T t ) {
91
+ if (!tryOnNext (t )) {
92
+ s .request (1 );
93
+ }
94
+ }
95
+ }
96
+
97
+ static final class ParallelFilterSubscriber <T > extends BaseFilterSubscriber <T > {
98
+
99
+ final Subscriber <? super T > actual ;
100
+
101
+ ParallelFilterSubscriber (Subscriber <? super T > actual , Predicate <? super T > predicate ) {
102
+ super (predicate );
103
+ this .actual = actual ;
104
+ }
105
+
87
106
@ Override
88
107
public void onSubscribe (Subscription s ) {
89
108
if (SubscriptionHelper .validate (this .s , s )) {
@@ -94,26 +113,83 @@ public void onSubscribe(Subscription s) {
94
113
}
95
114
96
115
@ Override
97
- public void onNext (T t ) {
116
+ public boolean tryOnNext (T t ) {
117
+ if (!done ) {
118
+ boolean b ;
119
+
120
+ try {
121
+ b = predicate .test (t );
122
+ } catch (Throwable ex ) {
123
+ Exceptions .throwIfFatal (ex );
124
+ cancel ();
125
+ onError (ex );
126
+ return false ;
127
+ }
128
+
129
+ if (b ) {
130
+ actual .onNext (t );
131
+ return true ;
132
+ }
133
+ }
134
+ return false ;
135
+ }
136
+
137
+ @ Override
138
+ public void onError (Throwable t ) {
98
139
if (done ) {
140
+ RxJavaPlugins .onError (t );
99
141
return ;
100
142
}
101
- boolean b ;
102
-
103
- try {
104
- b = predicate . test ( t );
105
- } catch ( Throwable ex ) {
106
- Exceptions . throwIfFatal ( ex );
107
- cancel ();
108
- onError ( ex ) ;
109
- return ;
143
+ done = true ;
144
+ actual . onError ( t );
145
+ }
146
+
147
+ @ Override
148
+ public void onComplete () {
149
+ if (! done ) {
150
+ done = true ;
151
+ actual . onComplete () ;
110
152
}
153
+ }
154
+ }
111
155
112
- if (b ) {
113
- actual .onNext (t );
114
- } else {
115
- s .request (1 );
156
+ static final class ParallelFilterConditionalSubscriber <T > extends BaseFilterSubscriber <T > {
157
+
158
+ final ConditionalSubscriber <? super T > actual ;
159
+
160
+ ParallelFilterConditionalSubscriber (ConditionalSubscriber <? super T > actual , Predicate <? super T > predicate ) {
161
+ super (predicate );
162
+ this .actual = actual ;
163
+ }
164
+
165
+ @ Override
166
+ public void onSubscribe (Subscription s ) {
167
+ if (SubscriptionHelper .validate (this .s , s )) {
168
+ this .s = s ;
169
+
170
+ actual .onSubscribe (this );
171
+ }
172
+ }
173
+
174
+ @ Override
175
+ public boolean tryOnNext (T t ) {
176
+ if (!done ) {
177
+ boolean b ;
178
+
179
+ try {
180
+ b = predicate .test (t );
181
+ } catch (Throwable ex ) {
182
+ Exceptions .throwIfFatal (ex );
183
+ cancel ();
184
+ onError (ex );
185
+ return false ;
186
+ }
187
+
188
+ if (b ) {
189
+ return actual .tryOnNext (t );
190
+ }
116
191
}
192
+ return false ;
117
193
}
118
194
119
195
@ Override
@@ -128,12 +204,9 @@ public void onError(Throwable t) {
128
204
129
205
@ Override
130
206
public void onComplete () {
131
- if (done ) {
132
- return ;
207
+ if (!done ) {
208
+ done = true ;
209
+ actual .onComplete ();
133
210
}
134
- done = true ;
135
- actual .onComplete ();
136
211
}
137
-
138
- }
139
- }
212
+ }}
0 commit comments