Skip to content

chRyNaN/ConditionalRx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConditionalRx

If, else-if, else, and switch condition operators for RxJava.

These operators are like a combination of both the filter and doOnNext operators. They filter on a condition then perform a function for those objects that passed the condition. All objects are emmitted whether they passed the filter condition or not.

ConditionalRx provides another approach to handling conditions in a stream like manner by providing a builder styled Operator API.

Example:

Consider the scenario of performing an action on all objects of a stream that meet a certain condition and performing another action on all the other objects that do not meet that condition. One approach might look something like this:

myObservable.doOnNext(new Action1<MyObject>() {
    @Override
    public void call(MyObject myObject) {
        if (myObject.isTrue()) {
            performFunction(myObject);
        } else {
            performOtherFunction(myObject);
        }
    }
}

It's usually desireable to use Lamda Expressions with RxJava to reduce the amount of code needed and to increase readability. However, when there are multiple conditions it's hard, if not impossible, to use Lambdas (at least single line Lambda expressions), so, the code begins to look convoluted. With ConditionalRx, this problem is addressed by providing stream like builder styled Operators that can be used with RxJava's lift method. The above code can be written as the following using ConditionalRx:

myObservable.lift(Conditional.ifThis(myObject -> myObject.isTrue())
                      .then(myObject::performFunction)
                      .otherwise(myObject::performOtherFunction));

ConditionalRx also provides the support for Switch Statements via the SwitchOperator. Here's an example of using the SwitchOperator:

myObservable.lift(Conditional.switchOn(myObject -> myObject.getInteger())
                      .caseWithBreak(5)
                      .then(myObject::performFunction)
                      .caseWithBreak(24)
                      .makeDefault()
                      .then(myObject::performOtherFunction));

Note:

This is a new experimental project and is currently not thoroughly tested.

TODO:

  • Clean-up code
  • Add more tests
  • Add JavaDoc

About

RxJava Conditional Operators

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages