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

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. OptionalFirstIteratorElement
  7. TernaryOperatorOptionalPositiveFiltering
  8. TernaryOperatorOptionalNegativeFiltering
  9. MapOptionalToBoolean
  10. MapToNullable
  11. FlatMapToOptional
  12. OrOrElseThrow
  13. StreamFlatMapOptional
  14. StreamMapToOptionalGet
  15. FilterOuterOptionalAfterFlatMap
  16. MapOuterOptionalAfterFlatMap
  17. FlatMapOuterOptionalAfterFlatMap
  18. OptionalOrOtherOptional
  19. OptionalIdentity
  20. OptionalFilter
  21. OptionalMap

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;
   }

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();
   }

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("foo").or(() -> Optional.of("bar")),
+        Optional.of("baz").or(() -> Optional.of("qux")),
+        Optional.of("quux").or(() -> Optional.of("quuz")));
   }

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").stream().findFirst(),
-        Optional.of("bar").stream().findAny(),
-        Optional.of("baz").stream().min(String::compareTo),
-        Optional.of("qux").stream().max(String::compareTo));
+        Optional.of("foo"), Optional.of("bar"), Optional.of("baz"), Optional.of("qux"));
   }

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);
   }

Copyright © 2017-2023 Picnic Technologies BV