Skip to main content Link Search Menu Expand Document (external link)

ComparatorRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

Disable all rules by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules.*)) as compiler argument.

Table of contents
  1. NaturalOrder
  2. ReverseOrder
  3. CustomComparator
  4. ThenComparing
  5. ThenComparingReversed
  6. ThenComparingCustom
  7. ThenComparingCustomReversed
  8. ThenComparingDouble
  9. ThenComparingInt
  10. ThenComparingLong
  11. ThenComparingNaturalOrder
  12. CompareTo
  13. MinOfVarargs
  14. MinOfPairNaturalOrder
  15. MinOfPairCustomOrder
  16. MaxOfVarargs
  17. MaxOfPairNaturalOrder
  18. MaxOfPairCustomOrder
  19. ComparatorsMin
  20. ComparatorsMax

NaturalOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("NaturalOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.NaturalOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Comparator<String>> testNaturalOrder() {
     return ImmutableSet.of(
-        String::compareTo,
-        Comparator.comparing(identity()),
-        Comparator.comparing(s -> s),
-        Collections.<String>reverseOrder(reverseOrder()),
-        Comparator.<String>reverseOrder().reversed());
+        naturalOrder(), naturalOrder(), naturalOrder(), naturalOrder(), naturalOrder());
   }

ReverseOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ReverseOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ReverseOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Comparator<String>> testReverseOrder() {
     return ImmutableSet.of(
-        Collections.reverseOrder(),
-        Collections.<String>reverseOrder(naturalOrder()),
-        Comparator.<String>naturalOrder().reversed());
+        Comparator.reverseOrder(), Comparator.reverseOrder(), Comparator.reverseOrder());
   }

CustomComparator

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("CustomComparator") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.CustomComparator)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Comparator<String>> testCustomComparator() {
     return ImmutableSet.of(
-        Comparator.comparing(identity(), Comparator.comparingInt(String::length)),
-        Comparator.comparing(s -> s, Comparator.comparingInt(String::length)));
+        Comparator.comparingInt(String::length), Comparator.comparingInt(String::length));
   }

ThenComparing

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparing") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparing)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<String> testThenComparing() {
-    return Comparator.<String>naturalOrder().thenComparing(Comparator.comparing(String::isEmpty));
+    return Comparator.<String>naturalOrder().thenComparing(String::isEmpty);
   }

ThenComparingReversed

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingReversed") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingReversed)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<String> testThenComparingReversed() {
-    return Comparator.<String>naturalOrder()
-        .thenComparing(Comparator.comparing(String::isEmpty).reversed());
+    return Comparator.<String>naturalOrder().thenComparing(String::isEmpty, reverseOrder());
   }

ThenComparingCustom

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingCustom") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingCustom)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<String> testThenComparingCustom() {
-    return Comparator.<String>naturalOrder()
-        .thenComparing(Comparator.comparing(String::isEmpty, reverseOrder()));
+    return Comparator.<String>naturalOrder().thenComparing(String::isEmpty, reverseOrder());
   }

ThenComparingCustomReversed

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingCustomReversed") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingCustomReversed)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<String> testThenComparingCustomReversed() {
     return Comparator.<String>naturalOrder()
-        .thenComparing(
-            Comparator.comparing(String::isEmpty, Comparator.<Boolean>reverseOrder()).reversed());
+        .thenComparing(String::isEmpty, Comparator.<Boolean>reverseOrder().reversed());
   }

ThenComparingDouble

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingDouble") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingDouble)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<Integer> testThenComparingDouble() {
-    return Comparator.<Integer>naturalOrder()
-        .thenComparing(Comparator.comparingDouble(Integer::doubleValue));
+    return Comparator.<Integer>naturalOrder().thenComparingDouble(Integer::doubleValue);
   }

ThenComparingInt

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingInt") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingInt)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<Integer> testThenComparingInt() {
-    return Comparator.<Integer>naturalOrder()
-        .thenComparing(Comparator.comparingInt(Integer::intValue));
+    return Comparator.<Integer>naturalOrder().thenComparingInt(Integer::intValue);
   }

ThenComparingLong

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingLong") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingLong)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Comparator<Integer> testThenComparingLong() {
-    return Comparator.<Integer>naturalOrder()
-        .thenComparing(Comparator.comparingLong(Integer::longValue));
+    return Comparator.<Integer>naturalOrder().thenComparingLong(Integer::longValue);
   }

ThenComparingNaturalOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ThenComparingNaturalOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ThenComparingNaturalOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Comparator<String>> testThenComparingNaturalOrder() {
     return ImmutableSet.of(
-        Comparator.<String>naturalOrder().thenComparing(identity()),
-        Comparator.<String>naturalOrder().thenComparing(s -> s));
+        Comparator.<String>naturalOrder().thenComparing(naturalOrder()),
+        Comparator.<String>naturalOrder().thenComparing(naturalOrder()));
   }

CompareTo

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("CompareTo") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.CompareTo)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Integer> testCompareTo() {
-    return ImmutableSet.of(
-        Comparator.<String>naturalOrder().compare("foo", "bar"),
-        Comparator.<String>reverseOrder().compare("baz", "qux"));
+    return ImmutableSet.of("foo".compareTo("bar"), "qux".compareTo("baz"));
   }

MinOfVarargs

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MinOfVarargs") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.MinOfVarargs)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testMinOfVarargs() {
-    return Stream.of(1, 2).min(naturalOrder()).orElseThrow();
+    return Collections.min(Arrays.asList(1, 2), naturalOrder());
   }

MinOfPairNaturalOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MinOfPairNaturalOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.MinOfPairNaturalOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testMinOfPairNaturalOrder() {
     return ImmutableSet.of(
-        "a".compareTo("b") <= 0 ? "a" : "b",
-        "a".compareTo("b") > 0 ? "b" : "a",
-        "a".compareTo("b") < 0 ? "a" : "b",
-        "a".compareTo("b") >= 0 ? "b" : "a",
-        Comparators.min("a", "b", naturalOrder()),
-        Comparators.max("a", "b", reverseOrder()),
-        Collections.min(Arrays.asList("a", "b")),
-        Collections.min(ImmutableList.of("a", "b")),
-        Collections.min(ImmutableSet.of("a", "b")));
+        Comparators.min("a", "b"),
+        Comparators.min("a", "b"),
+        Comparators.min("b", "a"),
+        Comparators.min("b", "a"),
+        Comparators.min("a", "b"),
+        Comparators.min("a", "b"),
+        Comparators.min("a", "b"),
+        Comparators.min("a", "b"),
+        Comparators.min("a", "b"));
   }

MinOfPairCustomOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MinOfPairCustomOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.MinOfPairCustomOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Object> testMinOfPairCustomOrder() {
     return ImmutableSet.of(
-        Comparator.comparingInt(String::length).compare("a", "b") <= 0 ? "a" : "b",
-        Comparator.comparingInt(String::length).compare("a", "b") > 0 ? "b" : "a",
-        Comparator.comparingInt(String::length).compare("a", "b") < 0 ? "a" : "b",
-        Comparator.comparingInt(String::length).compare("a", "b") >= 0 ? "b" : "a",
-        Collections.min(Arrays.asList("a", "b"), (a, b) -> -1),
-        Collections.min(ImmutableList.of("a", "b"), (a, b) -> 0),
-        Collections.min(ImmutableSet.of("a", "b"), (a, b) -> 1));
+        Comparators.min("a", "b", Comparator.comparingInt(String::length)),
+        Comparators.min("a", "b", Comparator.comparingInt(String::length)),
+        Comparators.min("b", "a", Comparator.comparingInt(String::length)),
+        Comparators.min("b", "a", Comparator.comparingInt(String::length)),
+        Comparators.min("a", "b", (a, b) -> -1),
+        Comparators.min("a", "b", (a, b) -> 0),
+        Comparators.min("a", "b", (a, b) -> 1));
   }

MaxOfVarargs

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MaxOfVarargs") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.MaxOfVarargs)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testMaxOfVarargs() {
-    return Stream.of(1, 2).max(naturalOrder()).orElseThrow();
+    return Collections.max(Arrays.asList(1, 2), naturalOrder());
   }

MaxOfPairNaturalOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MaxOfPairNaturalOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.MaxOfPairNaturalOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testMaxOfPairNaturalOrder() {
     return ImmutableSet.of(
-        "a".compareTo("b") >= 0 ? "a" : "b",
-        "a".compareTo("b") < 0 ? "b" : "a",
-        "a".compareTo("b") > 0 ? "a" : "b",
-        "a".compareTo("b") <= 0 ? "b" : "a",
-        Comparators.max("a", "b", naturalOrder()),
-        Comparators.min("a", "b", reverseOrder()),
-        Collections.max(Arrays.asList("a", "b")),
-        Collections.max(ImmutableList.of("a", "b")),
-        Collections.max(ImmutableSet.of("a", "b")));
+        Comparators.max("a", "b"),
+        Comparators.max("a", "b"),
+        Comparators.max("b", "a"),
+        Comparators.max("b", "a"),
+        Comparators.max("a", "b"),
+        Comparators.max("a", "b"),
+        Comparators.max("a", "b"),
+        Comparators.max("a", "b"),
+        Comparators.max("a", "b"));
   }

MaxOfPairCustomOrder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MaxOfPairCustomOrder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.MaxOfPairCustomOrder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Object> testMaxOfPairCustomOrder() {
     return ImmutableSet.of(
-        Comparator.comparingInt(String::length).compare("a", "b") >= 0 ? "a" : "b",
-        Comparator.comparingInt(String::length).compare("a", "b") < 0 ? "b" : "a",
-        Comparator.comparingInt(String::length).compare("a", "b") > 0 ? "a" : "b",
-        Comparator.comparingInt(String::length).compare("a", "b") <= 0 ? "b" : "a",
-        Collections.max(Arrays.asList("a", "b"), (a, b) -> -1),
-        Collections.max(ImmutableList.of("a", "b"), (a, b) -> 0),
-        Collections.max(ImmutableSet.of("a", "b"), (a, b) -> 1));
+        Comparators.max("a", "b", Comparator.comparingInt(String::length)),
+        Comparators.max("a", "b", Comparator.comparingInt(String::length)),
+        Comparators.max("b", "a", Comparator.comparingInt(String::length)),
+        Comparators.max("b", "a", Comparator.comparingInt(String::length)),
+        Comparators.max("a", "b", (a, b) -> -1),
+        Comparators.max("a", "b", (a, b) -> 0),
+        Comparators.max("a", "b", (a, b) -> 1));
   }

ComparatorsMin

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ComparatorsMin") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ComparatorsMin)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 BinaryOperator<String> testComparatorsMin() {
-    return BinaryOperator.minBy(naturalOrder());
+    return Comparators::min;
   }

ComparatorsMax

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("ComparatorsMax") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(ComparatorRules\.ComparatorsMax)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 BinaryOperator<String> testComparatorsMax() {
-    return BinaryOperator.maxBy(naturalOrder());
+    return Comparators::max;
   }

Copyright © 2017-2023 Picnic Technologies BV