From cb2e17d383cfe9498f54c0b57b2ee06885ff436c Mon Sep 17 00:00:00 2001 From: Philipp Zeipert Date: Tue, 17 Dec 2024 11:35:21 +0100 Subject: [PATCH] Introduce `StreamMapToValuesFromMap` Refaster rule --- .../picnic/errorprone/refasterrules/StreamRules.java | 12 ++++++++++++ .../refasterrules/StreamRulesTestInput.java | 5 +++++ .../refasterrules/StreamRulesTestOutput.java | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java index e7bea741423..adfa70d5044 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java @@ -297,6 +297,18 @@ boolean after(Stream stream) { } } + static final class StreamMapToValuesFromMap { + @BeforeTemplate + Stream before(Stream stream, Map map) { + return stream.filter(map::containsKey).map(map::get); + } + + @AfterTemplate + Stream after(Stream stream, Map map) { + return stream.map(map::get).filter(Objects::nonNull); + } + } + static final class StreamMin { @BeforeTemplate @SuppressWarnings("java:S4266" /* This violation will be rewritten. */) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java index 5126726c3bf..3790e028bde 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java @@ -141,6 +141,11 @@ boolean testStreamFindAnyIsPresent() { return Stream.of(1).findFirst().isPresent(); } + Stream testStreamMapToValuesFromMap() { + ImmutableMap map = ImmutableMap.of("foo", "bar"); + return Stream.of("foo").filter(map::containsKey).map(map::get); + } + ImmutableSet> testStreamMin() { return ImmutableSet.of( Stream.of("foo").max(comparingInt(String::length).reversed()), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java index a11b7ca644c..10b0050c54f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java @@ -141,6 +141,11 @@ boolean testStreamFindAnyIsPresent() { return Stream.of(1).findAny().isPresent(); } + Stream testStreamMapToValuesFromMap() { + ImmutableMap map = ImmutableMap.of("foo", "bar"); + return Stream.of("foo").map(map::get).filter(Objects::nonNull); + } + ImmutableSet> testStreamMin() { return ImmutableSet.of( Stream.of("foo").min(comparingInt(String::length)),