Skip to content

Commit

Permalink
Introduce StreamMapToValuesFromMap Refaster rule
Browse files Browse the repository at this point in the history
  • Loading branch information
zphilipp committed Dec 18, 2024
1 parent 67bf5b9 commit cb2e17d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ boolean after(Stream<T> stream) {
}
}

static final class StreamMapToValuesFromMap<K, V> {
@BeforeTemplate
Stream<V> before(Stream<K> stream, Map<K, V> map) {
return stream.filter(map::containsKey).map(map::get);
}

@AfterTemplate
Stream<V> after(Stream<K> stream, Map<K, V> map) {
return stream.map(map::get).filter(Objects::nonNull);
}
}

static final class StreamMin<T> {
@BeforeTemplate
@SuppressWarnings("java:S4266" /* This violation will be rewritten. */)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ boolean testStreamFindAnyIsPresent() {
return Stream.of(1).findFirst().isPresent();
}

Stream<String> testStreamMapToValuesFromMap() {
ImmutableMap<String, String> map = ImmutableMap.of("foo", "bar");
return Stream.of("foo").filter(map::containsKey).map(map::get);
}

ImmutableSet<Optional<String>> testStreamMin() {
return ImmutableSet.of(
Stream.of("foo").max(comparingInt(String::length).reversed()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ boolean testStreamFindAnyIsPresent() {
return Stream.of(1).findAny().isPresent();
}

Stream<String> testStreamMapToValuesFromMap() {
ImmutableMap<String, String> map = ImmutableMap.of("foo", "bar");
return Stream.of("foo").map(map::get).filter(Objects::nonNull);
}

ImmutableSet<Optional<String>> testStreamMin() {
return ImmutableSet.of(
Stream.of("foo").min(comparingInt(String::length)),
Expand Down

0 comments on commit cb2e17d

Please sign in to comment.