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 {Max,Min}Of{Array,Collection} Refaster rules #1276

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

"collection" here is not a copy-paste error: "collection" != Collection.

* of values.
*/
static final class MinOfVarargs<T> {
static final class MinOfArray<S, T extends S> {
@BeforeTemplate
T before(@Repeated T value, Comparator<T> cmp) {
T before(T[] array, Comparator<S> cmp) {
return Arrays.stream(array).min(cmp).orElseThrow();
}

@AfterTemplate
T after(T[] array, Comparator<S> 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<S, T extends S> {
@BeforeTemplate
T before(Collection<T> collection, Comparator<S> cmp) {
return collection.stream().min(cmp).orElseThrow();
}

@AfterTemplate
T after(Collection<T> collection, Comparator<S> 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<S, T extends S> {
@BeforeTemplate
T before(@Repeated T value, Comparator<S> cmp) {
return Stream.of(Refaster.asVarargs(value)).min(cmp).orElseThrow();
}

@AfterTemplate
T after(@Repeated T value, Comparator<T> cmp) {
T after(@Repeated T value, Comparator<S> cmp) {
return Collections.min(Arrays.asList(value), cmp);
}
}
Expand Down Expand Up @@ -314,14 +347,46 @@ T after(T value1, T value2, Comparator<? super T> cmp) {
* Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection
* of values.
*/
static final class MaxOfVarargs<T> {
static final class MaxOfArray<S, T extends S> {
@BeforeTemplate
T before(T[] array, Comparator<S> cmp) {
return Arrays.stream(array).max(cmp).orElseThrow();
}

@AfterTemplate
T after(T[] array, Comparator<S> 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<S, T extends S> {
Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah Aug 10, 2024

Choose a reason for hiding this comment

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

Looking at the unit test and usage of Comparator#naturalOrder, it made me wonder if we can re-write the case when the natural order is used as a comparator to the Collections#max(collection) method.

===================================================================
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
--- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java	(revision 49825759b2310bc0ea8fd72d8e4842f9c5460313)
+++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java	(date 1723313983688)
@@ -138,8 +138,12 @@
     return Collections.max(Arrays.asList(new String[0]), naturalOrder());
   }
 
-  String testMaxOfCollection() {
-    return Collections.max(ImmutableSet.of("foo", "bar"), naturalOrder());
+  String testMaxOfCollectionComparator() {
+    return Collections.max(ImmutableSet.of("foo", "bar"), reverseOrder());
+  }
+
+  String testMaxOfCollectionNaturalOrder() {
+    return Collections.max(ImmutableSet.of("foo", "bar"));
   }
 
   int testMaxOfVarargs() {
Index: error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>ISO-8859-1
===================================================================
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
--- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java	(revision 49825759b2310bc0ea8fd72d8e4842f9c5460313)
+++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestInput.java	(date 1723313983685)
@@ -147,7 +147,11 @@
     return Arrays.stream(new String[0]).max(naturalOrder()).orElseThrow();
   }
 
-  String testMaxOfCollection() {
+  String testMaxOfCollectionComparator() {
+    return ImmutableSet.of("foo", "bar").stream().max(reverseOrder()).orElseThrow();
+  }
+
+  String testMaxOfCollectionNaturalOrder() {
     return ImmutableSet.of("foo", "bar").stream().max(naturalOrder()).orElseThrow();
   }
 
Index: error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
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
--- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java	(revision 49825759b2310bc0ea8fd72d8e4842f9c5460313)
+++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java	(date 1723313988286)
@@ -363,7 +363,7 @@
    * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection
    * of values.
    */
-  static final class MaxOfCollection<S, T extends S> {
+  static final class MaxOfCollectionComparator<S, T extends S> {
     @BeforeTemplate
     T before(Collection<T> collection, Comparator<S> cmp) {
       return collection.stream().max(cmp).orElseThrow();
@@ -375,6 +375,22 @@
     }
   }
 
+  /**
+   * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection
+   * of values.
+   */
+  static final class MaxOfCollectionNaturalOrder<T extends Comparable<? super T>> {
+    @BeforeTemplate
+    T before(Collection<T> collection) {
+      return collection.stream().max(naturalOrder()).orElseThrow();
+    }
+
+    @AfterTemplate
+    T after(Collection<T> collection) {
+      return Collections.max(collection);
+    }
+  }
+
   /**
    * Avoid unnecessary creation of a {@link Stream} to determine the maximum of a known collection
    * of values.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or perhaps

Collections.max(collection, naturalOrder());

to

Collections.max(collection);

could be its own rule 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, it should be a separate rule, for maximum applicability. And likewise for min, of course, and with naturalOrder() and reverseOrder() variants. Are you up for a PR? 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense 👌

Are you up for a PR? 😄

Always 🫡

Copy link
Contributor

Choose a reason for hiding this comment

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

@BeforeTemplate
T before(Collection<T> collection, Comparator<S> cmp) {
return collection.stream().max(cmp).orElseThrow();
}

@AfterTemplate
T after(Collection<T> collection, Comparator<S> 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<S, T extends S> {
@BeforeTemplate
T before(@Repeated T value, Comparator<T> cmp) {
T before(@Repeated T value, Comparator<S> cmp) {
return Stream.of(Refaster.asVarargs(value)).max(cmp).orElseThrow();
}

@AfterTemplate
T after(@Repeated T value, Comparator<T> cmp) {
T after(@Repeated T value, Comparator<S> cmp) {
return Collections.max(Arrays.asList(value), cmp);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ ImmutableSet<Integer> testCompareTo() {
Comparator.<String>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();
}
Expand Down Expand Up @@ -135,6 +143,14 @@ ImmutableSet<Object> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ ImmutableSet<Integer> 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());
}
Expand Down Expand Up @@ -126,6 +134,14 @@ ImmutableSet<Object> 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());
}
Expand Down