RxJava operator (7) ConditionalandBoolean. RxJava operator (7) ConditionalandBoolean in this article, let's take a look at the conditions and Boolean operators. 1. AllAmbAll operator RxJava operator (7) Conditional and Boolean transmitted by a function to the source Observable
In this article, let's take a look at the conditions and Boolean operators.
I. All/Amb
The All operator determines All data transmitted by the source Observable based on a function. The final result returned is this judgment result. This function uses the transmitted data as a parameter to internally determine whether all data meets the defined judgment conditions. If all data meets the conditions, true is returned; otherwise, false is returned.
Amb operators can combine up to nine Observable operators to compete with each other. Which Observable first emits data (including onError and onComplete) and then continues to launch the data of this Observable. Other data transmitted by the Observable will not be discarded.
The following two operators are used. we impose such restrictions on the all operator. when the tag is false for the first time, an Observable with 6 numbers will be created, an Observable with five numbers will be created in the future.
- private Observable allObserver() {
- Observable just;
- if (tag) {
- just = Observable.just(1, 2, 3, 4, 5);
- } else {
- just = Observable.just(1, 2, 3, 4, 5, 6);
- }
- tag = true;
- return just.all(integer -> integer < 6);
- }
- private Observable ambObserver() {
- Observable delay3 = Observable.just(1, 2, 3).delay(3000, TimeUnit.MILLISECONDS);
- Observable delay2 = Observable.just(4, 5, 6).delay(2000, TimeUnit.MILLISECONDS);
- Observable delay1 = Observable.just(7, 8, 9).delay(1000, TimeUnit.MILLISECONDS);
- return Observable.amb(delay1, delay2, delay3);
- }
Subscribe separately
- mLButton.setText("all");
- mLButton.setOnClickListener(e -> allObserver().subscribe(i -> log("all:" + i)));
- mRButton.setText("amb");
- mRButton.setOnClickListener(e -> ambObserver().subscribe(i -> log("amb:" + i)));
The running result is as follows. The Observable of the six data returned for the first time does not meet all the conditions smaller than 6. Therefore, the result is false, and all the subsequent conditions are met. Therefore, the result is true. Using the Observable of amb operator, the first data to be transmitted is 7, so 7, 8, and 9 are output, and other data is discarded.
II. Contains and IsEmpty
The Contains operator is used to determine whether the data transmitted by the source Observable Contains a certain data. if the data is included, true is returned. if the source Observable has ended, but the data is not yet released, false is returned.
The IsEmpty operator is used to determine whether the source Observable has transmitted data. if the source Observable has been transmitted, false is returned. if the source Observable has ended, but the source Observable has not yet released the data, true is returned.
Use these two operators to determine whether two Observable objects contain a certain data and whether they are empty.
- private Observable containsObserver() {
- if (tag) {
- return Observable.just(1, 2, 3).contains(3);
- }
- tag = true;
- return Observable.just(1, 2, 3).contains(4);
- }
- private Observable defaultObserver() {
- return Observable.create(new Observable.OnSubscribe () {
- @Override
- public void call(Subscriber subscriber) {
- subscriber.onCompleted();
- }
- }).isEmpty();
- }
Subscribe separately
- mLButton.setText("contains");
- mLButton.setOnClickListener(e -> containsObserver().subscribe(i -> log("contains:" + i)));
- mRButton.setText("isEmpty");
- mRButton.setOnClickListener(e -> defaultObserver().subscribe(i -> log("isEmpty:" + i)));
The running result is as follows:
III. DefaultIfEmpty
The DefaultIfEmpty operator determines whether the source Observable transmits data. if the source Observable emits data, the data is normally transmitted. if not, a default data is sent.
We use this operator to process an empty object and a non-empty object. if it is null, the default value 10 is returned.
- private Observable emptyObserver() {
- return Observable.create(new Observable.OnSubscribe () {
- @Override
- public void call(Subscriber subscriber) {
- subscriber.onCompleted();
- }
- }).defaultIfEmpty(10);
- }
- private Observable notEmptyObserver() {
- return Observable.create(new Observable.OnSubscribe () {
- @Override
- public void call(Subscriber subscriber) {
- subscriber.onNext(1);
- subscriber.onCompleted();
- }
- }).defaultIfEmpty(10);
- }
Subscribe separately
- mLButton.setText("empty");
- mLButton.setOnClickListener(e -> emptyObserver().subscribe(i -> log("empty:" + i)));
- mRButton.setText("notEmpty");
- mRButton.setOnClickListener(e -> notEmptyObserver().subscribe(i -> log("notEmpty:" + i)));
The running result is as follows:
IV. SequenceEqual
The SequenceEqual operator is used to determine whether two Observable data sequences are the same (the transmitted data is the same, the data sequence is the same, and the end state is the same). if the same, true is returned; otherwise, false is returned.
The following uses SequenceEqual to determine two identical and different Observable objects.
- private Observable equalObserver() {
- return Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2, 3));
- }
- private Observable notEqualObserver() {
- return Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2));
- }
Subscribe separately
- mLButton.setText("equal");
- mLButton.setOnClickListener(e -> equalObserver().subscribe(i -> log("equal:" + i)));
- mRButton.setText("notequal");
- mRButton.setOnClickListener(e -> notEqualObserver().subscribe(i -> log("notequal:" + i)));
The running result is as follows:
5. SkipUntil and SkipWhile
The two operators skip some data based on the condition. The difference is that SkipUnitl is determined based on the Observable flag. when the Observable flag does not transmit data, all data transmitted by the active Observable will be skipped. when the Observable emits a data, the data will be transmitted normally. SkipWhile is used to determine whether to skip data based on a function. when the return value of the function is true, the data transmitted from the source Observable is skipped. when the function returns false, data is transmitted normally.
The following two operators are used to skip some data items.
- private Observable skipUntilObserver() {
- return Observable.interval(1, TimeUnit.SECONDS).skipUntil(Observable.timer(3, TimeUnit.SECONDS));
- }
- private Observable skipWhileObserver() {
- return Observable.interval(1, TimeUnit.SECONDS).skipWhile(aLong -> aLong < 5);
- }
Subscribe separately
- mLButton.setText("skipUntil");
- mLButton.setOnClickListener(e -> skipUntilObserver().subscribe(i -> log("skipUntil:" + i)));
- mRButton.setText("skipWhile");
- mRButton.setOnClickListener(e -> skipWhileObserver().subscribe(i -> log("skipWhile:" + i)));
The running result is as follows:
6. TakeUntil and TakeWhile
The TakeUntil and TakeWhile operators are similar to the SkipUnitl and SkipWhile operators. TakeUntil is also used to determine whether the Observable transmits data. when the Mark Observable does not transmit data, the data is normally transmitted. once the mark Observable emits data, the subsequent data is discarded. TakeWhile determines whether to transmit data based on a function. when the return value of the function is true, data is normally transmitted. when the function returns false, all subsequent data is discarded.
The following two operators are used to take the data transmitted by two Observable nodes.
- private Observable takeUntilObserver() {
- return Observable.interval(1, TimeUnit.SECONDS).takeUntil(Observable.timer(3, TimeUnit.SECONDS));
- }
- private Observable takeWhileObserver() {
- return Observable.interval(1, TimeUnit.SECONDS).takeWhile(aLong -> aLong < 5);
- }
Subscribe separately
- mLButton.setText("takeUntil");
- mLButton.setOnClickListener(e -> takeUntilObserver().subscribe(i -> log("takeUntil:" + i)));
- mRButton.setText("takeWhile");
- mRButton.setOnClickListener(e -> takeWhileObserver().subscribe(i -> log("takeWhile:" + i)));
The running result is as follows:
Here is the condition and Boolean operator. for all the demo programs in this article, see: https://github.com/Chaoba/RxJavaDemo
Condition (7) Conditional and Boolean in this article we will take a look at the conditions and Boolean operators. I. The All/Amb All operator transmits data to the source Observable based on a function...