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 932e5e9050..d01820135b 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 @@ -24,6 +24,7 @@ import com.google.errorprone.refaster.annotation.Repeated; import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Optional; @@ -247,14 +248,46 @@ int after(T value1, T value2) { * Avoid unnecessary creation of a {@link Stream} to determine the minimum of a known collection * of values. */ - static final class MinOfVarargs { + static final class MinOfArray { @BeforeTemplate - T before(@Repeated T value, Comparator cmp) { + T before(T[] array, Comparator cmp) { + return Arrays.stream(array).min(cmp).orElseThrow(); + } + + @AfterTemplate + T after(T[] array, Comparator cmp) { + return Collections.min(Arrays.asList(array), cmp); + } + } + + /** + * Avoid unnecessary creation of a {@link Stream} to determine the minimum of a known collection + * of values. + */ + static final class MinOfCollection { + @BeforeTemplate + T before(Collection collection, Comparator cmp) { + return collection.stream().min(cmp).orElseThrow(); + } + + @AfterTemplate + T after(Collection collection, Comparator cmp) { + return Collections.min(collection, cmp); + } + } + + /** + * Avoid unnecessary creation of a {@link Stream} to determine the minimum of a known collection + * of values. + */ + static final class MinOfVarargs { + @BeforeTemplate + T before(@Repeated T value, Comparator cmp) { return Stream.of(Refaster.asVarargs(value)).min(cmp).orElseThrow(); } @AfterTemplate - T after(@Repeated T value, Comparator cmp) { + T after(@Repeated T value, Comparator cmp) { return Collections.min(Arrays.asList(value), cmp); } } @@ -314,14 +347,46 @@ T after(T value1, T value2, Comparator cmp) { * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection * of values. */ - static final class MaxOfVarargs { + static final class MaxOfArray { + @BeforeTemplate + T before(T[] array, Comparator cmp) { + return Arrays.stream(array).max(cmp).orElseThrow(); + } + + @AfterTemplate + T after(T[] array, Comparator cmp) { + return Collections.max(Arrays.asList(array), cmp); + } + } + + /** + * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection + * of values. + */ + static final class MaxOfCollection { + @BeforeTemplate + T before(Collection collection, Comparator cmp) { + return collection.stream().max(cmp).orElseThrow(); + } + + @AfterTemplate + T after(Collection collection, Comparator cmp) { + return Collections.max(collection, cmp); + } + } + + /** + * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection + * of values. + */ + static final class MaxOfVarargs { @BeforeTemplate - T before(@Repeated T value, Comparator cmp) { + T before(@Repeated T value, Comparator cmp) { return Stream.of(Refaster.asVarargs(value)).max(cmp).orElseThrow(); } @AfterTemplate - T after(@Repeated T value, Comparator cmp) { + T after(@Repeated T value, Comparator cmp) { return Collections.max(Arrays.asList(value), cmp); } } 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 0fca14f8d4..0f0a37ae4a 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,6 +107,14 @@ ImmutableSet testCompareTo() { Comparator.reverseOrder().compare("baz", "qux")); } + String testMinOfArray() { + return Arrays.stream(new String[0]).min(naturalOrder()).orElseThrow(); + } + + String testMinOfCollection() { + return ImmutableSet.of("foo", "bar").stream().min(naturalOrder()).orElseThrow(); + } + int testMinOfVarargs() { return Stream.of(1, 2).min(naturalOrder()).orElseThrow(); } @@ -135,6 +143,14 @@ ImmutableSet testMinOfPairCustomOrder() { Collections.min(ImmutableSet.of("a", "b"), (a, b) -> 1)); } + String testMaxOfArray() { + return Arrays.stream(new String[0]).max(naturalOrder()).orElseThrow(); + } + + String testMaxOfCollection() { + return ImmutableSet.of("foo", "bar").stream().max(naturalOrder()).orElseThrow(); + } + int testMaxOfVarargs() { return Stream.of(1, 2).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 9c34e00831..850fc62d63 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,6 +98,14 @@ ImmutableSet testCompareTo() { return ImmutableSet.of("foo".compareTo("bar"), "qux".compareTo("baz")); } + String testMinOfArray() { + return Collections.min(Arrays.asList(new String[0]), naturalOrder()); + } + + String testMinOfCollection() { + return Collections.min(ImmutableSet.of("foo", "bar"), naturalOrder()); + } + int testMinOfVarargs() { return Collections.min(Arrays.asList(1, 2), naturalOrder()); } @@ -126,6 +134,14 @@ ImmutableSet testMinOfPairCustomOrder() { Comparators.min("a", "b", (a, b) -> 1)); } + String testMaxOfArray() { + return Collections.max(Arrays.asList(new String[0]), naturalOrder()); + } + + String testMaxOfCollection() { + return Collections.max(ImmutableSet.of("foo", "bar"), naturalOrder()); + } + int testMaxOfVarargs() { return Collections.max(Arrays.asList(1, 2), naturalOrder()); }