OptionalRules
SUGGESTION
Simplification
Suppression
Disable all rules by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$).*as compiler argument.
Table of contents
- OptionalEmpty
- OptionalOfNullable
- OptionalIsEmpty
- OptionalIsPresent
- OptionalOrElseThrowWithOptional
- OptionalOrElseThrow
- OptionalEqualsOptionalOf
- StreamsStreamFindFirst
- RefasterEmitCommentBeforeOptionalOfFilter
- RefasterEmitCommentBeforeOptionalOfFilterNot
- OptionalFilterIsPresent
- OptionalMap
- OptionalFlatMap
- OptionalOrOrElseThrow
- OptionalOrElse
- StreamFlatMapOptionalStream
- StreamFlatMapStream
- OptionalFlatMapFilter
- OptionalFlatMapMap
- OptionalFlatMapFlatMap
- OptionalOr
- OptionalIdentity
- OptionalFilter
- OptionalMapWithFunction
- 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.
boolean testOptionalIsEmpty() {
- return !Optional.of("foo").isPresent();
+ return 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.
boolean testOptionalIsPresent() {
- return !Optional.of("foo").isEmpty();
+ return Optional.of("foo").isPresent();
}OptionalOrElseThrowWithOptional
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalOrElseThrowWithOptional")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOrElseThrowWithOptional).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
String testOptionalOrElseThrowWithOptional() {
- return Optional.of("foo").get();
+ return Optional.of("foo").orElseThrow();
}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.
Function<Optional<Integer>, Integer> testOptionalOrElseThrow() {
- return Optional::get;
+ return Optional::orElseThrow;
}OptionalEqualsOptionalOf
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalEqualsOptionalOf")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalEqualsOptionalOf).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Boolean> testOptionalEqualsOptionalOf() {
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")));
}StreamsStreamFindFirst
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StreamsStreamFindFirst")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$StreamsStreamFindFirst).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Optional<String>> testStreamsStreamFindFirst() {
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());
}RefasterEmitCommentBeforeOptionalOfFilter
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("RefasterEmitCommentBeforeOptionalOfFilter")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$RefasterEmitCommentBeforeOptionalOfFilter).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Optional<String>> testRefasterEmitCommentBeforeOptionalOfFilter() {
return ImmutableSet.of(
- "foo".length() > 5 ? Optional.of("foo") : Optional.empty(),
- !("bar".length() > 5) ? Optional.empty() : Optional.of("bar"));
+ /* Or Optional.ofNullable (can't auto-infer). */ Optional.of("foo")
+ .filter(v -> v.length() > 5),
+ /* Or Optional.ofNullable (can't auto-infer). */ Optional.of("bar")
+ .filter(v -> v.length() > 5));
}RefasterEmitCommentBeforeOptionalOfFilterNot
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("RefasterEmitCommentBeforeOptionalOfFilterNot")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$RefasterEmitCommentBeforeOptionalOfFilterNot).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Optional<String>> testRefasterEmitCommentBeforeOptionalOfFilterNot() {
return ImmutableSet.of(
- "foo".length() > 5 ? Optional.empty() : Optional.of("foo"),
- !("bar".length() > 5) ? Optional.of("bar") : Optional.empty());
+ /* Or Optional.ofNullable (can't auto-infer). */ Optional.of("foo")
+ .filter(v -> v.length() <= 5),
+ /* Or Optional.ofNullable (can't auto-infer). */ Optional.of("bar")
+ .filter(v -> v.length() <= 5));
}OptionalFilterIsPresent
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalFilterIsPresent")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFilterIsPresent).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
boolean testOptionalFilterIsPresent() {
- return Optional.of("foo").map(String::isEmpty).orElse(false);
+ return Optional.of("foo").filter(String::isEmpty).isPresent();
}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.
ImmutableSet<Optional<String>> testOptionalMap() {
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)));
}OptionalFlatMap
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalFlatMap")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFlatMap).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Optional<String> testOptionalFlatMap() {
- return Optional.of(1).map(n -> Optional.of(String.valueOf(n)).orElseThrow());
+ return Optional.of(1).flatMap(n -> Optional.of(String.valueOf(n)));
}OptionalOrOrElseThrow
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalOrOrElseThrow")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOrOrElseThrow).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
String testOptionalOrOrElseThrow() {
- 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(() -> toString()), Optional.of("bar").orElseGet(() -> "baz"));
+ Optional.of("foo").orElseGet(() -> toString()), Optional.of("bar").orElse("baz"));
}StreamFlatMapOptionalStream
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StreamFlatMapOptionalStream")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$StreamFlatMapOptionalStream).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Object> testStreamFlatMapOptionalStream() {
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));
}StreamFlatMapStream
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StreamFlatMapStream")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$StreamFlatMapStream).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Stream<String> testStreamFlatMapStream() {
- return Stream.of(1).map(n -> Optional.of(String.valueOf(n)).orElseThrow());
+ return Stream.of(1).flatMap(n -> Optional.of(String.valueOf(n)).stream());
}OptionalFlatMapFilter
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalFlatMapFilter")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFlatMapFilter).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Optional<Integer> testOptionalFlatMapFilter() {
- 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);
}OptionalFlatMapMap
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalFlatMapMap")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFlatMapMap).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Optional<Integer> testOptionalFlatMapMap() {
- 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);
}OptionalFlatMapFlatMap
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalFlatMapFlatMap")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalFlatMapFlatMap).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Optional<Integer> testOptionalFlatMapFlatMap() {
- 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);
}OptionalOr
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalOr")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalOr).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Optional<String>> testOptionalOr() {
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("corge")).flatMap(Optional::stream).findFirst(),
- Optional.of("grault").isPresent() ? Optional.of("grault") : Optional.of("garply"));
+ Optional.of("foo").or(() -> Optional.of("bar")),
+ Optional.of("baz").or(() -> Optional.of("qux")),
+ Optional.of("quux").or(() -> Optional.of("corge")),
+ Optional.of("grault").or(() -> Optional.of("garply")));
}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("corge").stream().findAny(),
- Optional.of("grault").stream().min(String::compareTo),
- Optional.of("garply").stream().max(String::compareTo));
+ Optional.of("foo"),
+ Optional.of("bar"),
+ Optional.of("baz"),
+ Optional.of("qux"),
+ Optional.of("quux"),
+ Optional.of("corge"),
+ Optional.of("grault"),
+ Optional.of("garply"));
}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));
}OptionalMapWithFunction
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalMapWithFunction")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!OptionalRules\$OptionalMapWithFunction).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Optional<String> testOptionalMapWithFunction() {
- 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();
}