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

Conversation

Stephan202
Copy link
Member

@Stephan202 Stephan202 commented Aug 10, 2024

Suggested commit message:

Introduce `{Max,Min}Of{Array,Collection}` Refaster rules (#1276)

While there, generalize the `{Max,Min}OfVarargs` rules.

@Stephan202 Stephan202 added this to the 0.18.0 milestone Aug 10, 2024
@@ -243,6 +244,38 @@ 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.

Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

Clean 🧹

One small enquiry 💭

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

@Stephan202 Stephan202 modified the milestones: 0.18.0, 0.19.0 Aug 11, 2024
Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

🚀

* 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

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 🫡

@rickie rickie force-pushed the sschroevers/more-min-max-rules branch from 4982575 to 1ca77b4 Compare August 22, 2024 14:58
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

@rickie rickie merged commit ec54e79 into master Aug 23, 2024
16 checks passed
@rickie rickie deleted the sschroevers/more-min-max-rules branch August 23, 2024 06:47
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.

3 participants