diff --git a/refactoring/src/main/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/Author.java b/refactoring/src/main/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/Author.java new file mode 100644 index 00000000..a0c54335 --- /dev/null +++ b/refactoring/src/main/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/Author.java @@ -0,0 +1,25 @@ +package com.example.refactoring._13_loops._33_replace_loop_with_pipeline; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Author { + + private String company; + + private String twitterHandle; + + public Author(String company, String twitterHandle) { + this.company = company; + this.twitterHandle = twitterHandle; + } + + static public List TwitterHandles(List authors, String company) { + return authors.stream() + .filter(author -> author.company.equals(company) && Objects.nonNull(author.twitterHandle)) + .map(author -> author.twitterHandle) + .toList(); + } + +} diff --git a/refactoring/src/main/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/_before/Author.java b/refactoring/src/main/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/_before/Author.java new file mode 100644 index 00000000..5bb3baf6 --- /dev/null +++ b/refactoring/src/main/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/_before/Author.java @@ -0,0 +1,29 @@ +package com.example.refactoring._13_loops._33_replace_loop_with_pipeline._before; + +import java.util.ArrayList; +import java.util.List; + +public class Author { + + private String company; + + private String twitterHandle; + + public Author(String company, String twitterHandle) { + this.company = company; + this.twitterHandle = twitterHandle; + } + + static public List TwitterHandles(List authors, String company) { + var result = new ArrayList (); + for (Author a : authors) { + if (a.company.equals(company)) { + var handle = a.twitterHandle; + if (handle != null) + result.add(handle); + } + } + return result; + } + +} diff --git a/refactoring/src/test/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/AuthorTest.java b/refactoring/src/test/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/AuthorTest.java new file mode 100644 index 00000000..b3101294 --- /dev/null +++ b/refactoring/src/test/java/com/example/refactoring/_13_loops/_33_replace_loop_with_pipeline/AuthorTest.java @@ -0,0 +1,19 @@ +package com.example.refactoring._13_loops._33_replace_loop_with_pipeline; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AuthorTest { + + @Test + void twitterHandler() { + Author kim = new Author("ms", null); + Author lee = new Author("naver", "lee"); + assertEquals(List.of("lee"), Author.TwitterHandles(List.of(kim, lee), "naver")); + } + + +} \ No newline at end of file