-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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); | ||
} | ||
} | ||
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the unit test and usage of ===================================================================
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or perhaps
to
could be its own rule 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense 👌
Always 🫡 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
.