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 Comparators{Min,Max} Refaster templates #270

Merged
merged 3 commits into from
Sep 29, 2022

Conversation

nadirbelarouci
Copy link
Contributor

Added 2 rules for BinaryOperator#minBy and BinaryOperator#maxBy when they're called with a naturalOrder comparator

@rickie
Copy link
Member

rickie commented Sep 29, 2022

Nice! Thanks for opening a PR 🚀 !

This case is a tricky one, make sure to add the BinaryOperator to the elidedTypesAndStaticImports method. The problem is that the BinaryOperator is there in the TestInput file but not in the TestOutput file. However, Refaster does not clean up these imports for us. While the mvn fmt:format does remove it in the TestOutput file. To prevent mvn fmt:format from deleting it in the TestOutput file, we add it to the elidedTypesAndStaticImports method.

@nadirbelarouci
Copy link
Contributor Author

Thanks @rickie for explanation!

@Stephan202 Stephan202 added this to the 0.4.0 milestone Sep 29, 2022
@Stephan202 Stephan202 force-pushed the nadirbelarouci/binary-operator-min-by branch from b367315 to d3b766c Compare September 29, 2022 18:00
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. 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() {
Copy link
Member

Choose a reason for hiding this comment

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

To be more precise:

Suggested change
BiFunction<T, T, T> before() {
BinaryOperator<T> before() {


/**
* Prefer {@link Comparators#min(Comparable, Comparable)} over {@link
* BinaryOperator#minBy(Comparator)}for natural ordering.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* 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>> {
Copy link
Member

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,
Copy link
Member

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 {
Copy link
Member

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. 🤷

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.

Changes LGTM, the suggested commit message as well.

Thanks for opening a PR @nadirbelarouci ! 🚀 🚀

@rickie rickie changed the title Add Refaster rule for BinaryOperator's minBy and maxBy methods Introduce Comparators{Min,Max} Refaster templates Sep 29, 2022
@rickie rickie merged commit aa5ad4d into master Sep 29, 2022
@rickie rickie deleted the nadirbelarouci/binary-operator-min-by branch September 29, 2022 18:35
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.

None yet

3 participants