O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making conditions explicit

To see how effective the method extraction technique can be, we will first try out a simple example on .filter() in the Twitter stream filtering code:

observeTwitterStream(configuration, filterQuery)        .sample(2700, TimeUnit.MILLISECONDS)        .map(StockUpdate::create)        .filter(stockUpdate -> {            for(String keyword : trackingKeywords) {                if (stockUpdate.getTwitterStatus()                .contains(keyword)) {                    return true;                }            }        return false;        })

The .filter() block here is responsible for the removal of tweets that do not contain the keywords. Let's make that explicit by extracting a method:

containsAnyOfKeywords()

So, the block before will become this:

observeTwitterStream(configuration, filterQuery)        .sample(2700, TimeUnit.MILLISECONDS ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required