Skip to content

Commit

Permalink
Introduce Collections{Max,Min,Sort} Refaster rules (#1297)
Browse files Browse the repository at this point in the history
While there, rename `{Max,Min}OfCollection` to
`Collections{Max,Min}WithComparator`.
  • Loading branch information
mohamedsamehsalah authored Aug 26, 2024
1 parent ca5c3dd commit 73ed6e3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
Expand Down Expand Up @@ -244,6 +245,33 @@ int after(T value1, T value2) {
}
}

/** Prefer {@link Collections#sort(List)} over more verbose alternatives. */
static final class CollectionsSort<T extends Comparable<? super T>> {
@BeforeTemplate
void before(List<T> collection) {
Collections.sort(collection, naturalOrder());
}

@AfterTemplate
void after(List<T> collection) {
Collections.sort(collection);
}
}

/** Prefer {@link Collections#min(Collection)} over more verbose alternatives. */
static final class CollectionsMin<T extends Comparable<? super T>> {
@BeforeTemplate
T before(Collection<T> collection) {
return Refaster.anyOf(
Collections.min(collection, naturalOrder()), Collections.max(collection, reverseOrder()));
}

@AfterTemplate
T after(Collection<T> collection) {
return Collections.min(collection);
}
}

/**
* Avoid unnecessary creation of a {@link Stream} to determine the minimum of a known collection
* of values.
Expand All @@ -264,7 +292,7 @@ T after(T[] array, Comparator<S> 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> {
static final class CollectionsMinWithComparator<S, T extends S> {
@BeforeTemplate
T before(Collection<T> collection, Comparator<S> cmp) {
return collection.stream().min(cmp).orElseThrow();
Expand Down Expand Up @@ -343,6 +371,20 @@ T after(T value1, T value2, Comparator<? super T> cmp) {
}
}

/** Prefer {@link Collections#max(Collection)} over more verbose alternatives. */
static final class CollectionsMax<T extends Comparable<? super T>> {
@BeforeTemplate
T before(Collection<T> collection) {
return Refaster.anyOf(
Collections.max(collection, naturalOrder()), Collections.min(collection, reverseOrder()));
}

@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.
Expand All @@ -363,7 +405,7 @@ T after(T[] array, Comparator<S> 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> {
static final class CollectionsMaxWithComparator<S, T extends S> {
@BeforeTemplate
T before(Collection<T> collection, Comparator<S> cmp) {
return collection.stream().max(cmp).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,21 @@ ImmutableSet<Integer> testCompareTo() {
Comparator.<String>reverseOrder().compare("baz", "qux"));
}

void testCollectionsSort() {
Collections.sort(ImmutableList.of("foo", "bar"), naturalOrder());
}

ImmutableSet<String> testCollectionsMin() {
return ImmutableSet.of(
Collections.min(ImmutableList.of("foo"), naturalOrder()),
Collections.max(ImmutableList.of("bar"), reverseOrder()));
}

String testMinOfArray() {
return Arrays.stream(new String[0]).min(naturalOrder()).orElseThrow();
}

String testMinOfCollection() {
String testCollectionsMinWithComparator() {
return ImmutableSet.of("foo", "bar").stream().min(naturalOrder()).orElseThrow();
}

Expand Down Expand Up @@ -143,11 +153,17 @@ ImmutableSet<Object> testMinOfPairCustomOrder() {
Collections.min(ImmutableSet.of("a", "b"), (a, b) -> 1));
}

ImmutableSet<String> testCollectionsMax() {
return ImmutableSet.of(
Collections.max(ImmutableList.of("foo"), naturalOrder()),
Collections.min(ImmutableList.of("bar"), reverseOrder()));
}

String testMaxOfArray() {
return Arrays.stream(new String[0]).max(naturalOrder()).orElseThrow();
}

String testMaxOfCollection() {
String testCollectionsMaxWithComparator() {
return ImmutableSet.of("foo", "bar").stream().max(naturalOrder()).orElseThrow();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,20 @@ ImmutableSet<Integer> testCompareTo() {
return ImmutableSet.of("foo".compareTo("bar"), "qux".compareTo("baz"));
}

void testCollectionsSort() {
Collections.sort(ImmutableList.of("foo", "bar"));
}

ImmutableSet<String> testCollectionsMin() {
return ImmutableSet.of(
Collections.min(ImmutableList.of("foo")), Collections.min(ImmutableList.of("bar")));
}

String testMinOfArray() {
return Collections.min(Arrays.asList(new String[0]), naturalOrder());
}

String testMinOfCollection() {
String testCollectionsMinWithComparator() {
return Collections.min(ImmutableSet.of("foo", "bar"), naturalOrder());
}

Expand Down Expand Up @@ -134,11 +143,16 @@ ImmutableSet<Object> testMinOfPairCustomOrder() {
Comparators.min("a", "b", (a, b) -> 1));
}

ImmutableSet<String> testCollectionsMax() {
return ImmutableSet.of(
Collections.max(ImmutableList.of("foo")), Collections.max(ImmutableList.of("bar")));
}

String testMaxOfArray() {
return Collections.max(Arrays.asList(new String[0]), naturalOrder());
}

String testMaxOfCollection() {
String testCollectionsMaxWithComparator() {
return Collections.max(ImmutableSet.of("foo", "bar"), naturalOrder());
}

Expand Down

0 comments on commit 73ed6e3

Please sign in to comment.