Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
About the Author
About the Reviewers

Composing multiple operators with the Observable.compose operator

The compose() operator has one parameter of type Transformer. The Transformer interface, like the Operator one, is an empty interface that extends Func1 (this approach hides the type complexities that are involved by using Func1). The difference is that it extends the Func1<Observable<T>, Observable<R>> method, so that it transforms an Observable and not a Subscriber. This means that, instead of operating on each individual item emitted by the source observable, it operates directly on the source.

We can illustrate the use of this operator and the Transformer interface through an example. First, we will create a Transformer implementation:

public class OddFilter<T> implements Transformer<T, T> {
  public Observable<T> call(Observable<T> observable) {
    return observable
      .lift(new Indexed<T>(1L))
      .filter(pair -> pair.getLeft() % 2 == 1)