Skip to content

Commit

Permalink
Introduce {Max,Min}ByNaturalOrder Refaster rules (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored Jan 16, 2024
1 parent e484926 commit 51317fb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static java.util.Comparator.comparingLong;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.minBy;

import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
Expand All @@ -21,11 +23,13 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
import tech.picnic.errorprone.refaster.matchers.IsIdentityOperation;
Expand Down Expand Up @@ -383,4 +387,36 @@ BinaryOperator<T> after() {
return Comparators::max;
}
}

/**
* Prefer {@link Comparator#naturalOrder()} over {@link Comparator#reverseOrder()} where possible.
*/
static final class MinByNaturalOrder<T extends Comparable<? super T>> {
@BeforeTemplate
Collector<T, ?, Optional<T>> before() {
return maxBy(reverseOrder());
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Collector<T, ?, Optional<T>> after() {
return minBy(naturalOrder());
}
}

/**
* Prefer {@link Comparator#naturalOrder()} over {@link Comparator#reverseOrder()} where possible.
*/
static final class MaxByNaturalOrder<T extends Comparable<? super T>> {
@BeforeTemplate
Collector<T, ?, Optional<T>> before() {
return minBy(reverseOrder());
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Collector<T, ?, Optional<T>> after() {
return maxBy(naturalOrder());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.minBy;

import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Collector;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

Expand Down Expand Up @@ -161,4 +165,12 @@ BinaryOperator<String> testComparatorsMin() {
BinaryOperator<String> testComparatorsMax() {
return BinaryOperator.maxBy(naturalOrder());
}

Collector<Integer, ?, Optional<Integer>> testMinByNaturalOrder() {
return maxBy(reverseOrder());
}

Collector<Integer, ?, Optional<Integer>> testMaxByNaturalOrder() {
return minBy(reverseOrder());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.minBy;

import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Collector;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

Expand Down Expand Up @@ -151,4 +155,12 @@ BinaryOperator<String> testComparatorsMin() {
BinaryOperator<String> testComparatorsMax() {
return Comparators::max;
}

Collector<Integer, ?, Optional<Integer>> testMinByNaturalOrder() {
return minBy(naturalOrder());
}

Collector<Integer, ?, Optional<Integer>> testMaxByNaturalOrder() {
return maxBy(naturalOrder());
}
}

0 comments on commit 51317fb

Please sign in to comment.