Rxjava condition and Boolean operator __java

Source: Internet
Author: User
Tags emit terminates throwable

In Rxjava, conditions and Boolean Operators act on a data source in observable, such as whether the data in the data source satisfies a condition, whether the data source is empty, or how to launch the backup data when the data source is empty. In the filtering operation Fu Zeng understand the use of take, skip operators, whether we set a certain implementation conditions for them. All Flow Chart

Overview

The all operator determines whether all data emitted by the original observable satisfies a certain condition.

The all operator receives a function parameter, creates and returns a observable of a single Boolean value, returns True if the original observable terminates normally and every data satisfies the condition. If any of the original observable data does not meet the condition or does not terminate normally, return false.

The all operator is not executed on any particular scheduler by default. API

Javadoc:all (FUNC1)
Sample Code
Observable.from (Getlistofstudent ())
        . Subscribeon (Schedulers.io ())
        . All (new func1<student, boolean> () {
            @Override public
            Boolean Call (Student Student) {return
                student.getage () > && Stutdent.getage () <
            }
        }). Observeon (Androidschedulers.mainthread ())
        . Subscribe (New subscriber<boolean> () {
            @Override
            public void oncompleted () {

            }

            @Override public
            void OnError (Throwable e) {

            }

            @Override
            public void OnNext (Boolean aboolean) {
                log.i (TAG, "All ():" + Aboolean);

            }
        });
Log Print
All (): True
Sample Resolution

In the example, each of the data emitted by observable, the student object, is determined by the all operator to determine whether the member variable age of each student object is between 16 and 40, at which point the all operator creates a single Boolean observabl. After a closer look at log printing, the Observer subscribes to a Boolean that evaluates to True, indicating that the original observable can terminate normally, and that every data emitted is satisfied with the requirement condition. Amb Flow Chart

Overview

The AMB operator, for a given two or more observables, launches all the data for the observable that first launches the data or the notification.

When you pass multiple observable to the AMB operator, the operator launches only one of the observable data and notifications: the observable that first sends the notification to the AMB operator, Regardless of whether the launch is a data or a onerror or oncompleted notice, Amb will ignore and discard all other observables emissions.

The Rxjava implementation is the AMB operator when there is a similar object method Ambwith. For example, Observable.amb (O1,O2) and O1.ambwith (O2) are equivalent.

The operator operator is not executed on any particular scheduler by default. API

Javadoc:amb (t O1, t ... O2) (acceptable 2 to 9 parameters)
Sample Code
Observable.amb (Observable.just (New Student (1001, "Amb-1"). Delay (2, timeunit.seconds),
                                observable.just (new Student (1002, "amb-2"))
        . Subscribeon (Schedulers.io ())
        . Observeon (Androidschedulers.mainthread ())
        . Subscribe (New subscriber<student> () {
            @Override public
            void oncompleted () {

            }

            @Override Public
            void OnError (Throwable e) {

            }

            @Override public
            void OnNext (Student Student) {
                Madastudent.adddata (student);
                LOG.I (TAG, student.tostring ());
            }
        );
Log Print
Student{id= ' 1002 ' name= ' amb-2 ', age=21}
Sample Resolution

From the example code, passed to the AMB operator two observable, but the first observable through the delay operator, delayed it 2S launch data, while the second did not delay processing. Therefore, the AMB operator first receives the data that the second observable launches. From the Amb overview, the AMB operator discards the data from the first observable and launches only the second data. From log printing, you can see that only the data corresponding to the second observable is printed, meaning that the observer receives only the data from the second observable, which confirms the role of the AMB operator. contains Flow Chart

Overview

The contains operator will receive a specific value as a parameter to determine whether the original observable fired the value, and if fired, the created and returned observable will launch true or FALSE.

The contains operator is not executed on any particular scheduler by default. API

Javadoc:contains (Object)
Sample Code
Observable.from (Getlistofstudent ()). Subscribeon (Schedulers.io ()). Contains (new Student (2, "B", 33))
            . Observeon (Androidschedulers.mainthread ()). Subscribe (New action1<boolean> () {@Override
            public void Call (Boolean Aboolean) {log.i (TAG, "contains:" + Aboolean);

}
        });
    public class Student extends Id {private String name;

    private int age;
        @Override public boolean equals (Object o) {if (this = O) return true;

        if (o = = NULL | | getclass ()!= O.getclass ()) return false;

        Student Student = (Student) o;

    return age = = Student.age;
    } * * * private list<student> getlistofstudent () {list<student> List = new arraylist<> ();
    List.add (New Student (1, "A", 23));
    List.add (New Student (2, "B", 33));
    List.add (New Student (3, "C", 24));
    List.add (New Student (4, "D", 24)); List.add (New Student (5, "E", 36));
    List.add (New Student (6, "F", 23));
return list;
 }
Log Print
Contains:true
Sample Resolution

First look at the student class, which overrides the Equals method and sets the age attribute as the criterion for determining whether two student objects are the same. Next look at the contains operator, A student object was received with the Age property value of 33. In the observable data, the Age property value of the second data is 33, consistent with the Coantains operator's parameter object's age property value, so the log is printed as contains: True In the case of observable data, the age attribute value of no data is 33, and log should be printed as contains:false. exists Flow Chart

Overview

The EXISTS operator is similar to the contains operator, in that it accepts a function parameter and, in a function, sets the comparison condition and makes a judgment on the data emitted by the original observable. If any one satisfies the condition, it creates and returns a observable that emits true, or returns a observable that emits false.

The operator is not executed on any particular scheduler by default. API

Javadoc:exists (FUNC1))
Sample Code
Observable.from (Getlistofstudent ())
        . Subscribeon (Schedulers.io ())
        . Exists (new func1<student, Boolean > () {
            @Override public
            Boolean Call (Student Student) {return
                student.getage () = ()
            )
        . Observeon (Androidschedulers.mainthread ())
        . Subscribe (New action1<boolean> () {
            @Override
            public void Call (Boolean aboolean) {
                log.i (TAG, "exists:" + Aboolean);
            }
        );
Log Print
Exists:true
IsEmpty Flow Chart

Overview

The IsEmpty operator is used to determine whether the original observable did not emit any data. Packages observable does not emit any data, creates and returns a observable that emits true, or returns a observable that emits false.

The isempty operator is not executed on any particular scheduler by default. API

Javadoc:isempty ()
Sample Code
Observable.from (New arraylist<student> ())
        . Subscribeon (Schedulers.io ())
        . IsEmpty ().
        Observeon (Androidschedulers.mainthread ())
        . Subscribe (new action1<boolean> () {
            @Override public
            void Call (Boolean aboolean) {
                log.i (TAG, " IsEmpty: "+ Aboolean);
            }"
        ;
Log Print
Isempty:true
Sample Resolution

In the example code, a observable is created by Observable.from () whose data source is a ArrayList but it is empty, meaning observable does not emit any data. At this point, calling the IsEmpty operator, you should create and return a observable that launches true, and observe that the subscription should receive this observable. The exact result, see log Print. DefaultIfEmpty Flow Chart

Overview

The DefaultIfEmpty operation accepts an alternate data that does not emit any data normally terminated (in oncompletedd form) in the original observable, which creates a observable with the standby data and launches the data.

Rxjava this operator as DefaultIfEmpty. It is not executed on any specific scheduler by default. API

Javadoc:defaultifempty (T)
Sample Code
Observable.from (New arraylist<student> ())
        . Subscribeon (Schedulers.io ())
        . DefaultIfEmpty (New Student (1001, "Default-1"))
        . Observeon (Androidschedulers.mainthread ())
        . Subscribe (New action1< Student> () {
            @Override public
            void Call (Student Student) {
                madastudent.adddata (Student);
                LOG.I (TAG, student.tostring ());
            }
        );
Log Print
Student{id= ' 1001 ' name= ' default-1 ', age=20}
sequenceequal Flow Chart

Overview

The sequenceequal operator accepts two observable parameters, and it compares the data emitted by two observable, if the sequence of the launch data is the same (same data, same order, same termination state), Creates and returns a observable that emits true, or returns a observable that emits false.


The SequenceEqual (OBSERVABLE,OBSERVABLE,FUNC2) variant receives two observable parameters and one function argument, in which the two parameters are the same.

The operator is not executed on any particular scheduler by default. API

Javadoc:sequenceequal (observable,observable)
javadoc:sequenceequal (OBSERVABLE,OBSERVABLE,FUNC2)
Sample Code
Observable.sequenceequal (Observable.from (Getlistofstudent ()), 
                         Observable.from (Getlistofstudent ()). Delay (1, timeunit.seconds))
            . Subscribeon (Schedulers.io ()).
            Observeon (Androidschedulers.mainthread ())
            . Subscribe (new action1<boolean> () {
                @Override public
                void Call (Boolean aboolean) {
                    log.i (TAG, " SequenceEqual: "+ Aboolean);
                }"
            ;
Log Print
Sequenceequal:true
Skipuntil Flow Chart

Overview

Skipuntil operator when the Observer subscribes to the original observable, the operator will ignore the original observable's launch data until the second observable launches a data that begins to launch the original observable.

The operator is not executed on any particular scheduler by default. API

Javadoc:skipuntil (observable))
Sample Code
Observable.interval (1, timeunit.seconds)
    . Subscribeon (Schedulers.io ())
    . Skipuntil (Observable.just (1). Delay (3, timeunit.seconds))
    . Flatmap (New Func1<long, observable<student>> () {
        @Override
        Public observable<student> called (Long along) {return
            observable.just (new Student (Along.intvalue ()), Skipuntil-"+ along.intvalue (), Along.intvalue () +);
        }
    ). Observeon (Androidschedulers.mainthread ())
    . Subscribe (New subscriber<student> () {
        @Override
        public void oncompleted () {

        }

        @Override public
        void OnError (Throwable e) {

        }

        @Override
        public void OnNext (Student Student) {
            log.i (TAG, student.tostring ());
            Madastudent.adddata (student);
            if (Student.getid () >  5) {
                unsubscribe ();
            }}}
    );
Log Print
Student{id= ' 2 ' name= ' skipWhile-2 ', age=22} student{id= ' 3 ' name= ' skipWhile-3 ' 
, age=23} 
student{id= ' 4 ' name= ' SkipWhile-4 ', age=24} 
student{id= ' 5 ' name= ' skipWhile-5 ', age=25} 
student{id= ' 6 ' name= ' skipWhile-6 ', age=26} 
SkipWhile Flow Chart

Overview

The skipwhile operator discards the data emitted by the original observable until the transmitted data does not meet a specified condition, starting the launch of the original observable.

When the Observer subscribes to the original observable, the skipwhile operator ignores the emitter of the original observable, and it starts launching the original observable launch data until a certain condition you specify becomes false.

The skipwhile operator is not executed on any particular scheduler by default. API

Javadoc:skipwhile (FUNC1)
Sample Code
Observable.interval (1, timeunit.seconds). Subscribeon (Schedulers.io ()). SkipWhile (New Func1<long, Boole
            An> () {@Override public Boolean call (Long along) {return along < 2;
            }). Flatmap (New Func1<long, observable<student>> () {@Override  Public observable<student> Call (Long along) {return observable.just (new Student (Along.intvalue),
            "SkipWhile-" + along.intvalue (), Along.intvalue () + 20)); }). Observeon (Androidschedulers.mainthread ()). Subscribe (New subscriber<student> () {@O Verride public void oncompleted () {} @Override the public void OnError (Thro Wable e) {} @Override public void OnNext (Student Student) {log.i (T
                AG, student.tostring ());
   Madastudent.adddata (student);             if (Student.getid () > 5) {unsubscribe ();
 }
            }
        });
Log Print
Student{id= ' 2 ' name= ' skipWhile-2 ', age=22} student{id= ' 3 ' name= ' skipWhile-3 ' 
, age=23} 
student{id= ' 4 ' name= ' SkipWhile-4 ', age=24} 
student{id= ' 5 ' name= ' skipWhile-5 ', age=25} 
student{id= ' 6 ' name= ' skipWhile-6 ', age=26} 
Takeuntil Flow Chart

Overview

The takeuntil operator, in contrast to the skipuntil operator, discards any data emitted by the original observable when the second observable launches a data or terminates.

There are many variations of the takeuntil operator in Rxjava to meet the needs of various situations. The following one by one describes:

The Takeuntil (observable) variant accepts a observable parameter, which is temporarily named _observable, and the observable operator monitors TAKEUNTIL when the observer subscribes to the original _observable. If the observable launches a data or emits a termination notification (ONERROR notification or ncompleted notification), the observable that the Takeuntil operator creates and returns stops the launch of the original observable data and terminates.

The Takeuntil (FUNC1) Variant accepts a function argument that is not in the Rxjava 1.0.0 version, and its behavior class begins in TakeWhile (FUNC1).

The takeuntil operator is not executed on any particular scheduler by default. API

Javadoc:takeuntil (observable)

Javadoc:takeuntil (FUNC1)
Sample Code
Observable.interval (1, timeunit.seconds)
    . Subscribeon (Schedulers.io ())
    . Takeuntil (Observable.just (1). Delay (3, timeunit.seconds))
    . Flatmap (New Func1<long, observable<student>> () {
        @Override
        Public observable<student> called (Long along) {return
            observable.just (new Student (Along.intvalue ()), Takeuntil-"+ along.intvalue (), Along.intvalue () +);
        }
    ). Observeon (Androidschedulers.mainthread ())
    . Subscribe (New subscriber<student> () {
        @Override
        public void oncompleted () {

        }

        @Override public
        void OnError (Throwable e) {

        }

        @Override
        public void OnNext (Student Student) {
            log.i (TAG, student.tostring ());
            Madastudent.adddata (student);
        }
    );
Log Print
Student{id= ' 0 ' name= ' takeUntil-0 ', age=20}
student{id= ' 1 ' name= ' takeUntil-1 ', age=21}
TakeWhile Flow Chart

Overview

The takewhile operator works contrary to the skipwhile operator. When the Observer subscribes to the original observable, TakeWhile creates and returns the mirror observable of the original oservable, tentatively named _observable, which launches the original observable data. When a condition you specify becomes false, _observable launches oncompleted termination notification.

The takewhile operator is not executed on any particular scheduler by default. API

Javadoc:takewhile (FUNC1)
Sample Code
Observable.interval (1, timeunit.seconds). Subscribeon (Schedulers.io ()). TakeWhile (New Func1<long, Boole
            An> () {@Override public Boolean call (Long along) {return along < 3;
            }). Flatmap (New Func1<long, observable<student>> () {@Override  Public observable<student> Call (Long along) {return observable.just (new Student (Along.intvalue),
            "TakeWhile-" + along.intvalue (), Along.intvalue () + 20)); }). Observeon (Androidschedulers.mainthread ()). Subscribe (New subscriber<student> () {@O Verride public void oncompleted () {} @Override the public void OnError (Thro Wable e) {} @Override public void OnNext (Student Student) {log.i (T
                AG, student.tostring ());
   Madastudent.adddata (student);         }
        });
 
Log Print
Student{id= ' 0 ' name= ' takeWhile-0 ', age=20} student{id= ' 1 ' name= ' takeWhile-1 ' 
, age=21} 
student{id= ' 2 ' name= ' TakeWhile-2 ', age=22} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.