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

Introduce Optional{Filter,Map} Refaster rules #327

Merged
merged 2 commits into from
Nov 4, 2022

Conversation

werli
Copy link
Member

@werli werli commented Nov 2, 2022

After I had found this construct during code review.

A mixture of Optional#map() and Optional#filter() is however not supported with these rules. At this point it seems that Refaster won't be enough, I suspect?

optional.stream().map(x -> x).filter(x -> true).find{Any,First}

Suggested commit message

Introduce `OptionalStream{Filter,Map}` Refaster rules (#327)

@werli
Copy link
Member Author

werli commented Nov 3, 2022

I'll try to circle back to this PR tomorrow to ensure that the build is running through. Local mvn clean install ran through. 👍

@rickie rickie requested review from rickie and Stephan202 November 3, 2022 09:08
@rickie
Copy link
Member

rickie commented Nov 3, 2022

Yeah it's the self-check that is failing, I think there is some overlapping check or rule that rewrites the code.
One can use this command to run the self-check:

mvn clean install -Dverification.warn -Perror-prone-fork -Pnon-maven-central -Pself-check -s settings.xml

@rickie rickie added this to the 0.6.0 milestone Nov 3, 2022
@Stephan202 Stephan202 force-pushed the werli/optional-stream-filter branch from d4c6743 to 8f1b0e3 Compare November 4, 2022 05:53
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Rebased and added a commit with a proposal. Nice one!

Suggested commit message:

Introduce `Optional{Filter,Map}` Refaster rules (#327)

@BeforeTemplate
Optional<? extends T> before(Optional<S> optional, Function<? super S, ? extends T> function) {
return Refaster.anyOf(
optional.stream().map(function).findAny(), optional.stream().map(function).findFirst());
Copy link
Member

Choose a reason for hiding this comment

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

The second case is covered by StreamMapFirst and then OptionalIdentity, which together also yield optional.map(function).

The findAny() is not simplified because StreamMapFirst doesn't want to assume that the Stream against which it matches has a defined order. What we should perhaps do, is implement a heuristics-based Matcher for that case.

@werli
Copy link
Member Author

werli commented Nov 4, 2022

Thanks for the suggestions, @Stephan202. After re-thinking parts of it, I agree with your proposed changes. Let's roll with it. 👍

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Nice @werli, another contribution!

Looks good 😄.

Suggested commit message LGTM.

* Avoid unnecessary {@link Optional} to {@link Stream} conversion when mapping a value of the
* former type.
*/
// XXX: If `StreamMapFirst` also simplifies `.findAny()` expressions, then this rule can be
Copy link
Member

Choose a reason for hiding this comment

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

IIUC in that case the self-check would identify this case right?

In that case I'd argue that it would be better to drop this XXX 😬.
@Stephan202

Copy link
Member

Choose a reason for hiding this comment

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

I agree that we could drop it for that reason, but without it on might ask "why not add .findFirst()?" IMHO now it's clearer why this rule exists in the first place 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Okay okay. Agreed, it doesn't really fit in a @Description or something like that, so XXX is most reasonable.

@rickie rickie force-pushed the werli/optional-stream-filter branch from 8f1b0e3 to d88bfec Compare November 4, 2022 18:30
@rickie
Copy link
Member

rickie commented Nov 4, 2022

Rebased, will merge once 🟢.

@rickie rickie changed the title Introduce OptionalStream{Filter,Map} Refaster rules Introduce Optional{Filter,Map} Refaster rules Nov 4, 2022
@rickie rickie merged commit 48772a0 into master Nov 4, 2022
@rickie rickie deleted the werli/optional-stream-filter branch November 4, 2022 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants