15
15
*/
16
16
package rx .internal .operators ;
17
17
18
- import java .util .concurrent .atomic .AtomicLong ;
19
18
20
19
import rx .*;
20
+ import rx .internal .producers .ProducerArbiter ;
21
21
import rx .subscriptions .SerialSubscription ;
22
22
23
23
/**
@@ -35,36 +35,30 @@ public OperatorSwitchIfEmpty(Observable<? extends T> alternate) {
35
35
@ Override
36
36
public Subscriber <? super T > call (Subscriber <? super T > child ) {
37
37
final SerialSubscription ssub = new SerialSubscription ();
38
- final SwitchIfEmptySubscriber parent = new SwitchIfEmptySubscriber (child , ssub );
38
+ ProducerArbiter arbiter = new ProducerArbiter ();
39
+ final SwitchIfEmptySubscriber parent = new SwitchIfEmptySubscriber (child , ssub , arbiter );
39
40
ssub .set (parent );
40
41
child .add (ssub );
42
+ child .setProducer (arbiter );
41
43
return parent ;
42
44
}
43
45
44
46
private class SwitchIfEmptySubscriber extends Subscriber <T > {
45
47
46
- boolean empty = true ;
47
- final AtomicLong consumerCapacity = new AtomicLong (0l );
48
-
48
+ private boolean empty = true ;
49
49
private final Subscriber <? super T > child ;
50
- final SerialSubscription ssub ;
50
+ private final SerialSubscription ssub ;
51
+ private final ProducerArbiter arbiter ;
51
52
52
- public SwitchIfEmptySubscriber (Subscriber <? super T > child , final SerialSubscription ssub ) {
53
+ SwitchIfEmptySubscriber (Subscriber <? super T > child , final SerialSubscription ssub , ProducerArbiter arbiter ) {
53
54
this .child = child ;
54
55
this .ssub = ssub ;
56
+ this .arbiter = arbiter ;
55
57
}
56
58
57
59
@ Override
58
60
public void setProducer (final Producer producer ) {
59
- super .setProducer (new Producer () {
60
- @ Override
61
- public void request (long n ) {
62
- if (empty ) {
63
- consumerCapacity .set (n );
64
- }
65
- producer .request (n );
66
- }
67
- });
61
+ arbiter .setProducer (producer );
68
62
}
69
63
70
64
@ Override
@@ -77,41 +71,7 @@ public void onCompleted() {
77
71
}
78
72
79
73
private void subscribeToAlternate () {
80
- ssub .set (alternate .unsafeSubscribe (new Subscriber <T >() {
81
-
82
- @ Override
83
- public void setProducer (final Producer producer ) {
84
- child .setProducer (new Producer () {
85
- @ Override
86
- public void request (long n ) {
87
- producer .request (n );
88
- }
89
- });
90
- }
91
-
92
- @ Override
93
- public void onStart () {
94
- final long capacity = consumerCapacity .get ();
95
- if (capacity > 0 ) {
96
- request (capacity );
97
- }
98
- }
99
-
100
- @ Override
101
- public void onCompleted () {
102
- child .onCompleted ();
103
- }
104
-
105
- @ Override
106
- public void onError (Throwable e ) {
107
- child .onError (e );
108
- }
109
-
110
- @ Override
111
- public void onNext (T t ) {
112
- child .onNext (t );
113
- }
114
- }));
74
+ ssub .set (alternate .unsafeSubscribe (new AlternateSubscriber <T >(child , arbiter )));
115
75
}
116
76
117
77
@ Override
@@ -125,4 +85,36 @@ public void onNext(T t) {
125
85
child .onNext (t );
126
86
}
127
87
}
88
+
89
+ private static final class AlternateSubscriber <T > extends Subscriber <T > {
90
+
91
+ private final ProducerArbiter arbiter ;
92
+ private final Subscriber <? super T > child ;
93
+
94
+ AlternateSubscriber (Subscriber <? super T > child , ProducerArbiter arbiter ) {
95
+ this .child = child ;
96
+ this .arbiter = arbiter ;
97
+ }
98
+
99
+ @ Override
100
+ public void setProducer (final Producer producer ) {
101
+ arbiter .setProducer (producer );
102
+ }
103
+
104
+ @ Override
105
+ public void onCompleted () {
106
+ child .onCompleted ();
107
+ }
108
+
109
+ @ Override
110
+ public void onError (Throwable e ) {
111
+ child .onError (e );
112
+ }
113
+
114
+ @ Override
115
+ public void onNext (T t ) {
116
+ child .onNext (t );
117
+ arbiter .produced (1 );
118
+ }
119
+ }
128
120
}
0 commit comments