1616package rx .internal .operators ;
1717
1818import java .util .NoSuchElementException ;
19- import java .util .concurrent .atomic .AtomicBoolean ;
2019
2120import rx .Observable .Operator ;
22- import rx .Producer ;
2321import rx .Subscriber ;
22+ import rx .internal .producers .SingleProducer ;
23+ import rx .internal .util .RxJavaPluginUtils ;
2424
2525/**
2626 * 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) {
6565
6666 final ParentSubscriber <T > parent = new ParentSubscriber <T >(child , hasDefaultValue ,
6767 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- });
8168 child .add (parent );
8269 return parent ;
8370 }
@@ -88,23 +75,23 @@ private static final class ParentSubscriber<T> extends Subscriber<T> {
8875 private final T defaultValue ;
8976
9077 private T value ;
91- private boolean isNonEmpty = false ;
92- private boolean hasTooManyElements = false ;
78+ private boolean isNonEmpty ;
79+ private boolean hasTooManyElements ;
9380
9481
9582 ParentSubscriber (Subscriber <? super T > child , boolean hasDefaultValue ,
9683 T defaultValue ) {
9784 this .child = child ;
9885 this .hasDefaultValue = hasDefaultValue ;
9986 this .defaultValue = defaultValue ;
100- }
101-
102- void requestMore (long n ) {
103- request (n );
87+ request (2 ); // could go unbounded, but test expect this
10488 }
10589
10690 @ Override
10791 public void onNext (T value ) {
92+ if (hasTooManyElements ) {
93+ return ;
94+ } else
10895 if (isNonEmpty ) {
10996 hasTooManyElements = true ;
11097 child .onError (new IllegalArgumentException ("Sequence contains too many elements" ));
@@ -121,12 +108,10 @@ public void onCompleted() {
121108 // We have already sent an onError message
122109 } else {
123110 if (isNonEmpty ) {
124- child .onNext (value );
125- child .onCompleted ();
111+ child .setProducer (new SingleProducer <T >(child , value ));
126112 } else {
127113 if (hasDefaultValue ) {
128- child .onNext (defaultValue );
129- child .onCompleted ();
114+ child .setProducer (new SingleProducer <T >(child , defaultValue ));
130115 } else {
131116 child .onError (new NoSuchElementException ("Sequence contains no elements" ));
132117 }
@@ -136,6 +121,11 @@ public void onCompleted() {
136121
137122 @ Override
138123 public void onError (Throwable e ) {
124+ if (hasTooManyElements ) {
125+ RxJavaPluginUtils .handleException (e );
126+ return ;
127+ }
128+
139129 child .onError (e );
140130 }
141131
0 commit comments