Home > Article > Backend Development > RxJava Operator (7) Conditional and Boolean_PHP Tutorial
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Boolean> allObserver() {<br /></li><li>Observable<Integer> just;<br /></li><li>if (tag) {<br /></li><li>just = Observable.just(1, 2, 3, 4, 5);<br /></li><li>} else {<br /></li><li>just = Observable.just(1, 2, 3, 4, 5, 6);<br /></li><li>}<br /></li><li>tag = true;<br /></li><li>return just.all(integer -> integer < 6);<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Integer> ambObserver() {<br /></li><li>Observable<Integer> delay3 = Observable.just(1, 2, 3).delay(3000, TimeUnit.MILLISECONDS);<br /></li><li>Observable<Integer> delay2 = Observable.just(4, 5, 6).delay(2000, TimeUnit.MILLISECONDS);<br /></li><li>Observable<Integer> delay1 = Observable.just(7, 8, 9).delay(1000, TimeUnit.MILLISECONDS);<br /></li><li>return Observable.amb(delay1, delay2, delay3);<br /></li><li>}</li></ol>Subscriptions
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("all");<br /></li><li>mLButton.setOnClickListener(e -> allObserver().subscribe(i -> log("all:" + i)));<br /></li><li>mRButton.setText("amb");<br /></li><li>mRButton.setOnClickListener(e -> ambObserver().subscribe(i -> log("amb:" + i)));</li></ol>are as follows. The Observable of the 6 data returned for the first time does not meet the condition that all of them are less than 6, so the result is false. The subsequent ones all meet the condition, so the result is true. Using the amb operator's Observable, the first emitted data is 7, so 7, 8, and 9 are output, and other data are discarded.
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Boolean> containsObserver() {<br /></li><li>if (tag) {<br /></li><li>return Observable.just(1, 2, 3).contains(3);<br /></li><li>}<br /></li><li>tag = true;<br /></li><li>return Observable.just(1, 2, 3).contains(4);<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Boolean> defaultObserver() {<br /></li><li>return Observable.create(new Observable.OnSubscribe<Integer>() {<br /></li><li>@Override<br /></li><li>public void call(Subscriber<? super Integer> subscriber) {<br /></li><li>subscriber.onCompleted();<br /></li><li>}<br /></li><li>}).isEmpty();<br /></li><li>}</li></ol>respectively Subscribe
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("contains");<br /></li><li>mLButton.setOnClickListener(e -> containsObserver().subscribe(i -> log("contains:" + i)));<br /></li><li>mRButton.setText("isEmpty");<br /></li><li>mRButton.setOnClickListener(e -> defaultObserver().subscribe(i -> log("isEmpty:" + i)));</li></ol>The operation results are as follows
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Integer> emptyObserver() {<br /></li><li>return Observable.create(new Observable.OnSubscribe<Integer>() {<br /></li><li>@Override<br /></li><li>public void call(Subscriber<? super Integer> subscriber) {<br /></li><li>subscriber.onCompleted();<br /></li><li>}<br /></li><li>}).defaultIfEmpty(10);<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Integer> notEmptyObserver() {<br /></li><li>return Observable.create(new Observable.OnSubscribe<Integer>() {<br /></li><li>@Override<br /></li><li>public void call(Subscriber<? super Integer> subscriber) {<br /></li><li>subscriber.onNext(1);<br /></li><li>subscriber.onCompleted();<br /></li><li>}<br /></li><li>}).defaultIfEmpty(10);<br /></li><li>}</li></ol>Subscribe separately
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("empty");<br /></li><li>mLButton.setOnClickListener(e -> emptyObserver().subscribe(i -> log("empty:" + i)));<br /></li><li>mRButton.setText("notEmpty");<br /></li><li>mRButton.setOnClickListener(e -> notEmptyObserver().subscribe(i -> log("notEmpty:" + i)));</li></ol>The running results are as follows
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Boolean> equalObserver() {<br /></li><li>return Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2, 3));<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Boolean> notEqualObserver() {<br /></li><li>return Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2));<br /></li><li>}</li></ol>and subscribe separately
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("equal");<br /></li><li>mLButton.setOnClickListener(e -> equalObserver().subscribe(i -> log("equal:" + i)));<br /></li><li>mRButton.setText("notequal");<br /></li><li>mRButton.setOnClickListener(e -> notEqualObserver().subscribe(i -> log("notequal:" + i)));</li></ol>The running results are as follows
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Long> skipUntilObserver() {<br /></li><li>return Observable.interval(1, TimeUnit.SECONDS).skipUntil(Observable.timer(3, TimeUnit.SECONDS));<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Long> skipWhileObserver() {<br /></li><li>return Observable.interval(1, TimeUnit.SECONDS).skipWhile(aLong -> aLong < 5);<br /></li><li>}</li></ol>Subscribe separately
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("skipUntil");<br /></li><li>mLButton.setOnClickListener(e -> skipUntilObserver().subscribe(i -> log("skipUntil:" + i)));<br /></li><li>mRButton.setText("skipWhile");<br /></li><li>mRButton.setOnClickListener(e -> skipWhileObserver().subscribe(i -> log("skipWhile:" + i)));</li></ol>运行结果如下
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>private Observable<Long> takeUntilObserver() {<br /></li><li>return Observable.interval(1, TimeUnit.SECONDS).takeUntil(Observable.timer(3, TimeUnit.SECONDS));<br /></li><li>}<br /></li><li><br /></li><li>private Observable<Long> takeWhileObserver() {<br /></li><li>return Observable.interval(1, TimeUnit.SECONDS).takeWhile(aLong -> aLong < 5);<br /></li><li>}</li></ol>分别进行订阅
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>mLButton.setText("takeUntil");<br /></li><li>mLButton.setOnClickListener(e -> takeUntilObserver().subscribe(i -> log("takeUntil:" + i)));<br /></li><li>mRButton.setText("takeWhile");<br /></li><li>mRButton.setOnClickListener(e -> takeWhileObserver().subscribe(i -> log("takeWhile:" + i)));</li></ol>运行结果如下