diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java index a37ef407c25..c6d537c0c35 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java @@ -7,6 +7,8 @@ import static java.util.Comparator.comparingLong; import static java.util.Comparator.naturalOrder; import static java.util.Comparator.reverseOrder; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.minBy; import com.google.common.collect.Comparators; import com.google.common.collect.ImmutableList; @@ -21,11 +23,13 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.Optional; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.ToDoubleFunction; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; +import java.util.stream.Collector; import java.util.stream.Stream; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; import tech.picnic.errorprone.refaster.matchers.IsIdentityOperation; @@ -383,4 +387,36 @@ BinaryOperator after() { return Comparators::max; } } + + /** + * Prefer {@link Comparator#naturalOrder()} over {@link Comparator#reverseOrder()} where possible. + */ + static final class MinByNaturalOrder> { + @BeforeTemplate + Collector> before() { + return maxBy(reverseOrder()); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + Collector> after() { + return minBy(naturalOrder()); + } + } + + /** + * Prefer {@link Comparator#naturalOrder()} over {@link Comparator#reverseOrder()} where possible. + */ + static final class MaxByNaturalOrder> { + @BeforeTemplate + Collector> before() { + return minBy(reverseOrder()); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + Collector> after() { + return maxBy(naturalOrder()); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java index b20174934f0..2d92ba5def3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java @@ -3,6 +3,8 @@ import static java.util.Comparator.naturalOrder; import static java.util.Comparator.reverseOrder; import static java.util.function.Function.identity; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.minBy; import com.google.common.collect.Comparators; import com.google.common.collect.ImmutableList; @@ -10,7 +12,9 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.Optional; import java.util.function.BinaryOperator; +import java.util.stream.Collector; import java.util.stream.Stream; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; @@ -161,4 +165,12 @@ BinaryOperator testComparatorsMin() { BinaryOperator testComparatorsMax() { return BinaryOperator.maxBy(naturalOrder()); } + + Collector> testMinByNaturalOrder() { + return maxBy(reverseOrder()); + } + + Collector> testMaxByNaturalOrder() { + return minBy(reverseOrder()); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java index 088b2f01a91..ff2058552b3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java @@ -3,6 +3,8 @@ import static java.util.Comparator.naturalOrder; import static java.util.Comparator.reverseOrder; import static java.util.function.Function.identity; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.minBy; import com.google.common.collect.Comparators; import com.google.common.collect.ImmutableList; @@ -10,7 +12,9 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.Optional; import java.util.function.BinaryOperator; +import java.util.stream.Collector; import java.util.stream.Stream; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; @@ -151,4 +155,12 @@ BinaryOperator testComparatorsMin() { BinaryOperator testComparatorsMax() { return Comparators::max; } + + Collector> testMinByNaturalOrder() { + return minBy(naturalOrder()); + } + + Collector> testMaxByNaturalOrder() { + return maxBy(naturalOrder()); + } }