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 d01820135b..f846de8a1c 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 @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.List; import java.util.Optional; import java.util.function.BinaryOperator; import java.util.function.Function; @@ -244,6 +245,33 @@ int after(T value1, T value2) { } } + /** Prefer {@link Collections#sort(List)} over more verbose alternatives. */ + static final class CollectionsSort> { + @BeforeTemplate + void before(List collection) { + Collections.sort(collection, naturalOrder()); + } + + @AfterTemplate + void after(List collection) { + Collections.sort(collection); + } + } + + /** Prefer {@link Collections#min(Collection)} over more verbose alternatives. */ + static final class CollectionsMin> { + @BeforeTemplate + T before(Collection collection) { + return Refaster.anyOf( + Collections.min(collection, naturalOrder()), Collections.max(collection, reverseOrder())); + } + + @AfterTemplate + T after(Collection collection) { + return Collections.min(collection); + } + } + /** * Avoid unnecessary creation of a {@link Stream} to determine the minimum of a known collection * of values. @@ -264,7 +292,7 @@ T after(T[] array, Comparator cmp) { * Avoid unnecessary creation of a {@link Stream} to determine the minimum of a known collection * of values. */ - static final class MinOfCollection { + static final class CollectionsMinWithComparator { @BeforeTemplate T before(Collection collection, Comparator cmp) { return collection.stream().min(cmp).orElseThrow(); @@ -343,6 +371,20 @@ T after(T value1, T value2, Comparator cmp) { } } + /** Prefer {@link Collections#max(Collection)} over more verbose alternatives. */ + static final class CollectionsMax> { + @BeforeTemplate + T before(Collection collection) { + return Refaster.anyOf( + Collections.max(collection, naturalOrder()), Collections.min(collection, reverseOrder())); + } + + @AfterTemplate + T after(Collection collection) { + return Collections.max(collection); + } + } + /** * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection * of values. @@ -363,7 +405,7 @@ T after(T[] array, Comparator cmp) { * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection * of values. */ - static final class MaxOfCollection { + static final class CollectionsMaxWithComparator { @BeforeTemplate T before(Collection collection, Comparator cmp) { return collection.stream().max(cmp).orElseThrow(); 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 0f0a37ae4a..26330d2835 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 @@ -107,11 +107,21 @@ ImmutableSet testCompareTo() { Comparator.reverseOrder().compare("baz", "qux")); } + void testCollectionsSort() { + Collections.sort(ImmutableList.of("foo", "bar"), naturalOrder()); + } + + ImmutableSet testCollectionsMin() { + return ImmutableSet.of( + Collections.min(ImmutableList.of("foo"), naturalOrder()), + Collections.max(ImmutableList.of("bar"), reverseOrder())); + } + String testMinOfArray() { return Arrays.stream(new String[0]).min(naturalOrder()).orElseThrow(); } - String testMinOfCollection() { + String testCollectionsMinWithComparator() { return ImmutableSet.of("foo", "bar").stream().min(naturalOrder()).orElseThrow(); } @@ -143,11 +153,17 @@ ImmutableSet testMinOfPairCustomOrder() { Collections.min(ImmutableSet.of("a", "b"), (a, b) -> 1)); } + ImmutableSet testCollectionsMax() { + return ImmutableSet.of( + Collections.max(ImmutableList.of("foo"), naturalOrder()), + Collections.min(ImmutableList.of("bar"), reverseOrder())); + } + String testMaxOfArray() { return Arrays.stream(new String[0]).max(naturalOrder()).orElseThrow(); } - String testMaxOfCollection() { + String testCollectionsMaxWithComparator() { return ImmutableSet.of("foo", "bar").stream().max(naturalOrder()).orElseThrow(); } 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 850fc62d63..885e13fcd6 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 @@ -98,11 +98,20 @@ ImmutableSet testCompareTo() { return ImmutableSet.of("foo".compareTo("bar"), "qux".compareTo("baz")); } + void testCollectionsSort() { + Collections.sort(ImmutableList.of("foo", "bar")); + } + + ImmutableSet testCollectionsMin() { + return ImmutableSet.of( + Collections.min(ImmutableList.of("foo")), Collections.min(ImmutableList.of("bar"))); + } + String testMinOfArray() { return Collections.min(Arrays.asList(new String[0]), naturalOrder()); } - String testMinOfCollection() { + String testCollectionsMinWithComparator() { return Collections.min(ImmutableSet.of("foo", "bar"), naturalOrder()); } @@ -134,11 +143,16 @@ ImmutableSet testMinOfPairCustomOrder() { Comparators.min("a", "b", (a, b) -> 1)); } + ImmutableSet testCollectionsMax() { + return ImmutableSet.of( + Collections.max(ImmutableList.of("foo")), Collections.max(ImmutableList.of("bar"))); + } + String testMaxOfArray() { return Collections.max(Arrays.asList(new String[0]), naturalOrder()); } - String testMaxOfCollection() { + String testCollectionsMaxWithComparator() { return Collections.max(ImmutableSet.of("foo", "bar"), naturalOrder()); }