Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional<Stream>.filter(Optional::isPresent).map(Optional::get) to Java 11 .flatMap(Optional::stream) #315

Merged
merged 14 commits into from
Oct 31, 2023

Conversation

aslaski2
Copy link
Contributor

@aslaski2 aslaski2 commented Oct 6, 2023

Fixes #313

Problem

Java 8 Optional class didn't provide ::stream method. As a result of this unfortunate mistake a popular idiom was created to flatten a Stream of Optionals: .filter(Optional::isPresent).map(Optional::get).
This was corrected in Java 9 and the idiom may be replaced with .flatMap(Optional::stream)

Anything in particular you'd like reviewers to focus on?

This is my first adventure with OpenRewrite, please check everything carefully.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ IDEA auto-formatter on affected files except for the flatMap chain

@timtebeek timtebeek added the recipe Recipe requested label Oct 11, 2023
@aslaski2
Copy link
Contributor Author

Hi @timtebeek! What happens now? Will someone come and review?

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressed with the work you've done here! Really like the attention to detail in handling edge cases, and the suite of tests to show what's correctly handled. I've added some comments that I think would improve this PR still, and bring it closer to a merge.

Let me know if you'd be open to making those changes still!

@timtebeek timtebeek changed the title #313 recipe for optional stream Optional<Stream>.filter(Optional::isPresent).map(Optional::get) to Java 11 .flatMap(Optional::stream) Oct 19, 2023
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this addition @aslaski2 ! Let us know how this works for you; you can try it out through app.moderne.io soon after the merge.

@timtebeek timtebeek merged commit 7efed71 into openrewrite:main Oct 31, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Optional<Stream>.filter(Optional::isPresent).map(Optional::get) to Java 11 .flatMap(Optional::stream)
3 participants