Skip to content

Commit

Permalink
Introduce {Max,Min}Of{Array,Collection} Refaster rules (#1276)
Browse files Browse the repository at this point in the history
While there, generalize the `{Max,Min}OfVarargs` rules.
  • Loading branch information
Stephan202 authored Aug 23, 2024
1 parent 4cbfdba commit ec54e79
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
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
* 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> {
@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

0 comments on commit ec54e79

Please sign in to comment.