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. OptionalEmpty
  2. OptionalOfNullable
  3. OptionalIsEmpty
  4. OptionalIsPresent
  5. OptionalOrElseThrow
  6. OptionalOrElseThrowMethodReference
  7. OptionalEqualsOptional
  8. OptionalFirstIteratorElement
  9. TernaryOperatorOptionalPositiveFiltering
  10. TernaryOperatorOptionalNegativeFiltering
  11. MapOptionalToBoolean
  12. MapToNullable
  13. FlatMapToOptional
  14. OrOrElseThrow
  15. OptionalOrElse
  16. StreamFlatMapOptional
  17. StreamMapToOptionalGet
  18. FilterOuterOptionalAfterFlatMap
  19. MapOuterOptionalAfterFlatMap
  20. FlatMapOuterOptionalAfterFlatMap
  21. OptionalOrOtherOptional
  22. OptionalIdentity
  23. OptionalFilter
  24. OptionalMap
  25. OptionalStream

OptionalEmpty

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 Optional<String> testOptionalEmpty() {
-  return Optional.ofNullable(null);
+  return Optional.empty();
 }

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

OptionalOrElse

SUGGESTION

Simplification

Suppression

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

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

Samples

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

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

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

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.

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

Copyright © 2017-2024 Picnic Technologies BV