aleien
aleien

Reputation: 821

Simplifying multiple 'if's with rxjava

Which RxJava operations should I use to rewrite this mess to more simple version?

.map(content -> {
                    if (content.getPost() != null) {
                        return content.getPost();
                    } else if (content.getAction() != null) {
                        return content.getAction().getPost();
                    } else {
                        return null;
                    }
                })

Something like

.map(Content::getPost)
.map(Content::getAction::getPost)
.combine( .. )

Is it possible?

Upvotes: 1

Views: 85

Answers (1)

JohnWowUs
JohnWowUs

Reputation: 3083

Pretty much what akarnokd said except you can perhaps filter out the null post emissions using

 .filter(content -> (content.getPost() != null) || ((content.getAction() != null) && (content.getAction().getPost())))
 .map(content -> (content.getPost() != null ? content.getPost() : content.getAction().getPost()))

Upvotes: 1

Related Questions