0

How to write this method in functional programming by using Java 8 lambda / stream() in a short more intuitive way?

int animation = R.style.DialogAnimationSlideHorizontal;
String body;
String subject = mSubjectView.getText().toString();
BaseDialog dialog = dialogFactory.getType(DialogTypes.DIALOG_FULL);

if (!checkFields()) {
    // one of the recipients is invalid.
    body = getString(R.string.bad_address);
    dialog.showDialog(body, animation, new DialogBuilder.Positive() {
        @Override
        public void handleClick(DialogInterface dialogInterface, View view) {
            // do nothing
        }
    });
} else if (Helpers.isEmpty(subject)) {
        // Yup, empty... send the message without a subject?
        body = getString(R.string.empty_subject_compose);
        dialog.showDialog(body, animation, new DialogBuilder.Positive() {
            @Override
            public void handleClick(DialogInterface dialogInterface, View view) {
                // user accepted to send anyway.
                mWebView.getComposeContent();
            }
        });
} else {
    // everything is correct! send the message.
    mWebView.getComposeContent();
}
1
  • Try with a broader overhaul that includes methods like checkFields() (if that code checks a set of fields it's a good candidate for streams). Functional works best when paired with an overall functional style architecture. Commented May 25, 2016 at 13:24

1 Answer 1

5

Replace all anonymous classes with lambdas like

new DialogBuilder.Positive() {
    @Override
    public void handleClick(DialogInterface dialogInterface, View view) {
       ...
    }
}

               |
               V

(dialogInterface, view) -> { /*do nothing*/ }
(dialogInterface, view) -> { mWebView.getComposeContent(); }

And I don't see how Stream API can be applied here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.