-
Notifications
You must be signed in to change notification settings - Fork 37
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 Comparators{Min,Max}
Refaster templates
#270
Conversation
Nice! Thanks for opening a PR 🚀 ! This case is a tricky one, make sure to add the |
Thanks @rickie for explanation! |
b367315
to
d3b766c
Compare
There was a problem hiding this 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. Thanks for the PR!
Suggested commit message:
Introduce `Comparators{Min,Max}` Refaster templates (#270)
*/ | ||
static final class BinaryOperatorMinByNaturalOrder<T extends Comparable<? super T>> { | ||
@BeforeTemplate | ||
BiFunction<T, T, T> before() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be more precise:
BiFunction<T, T, T> before() { | |
BinaryOperator<T> before() { |
|
||
/** | ||
* Prefer {@link Comparators#min(Comparable, Comparable)} over {@link | ||
* BinaryOperator#minBy(Comparator)}for natural ordering. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* BinaryOperator#minBy(Comparator)}for natural ordering. | |
* BinaryOperator#minBy(Comparator)} for natural ordering. |
But perhaps we can make the text a bit more specific; will propose something.
* Prefer {@link Comparators#min(Comparable, Comparable)} over {@link | ||
* BinaryOperator#minBy(Comparator)}for natural ordering. | ||
*/ | ||
static final class BinaryOperatorMinByNaturalOrder<T extends Comparable<? super T>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally name templates after the @AfterTemplate
code. This is a bit of a special case, but the current (not yet officially implemented) algorithm would yield ComparatorsMin
.
(The (old) templates further up don't follow this naming rule, and once we change that there'll be a nice clash. But we'll fix that then. I suspect eventually we'll end up with ComparatorsMinAsBinaryOperator
, by suffixing the target type for method references.)
import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; | ||
|
||
final class ComparatorTemplatesTest implements RefasterTemplateTestCase { | ||
@Override | ||
public ImmutableSet<?> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of( | ||
Arrays.class, Collections.class, ImmutableList.class, ImmutableSet.class, identity()); | ||
Arrays.class, | ||
BinaryOperator.class, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use BinaryOperator
in the return types, then we don't need this :)
@@ -10,13 +10,20 @@ | |||
import java.util.Arrays; | |||
import java.util.Collections; | |||
import java.util.Comparator; | |||
import java.util.function.BiFunction; | |||
import java.util.function.BinaryOperator; | |||
import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; | |||
|
|||
final class ComparatorTemplatesTest implements RefasterTemplateTestCase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice that half of the templates are about Comparators
rather than Comparator
. Maybe something to split one day. 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes LGTM, the suggested commit message as well.
Thanks for opening a PR @nadirbelarouci ! 🚀 🚀
Comparators{Min,Max}
Refaster templates
Added 2 rules for
BinaryOperator#minBy
andBinaryOperator#maxBy
when they're called with anaturalOrder
comparator