16
16
package rx .internal .operators ;
17
17
18
18
import java .util .NoSuchElementException ;
19
- import java .util .concurrent .atomic .AtomicBoolean ;
20
19
21
20
import rx .Observable .Operator ;
22
- import rx .Producer ;
23
21
import rx .Subscriber ;
22
+ import rx .internal .producers .SingleProducer ;
23
+ import rx .internal .util .RxJavaPluginUtils ;
24
24
25
25
/**
26
26
* If the Observable completes after emitting a single item that matches a
@@ -65,19 +65,6 @@ public Subscriber<? super T> call(final Subscriber<? super T> child) {
65
65
66
66
final ParentSubscriber <T > parent = new ParentSubscriber <T >(child , hasDefaultValue ,
67
67
defaultValue );
68
-
69
- child .setProducer (new Producer () {
70
-
71
- private final AtomicBoolean requestedTwo = new AtomicBoolean (false );
72
-
73
- @ Override
74
- public void request (long n ) {
75
- if (n > 0 && requestedTwo .compareAndSet (false , true )) {
76
- parent .requestMore (2 );
77
- }
78
- }
79
-
80
- });
81
68
child .add (parent );
82
69
return parent ;
83
70
}
@@ -88,23 +75,23 @@ private static final class ParentSubscriber<T> extends Subscriber<T> {
88
75
private final T defaultValue ;
89
76
90
77
private T value ;
91
- private boolean isNonEmpty = false ;
92
- private boolean hasTooManyElements = false ;
78
+ private boolean isNonEmpty ;
79
+ private boolean hasTooManyElements ;
93
80
94
81
95
82
ParentSubscriber (Subscriber <? super T > child , boolean hasDefaultValue ,
96
83
T defaultValue ) {
97
84
this .child = child ;
98
85
this .hasDefaultValue = hasDefaultValue ;
99
86
this .defaultValue = defaultValue ;
100
- }
101
-
102
- void requestMore (long n ) {
103
- request (n );
87
+ request (2 ); // could go unbounded, but test expect this
104
88
}
105
89
106
90
@ Override
107
91
public void onNext (T value ) {
92
+ if (hasTooManyElements ) {
93
+ return ;
94
+ } else
108
95
if (isNonEmpty ) {
109
96
hasTooManyElements = true ;
110
97
child .onError (new IllegalArgumentException ("Sequence contains too many elements" ));
@@ -121,12 +108,10 @@ public void onCompleted() {
121
108
// We have already sent an onError message
122
109
} else {
123
110
if (isNonEmpty ) {
124
- child .onNext (value );
125
- child .onCompleted ();
111
+ child .setProducer (new SingleProducer <T >(child , value ));
126
112
} else {
127
113
if (hasDefaultValue ) {
128
- child .onNext (defaultValue );
129
- child .onCompleted ();
114
+ child .setProducer (new SingleProducer <T >(child , defaultValue ));
130
115
} else {
131
116
child .onError (new NoSuchElementException ("Sequence contains no elements" ));
132
117
}
@@ -136,6 +121,11 @@ public void onCompleted() {
136
121
137
122
@ Override
138
123
public void onError (Throwable e ) {
124
+ if (hasTooManyElements ) {
125
+ RxJavaPluginUtils .handleException (e );
126
+ return ;
127
+ }
128
+
139
129
child .onError (e );
140
130
}
141
131
0 commit comments