Skip to content

Commit

Permalink
Introduce OptionalStream{Filter,Map} Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
werli committed Nov 2, 2022
1 parent be24eda commit d4c6743
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,41 @@ Optional<T> after(Optional<T> optional) {
}
}

/**
* Avoid unnecessary {@link Optional} to {@link Stream} conversion when filtering that ultimately
* returns the same result.
*/
static final class OptionalStreamFilter<T> {
@BeforeTemplate
Optional<T> before(Optional<T> optional, Predicate<? super T> predicate) {
return Refaster.anyOf(
optional.stream().filter(predicate).findAny(),
optional.stream().filter(predicate).findFirst());
}

@AfterTemplate
Optional<T> after(Optional<T> optional, Predicate<? super T> predicate) {
return optional.filter(predicate);
}
}

/**
* Avoid unnecessary {@link Optional} to {@link Stream} conversion when mapping that ultimately
* returns the same result.
*/
static final class OptionalStreamMap<S, T> {
@BeforeTemplate
Optional<? extends T> before(Optional<S> optional, Function<? super S, ? extends T> function) {
return Refaster.anyOf(
optional.stream().map(function).findAny(), optional.stream().map(function).findFirst());
}

@AfterTemplate
Optional<? extends T> after(Optional<S> optional, Function<? super S, ? extends T> function) {
return optional.map(function);
}
}

// 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.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,16 @@ ImmutableSet<Optional<String>> testOptionalIdentity() {
Optional.of("baz").stream().min(String::compareTo),
Optional.of("qux").stream().max(String::compareTo));
}

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

ImmutableSet<Optional<String>> testOptionalStreamMap() {
return ImmutableSet.of(
Optional.of(1).stream().map(String::valueOf).findFirst(),
Optional.of(2).stream().map(String::valueOf).findAny());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,14 @@ ImmutableSet<Optional<String>> testOptionalIdentity() {
return ImmutableSet.of(
Optional.of("foo"), Optional.of("bar"), Optional.of("baz"), Optional.of("qux"));
}

ImmutableSet<Optional<String>> testOptionalStreamFilter() {
return ImmutableSet.of(
Optional.of("foo").filter(String::isEmpty), Optional.of("bar").filter(String::isEmpty));
}

ImmutableSet<Optional<String>> testOptionalStreamMap() {
return ImmutableSet.of(
Optional.of(1).map(String::valueOf), Optional.of(2).map(String::valueOf));
}
}

0 comments on commit d4c6743

Please sign in to comment.