OptionalRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

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

Table of contents
  1. OptionalOfNullable
  2. OptionalIsEmpty
  3. OptionalIsPresent
  4. OptionalOrElseThrow
  5. OptionalOrElseThrowMethodReference
  6. OptionalEqualsOptional
  7. OptionalFirstIteratorElement
  8. TernaryOperatorOptionalPositiveFiltering
  9. TernaryOperatorOptionalNegativeFiltering
  10. MapOptionalToBoolean
  11. MapToNullable
  12. FlatMapToOptional
  13. OrOrElseThrow
  14. OptionalOrElseGet
  15. StreamFlatMapOptional
  16. StreamMapToOptionalGet
  17. FilterOuterOptionalAfterFlatMap
  18. MapOuterOptionalAfterFlatMap
  19. FlatMapOuterOptionalAfterFlatMap
  20. OptionalOrOtherOptional
  21. OptionalIdentity
  22. OptionalFilter
  23. OptionalMap
  24. OptionalStream

OptionalOfNullable

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOfNullable).* as compiler argument.

Samples

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

 ImmutableSet<Optional<String>> testOptionalOfNullable() {
-    return ImmutableSet.of(
-        toString() == null ? Optional.empty() : Optional.of(toString()),
-        toString() != null ? Optional.of(toString()) : Optional.empty());
+    return ImmutableSet.of(Optional.ofNullable(toString()), Optional.ofNullable(toString()));
   }

OptionalIsEmpty

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalIsEmpty).* as compiler argument.

Samples

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

 ImmutableSet<Boolean> testOptionalIsEmpty() {
-    return ImmutableSet.of(!Optional.empty().isPresent(), !Optional.of("foo").isPresent());
+    return ImmutableSet.of(Optional.empty().isEmpty(), Optional.of("foo").isEmpty());
   }

OptionalIsPresent

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalIsPresent).* as compiler argument.

Samples

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

 ImmutableSet<Boolean> testOptionalIsPresent() {
-    return ImmutableSet.of(!Optional.empty().isEmpty(), !Optional.of("foo").isEmpty());
+    return ImmutableSet.of(Optional.empty().isPresent(), Optional.of("foo").isPresent());
   }

OptionalOrElseThrow

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOrElseThrow).* as compiler argument.

Samples

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

 String testOptionalOrElseThrow() {
-    return Optional.of("foo").get();
+    return Optional.of("foo").orElseThrow();
   }

OptionalOrElseThrowMethodReference

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOrElseThrowMethodReference).* as compiler argument.

Samples

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

 Function<Optional<Integer>, Integer> testOptionalOrElseThrowMethodReference() {
-    return Optional::get;
+    return Optional::orElseThrow;
   }

OptionalEqualsOptional

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalEqualsOptional).* as compiler argument.

Samples

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

 ImmutableSet<Boolean> testOptionalEqualsOptional() {
     return ImmutableSet.of(
-        Optional.of("foo").filter("bar"::equals).isPresent(),
-        Optional.of("baz").stream().anyMatch("qux"::equals));
+        Optional.of("foo").equals(Optional.of("bar")),
+        Optional.of("baz").equals(Optional.of("qux")));
   }

OptionalFirstIteratorElement

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFirstIteratorElement).* as compiler argument.

Samples

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

 ImmutableSet<Optional<String>> testOptionalFirstIteratorElement() {
     return ImmutableSet.of(
-        ImmutableSet.of("foo").iterator().hasNext()
-            ? Optional.of(ImmutableSet.of("foo").iterator().next())
-            : Optional.empty(),
-        !ImmutableSet.of("foo").iterator().hasNext()
-            ? Optional.empty()
-            : Optional.of(ImmutableSet.of("foo").iterator().next()));
+        stream(ImmutableSet.of("foo").iterator()).findFirst(),
+        stream(ImmutableSet.of("foo").iterator()).findFirst());
   }

TernaryOperatorOptionalPositiveFiltering

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$TernaryOperatorOptionalPositiveFiltering).* as compiler argument.

Samples

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

 Optional<String> testTernaryOperatorOptionalPositiveFiltering() {
-    return "foo".length() > 5 ? Optional.of("foo") : Optional.empty();
+    return /* Or Optional.ofNullable (can't auto-infer). */ Optional.of("foo")
+        .filter(v -> v.length() > 5);
   }

TernaryOperatorOptionalNegativeFiltering

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$TernaryOperatorOptionalNegativeFiltering).* as compiler argument.

Samples

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

 Optional<String> testTernaryOperatorOptionalNegativeFiltering() {
-    return "foo".length() > 5 ? Optional.empty() : Optional.of("foo");
+    return /* Or Optional.ofNullable (can't auto-infer). */ Optional.of("foo")
+        .filter(v -> v.length() <= 5);
   }

MapOptionalToBoolean

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$MapOptionalToBoolean).* as compiler argument.

Samples

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

 ImmutableSet<Boolean> testMapOptionalToBoolean() {
     return ImmutableSet.of(
-        Optional.of("foo").map(String::isEmpty).orElse(false),
-        Optional.of("bar").map(s -> s.isEmpty()).orElse(Boolean.FALSE));
+        Optional.of("foo").filter(String::isEmpty).isPresent(),
+        Optional.of("bar").filter(s -> s.isEmpty()).isPresent());
   }

MapToNullable

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$MapToNullable).* as compiler argument.

Samples

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

 ImmutableSet<Optional<String>> testMapToNullable() {
     return ImmutableSet.of(
-        Optional.of(1).flatMap(n -> Optional.of(String.valueOf(n))),
-        Optional.of(2).flatMap(n -> Optional.ofNullable(String.valueOf(n))));
+        Optional.of(1).map(n -> String.valueOf(n)), Optional.of(2).map(n -> String.valueOf(n)));
   }

FlatMapToOptional

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$FlatMapToOptional).* as compiler argument.

Samples

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

 Optional<String> testFlatMapToOptional() {
-    return Optional.of(1).map(n -> Optional.of(String.valueOf(n)).orElseThrow());
+    return Optional.of(1).flatMap(n -> Optional.of(String.valueOf(n)));
   }

OrOrElseThrow

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OrOrElseThrow).* as compiler argument.

Samples

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

 String testOrOrElseThrow() {
-    return Optional.of("foo").orElseGet(() -> Optional.of("bar").orElseThrow());
+    return Optional.of("foo").or(() -> Optional.of("bar")).orElseThrow();
   }

OptionalOrElseGet

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOrElseGet).* as compiler argument.

Samples

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

 ImmutableSet<String> testOptionalOrElseGet() {
     return ImmutableSet.of(
         Optional.of("foo").orElse("bar"),
         Optional.of("baz").orElse(toString()),
-        Optional.of("qux").orElse(String.valueOf(true)));
+        Optional.of("qux").orElseGet(() -> String.valueOf(true)));
   }

StreamFlatMapOptional

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$StreamFlatMapOptional).* as compiler argument.

Samples

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

 ImmutableSet<Object> testStreamFlatMapOptional() {
     return ImmutableSet.of(
-        Stream.of(Optional.empty()).filter(Optional::isPresent).map(Optional::orElseThrow),
-        Stream.of(Optional.of("foo")).flatMap(Streams::stream));
+        Stream.of(Optional.empty()).flatMap(Optional::stream),
+        Stream.of(Optional.of("foo")).flatMap(Optional::stream));
   }

StreamMapToOptionalGet

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$StreamMapToOptionalGet).* as compiler argument.

Samples

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

 Stream<String> testStreamMapToOptionalGet() {
-    return Stream.of(1).map(n -> Optional.of(String.valueOf(n)).orElseThrow());
+    return Stream.of(1).flatMap(n -> Optional.of(String.valueOf(n)).stream());
   }

FilterOuterOptionalAfterFlatMap

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$FilterOuterOptionalAfterFlatMap).* as compiler argument.

Samples

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

 Optional<Integer> testFilterOuterOptionalAfterFlatMap() {
-    return Optional.of("foo").flatMap(v -> Optional.of(v.length()).filter(len -> len > 0));
+    return Optional.of("foo").flatMap(v -> Optional.of(v.length())).filter(len -> len > 0);
   }

MapOuterOptionalAfterFlatMap

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$MapOuterOptionalAfterFlatMap).* as compiler argument.

Samples

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

 Optional<Integer> testMapOuterOptionalAfterFlatMap() {
-    return Optional.of("foo").flatMap(v -> Optional.of(v.length()).map(len -> len * 0));
+    return Optional.of("foo").flatMap(v -> Optional.of(v.length())).map(len -> len * 0);
   }

FlatMapOuterOptionalAfterFlatMap

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$FlatMapOuterOptionalAfterFlatMap).* as compiler argument.

Samples

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

 Optional<Integer> testFlatMapOuterOptionalAfterFlatMap() {
-    return Optional.of("foo").flatMap(v -> Optional.of(v.length()).flatMap(Optional::of));
+    return Optional.of("foo").flatMap(v -> Optional.of(v.length())).flatMap(Optional::of);
   }

OptionalOrOtherOptional

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOrOtherOptional).* as compiler argument.

Samples

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

 ImmutableSet<Optional<String>> testOptionalOrOtherOptional() {
     return ImmutableSet.of(
-        Optional.of("foo").map(Optional::of).orElse(Optional.of("bar")),
-        Optional.of("baz").map(Optional::of).orElseGet(() -> Optional.of("qux")),
-        Stream.of(Optional.of("quux"), Optional.of("quuz")).flatMap(Optional::stream).findFirst(),
-        Optional.of("corge").isPresent() ? Optional.of("corge") : Optional.of("grault"));
+        Optional.of("foo").or(() -> Optional.of("bar")),
+        Optional.of("baz").or(() -> Optional.of("qux")),
+        Optional.of("quux").or(() -> Optional.of("quuz")),
+        Optional.of("corge").or(() -> Optional.of("grault")));
   }

OptionalIdentity

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalIdentity).* as compiler argument.

Samples

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

 ImmutableSet<Optional<String>> testOptionalIdentity() {
     return ImmutableSet.of(
-        Optional.of("foo").or(() -> Optional.empty()),
-        Optional.of("bar").or(Optional::empty),
-        Optional.of("baz").stream().findFirst(),
-        Optional.of("qux").stream().findAny(),
-        Optional.of("quux").stream().min(String::compareTo),
-        Optional.of("quuz").stream().max(String::compareTo));
+        Optional.of("foo"),
+        Optional.of("bar"),
+        Optional.of("baz"),
+        Optional.of("qux"),
+        Optional.of("quux"),
+        Optional.of("quuz"));
   }

OptionalFilter

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFilter).* as compiler argument.

Samples

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

 ImmutableSet<Optional<String>> testOptionalFilter() {
     return ImmutableSet.of(
-        Optional.of("foo").stream().filter(String::isEmpty).findFirst(),
-        Optional.of("bar").stream().filter(String::isEmpty).findAny());
+        Optional.of("foo").filter(String::isEmpty), Optional.of("bar").filter(String::isEmpty));
   }

OptionalMap

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalMap).* as compiler argument.

Samples

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

 Optional<String> testOptionalMap() {
-    return Optional.of(1).stream().map(String::valueOf).findAny();
+    return Optional.of(1).map(String::valueOf);
   }

OptionalStream

SUGGESTION

Simplification

Suppression

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

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalStream).* as compiler argument.

Samples

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

 ImmutableSet<Stream<String>> testOptionalStream() {
-    return ImmutableSet.of(
-        Optional.of("foo").map(Stream::of).orElse(Stream.empty()),
-        Optional.of("bar").map(Stream::of).orElseGet(Stream::empty));
+    return ImmutableSet.of(Optional.of("foo").stream(), Optional.of("bar").stream());
   }

Copyright © 2017-2024 Picnic Technologies BV