Skip to content

Conversation

@mpritham
Copy link
Contributor

Before this PR

As part of this PR, the check to recommend using stream.min() over stream.sorted().findFirst() was removed. This PR adds an error-prone rule with the same effect.

After this PR

==COMMIT_MSG==
Add check replacing stream.sorted().findFirst() with stream.min()
==COMMIT_MSG==

@mpritham mpritham self-assigned this Apr 14, 2023
@changelog-app
Copy link

changelog-app bot commented Apr 14, 2023

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Add check replacing stream.sorted().findFirst() with stream.min()

Check the box to generate changelog(s)

  • Generate changelog entry

@mpritham mpritham requested a review from carterkozak April 14, 2023 17:14
return Description.NO_MATCH;
}

if (Matchers.receiverOfInvocation(STREAM_SORTED_NO_PARAMS_MATCHER).matches(tree, state)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's extract the Matchers.receiverOfInvocation( Matcher instances to static fields so they can be reused.

return describeMatch(
tree,
SuggestedFix.builder()
.replace(tree, state.getSourceForNode(stream) + ".min(Comparator.naturalOrder())")
Copy link
Contributor

Choose a reason for hiding this comment

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

it may be slightly cleaner to use an index-based replace call from the beginning of sorted to the end of findFirst. That way we're more likely to preserve comments and such. Doesn't make a great deal of differnce either way though :-]

state.getSourceForNode(stream) + ".min("
+ state.getSourceForNode(
sortedTree.getArguments().get(0)) + ")")
.addImport(Comparator.class.getCanonicalName())
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this addImport is used


private RefactoringValidator fix() {
return RefactoringValidator.of(SortedStreamFirstElement.class, getClass());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

great tests!

Copy link
Contributor

@carterkozak carterkozak left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @mpritham!

@bulldozer-bot bulldozer-bot bot merged commit 905734b into develop Jun 1, 2023
@bulldozer-bot bulldozer-bot bot deleted the add-check-sorted-first branch June 1, 2023 18:48
@svc-autorelease
Copy link
Collaborator

Released 5.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants