Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce assorted Optional Refaster rules #764

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ Function<Optional<T>, T> after() {
}
}

/** Prefer {@link Optional#equals(Object)} over more contrived alternatives. */
static final class OptionalEqualsOptional<T, S> {
@BeforeTemplate
boolean before(Optional<T> optional, S value) {
return Refaster.anyOf(
optional.filter(value::equals).isPresent(), optional.stream().anyMatch(value::equals));
}

@AfterTemplate
boolean after(Optional<T> optional, S value) {
return optional.equals(Optional.of(value));
}
}

/**
* Don't use the ternary operator to extract the first element of a possibly-empty {@link
* Iterator} as an {@link Optional}.
Expand Down Expand Up @@ -350,13 +364,14 @@ static final class OptionalOrOtherOptional<T> {
Optional<T> before(Optional<T> optional1, Optional<T> optional2) {
// XXX: Note that rewriting the first and third variant will change the code's behavior if
// `optional2` has side-effects.
// XXX: Note that rewriting the first and third variant will introduce a compilation error if
// `optional2` is not effectively final. Review whether a `@Matcher` can be used to avoid
// this.
// XXX: Note that rewriting the first, third and fourth variant will introduce a compilation
// error if `optional2` is not effectively final. Review whether a `@Matcher` can be used to
// avoid this.
return Refaster.anyOf(
optional1.map(Optional::of).orElse(optional2),
optional1.map(Optional::of).orElseGet(() -> optional2),
Stream.of(optional1, optional2).flatMap(Optional::stream).findFirst());
Stream.of(optional1, optional2).flatMap(Optional::stream).findFirst(),
optional1.isPresent() ? optional1 : optional2);
}

@AfterTemplate
Expand Down Expand Up @@ -422,12 +437,22 @@ Optional<? extends T> after(Optional<S> optional, Function<? super S, ? extends
}
}

// XXX: Add a rule for:
// `optional.flatMap(x -> pred(x) ? Optional.empty() : Optional.of(x))` and variants.
// (Maybe canonicalize the inner expression. Maybe we rewrite already.)
/** Prefer {@link Optional#stream()} over more contrived alternatives. */
static final class OptionalStream<T> {
@BeforeTemplate
Stream<T> before(Optional<T> optional) {
return Refaster.anyOf(
optional.map(Stream::of).orElse(Stream.empty()),
optional.map(Stream::of).orElseGet(Stream::empty));
}

@AfterTemplate
Stream<T> after(Optional<T> optional) {
return optional.stream();
}
}

// XXX: Add a rule for:
// `optional.map(Stream::of).orElse(Stream.empty())`
// `optional.map(Stream::of).orElseGet(Stream::empty)`
// -> `optional.stream()`
// `optional.flatMap(x -> pred(x) ? Optional.empty() : Optional.of(x))` and variants.
// (Maybe canonicalize the inner expression. Maybe we rewrite it already.)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Function<Optional<Integer>, Integer> testOptionalOrElseThrowMethodReference() {
return Optional::get;
}

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

ImmutableSet<Optional<String>> testOptionalFirstIteratorElement() {
return ImmutableSet.of(
ImmutableSet.of("foo").iterator().hasNext()
Expand Down Expand Up @@ -73,6 +79,13 @@ String testOrOrElseThrow() {
return Optional.of("foo").orElseGet(() -> Optional.of("bar").orElseThrow());
}

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

ImmutableSet<Object> testStreamFlatMapOptional() {
return ImmutableSet.of(
Stream.of(Optional.empty()).filter(Optional::isPresent).map(Optional::orElseThrow),
Expand All @@ -99,7 +112,8 @@ 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());
Stream.of(Optional.of("quux"), Optional.of("quuz")).flatMap(Optional::stream).findFirst(),
Optional.of("corge").isPresent() ? Optional.of("corge") : Optional.of("grault"));
}

ImmutableSet<Optional<String>> testOptionalIdentity() {
Expand All @@ -120,10 +134,9 @@ Optional<String> testOptionalMap() {
return Optional.of(1).stream().map(String::valueOf).findAny();
}

ImmutableSet<String> testOptionalOrElseGet() {
ImmutableSet<Stream<String>> testOptionalStream() {
return ImmutableSet.of(
Optional.of("foo").orElse("bar"),
Optional.of("baz").orElse(toString()),
Optional.of("qux").orElse(String.valueOf(true)));
Optional.of("foo").map(Stream::of).orElse(Stream.empty()),
Optional.of("bar").map(Stream::of).orElseGet(Stream::empty));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Function<Optional<Integer>, Integer> testOptionalOrElseThrowMethodReference() {
return Optional::orElseThrow;
}

ImmutableSet<Boolean> testOptionalEqualsOptional() {
return ImmutableSet.of(
Optional.of("foo").equals(Optional.of("bar")),
Optional.of("baz").equals(Optional.of("qux")));
}

ImmutableSet<Optional<String>> testOptionalFirstIteratorElement() {
return ImmutableSet.of(
stream(ImmutableSet.of("foo").iterator()).findFirst(),
Expand Down Expand Up @@ -70,6 +76,13 @@ String testOrOrElseThrow() {
return Optional.of("foo").or(() -> Optional.of("bar")).orElseThrow();
}

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

ImmutableSet<Object> testStreamFlatMapOptional() {
return ImmutableSet.of(
Stream.of(Optional.empty()).flatMap(Optional::stream),
Expand All @@ -96,7 +109,8 @@ ImmutableSet<Optional<String>> testOptionalOrOtherOptional() {
return ImmutableSet.of(
Optional.of("foo").or(() -> Optional.of("bar")),
Optional.of("baz").or(() -> Optional.of("qux")),
Optional.of("quux").or(() -> Optional.of("quuz")));
Optional.of("quux").or(() -> Optional.of("quuz")),
Optional.of("corge").or(() -> Optional.of("grault")));
}

ImmutableSet<Optional<String>> testOptionalIdentity() {
Expand All @@ -113,10 +127,7 @@ Optional<String> testOptionalMap() {
return Optional.of(1).map(String::valueOf);
}

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