diff --git a/src/test/java/lambda/part3/exercise/Mapping.java b/src/test/java/lambda/part3/exercise/Mapping.java index c0a814a..2c054cc 100644 --- a/src/test/java/lambda/part3/exercise/Mapping.java +++ b/src/test/java/lambda/part3/exercise/Mapping.java @@ -9,10 +9,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.function.BiConsumer; -import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.Predicate; import static org.junit.Assert.assertEquals; @@ -32,8 +29,11 @@ public List getList() { // [T] -> (T -> R) -> [R] // [T1, T2, T3] -> (T -> R) -> [R1, R2, R3] public MapHelper map(Function f) { - // TODO - throw new UnsupportedOperationException(); + final List newList = new ArrayList<>(); + for (T element : list) { + newList.add(f.apply(element)); + } + return new MapHelper<>(newList); } // [T] -> (T -> [R]) -> [R] @@ -76,12 +76,10 @@ public void mapping() { final List mappedEmployees = new MapHelper<>(employees) - /* - .map(TODO) // change name to John .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) - .map(TODO) // add 1 year to experience duration .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) - .map(TODO) // replace qa with QA - * */ - .getList(); + .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) + .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) + .map(e -> e.withJobHistory(replaceQaToUpperCase(e.getJobHistory()))) // replace qa with QA + .getList(); final List expectedResult = Arrays.asList( @@ -108,10 +106,27 @@ public void mapping() { assertEquals(mappedEmployees, expectedResult); } + private List addOneYear(List jobHistoryEntries) { + MapHelper mapHelper = new MapHelper<>(jobHistoryEntries); + return mapHelper + .map(entry -> entry.withDuration(entry.getDuration() + 1)) + .getList(); + } + + private List replaceQaToUpperCase(List jobHistoryEntries) { + MapHelper mapHelper = new MapHelper<>(jobHistoryEntries); + return mapHelper + .map(entry -> entry.getPosition().equals("qa") ? entry.withPosition("QA") : entry) + .getList(); + } private static class LazyMapHelper { + private final List list; + private final Function function; public LazyMapHelper(List list, Function function) { + this.list = list; + this.function = function; } public static LazyMapHelper from(List list) { @@ -119,13 +134,16 @@ public static LazyMapHelper from(List list) { } public List force() { - // TODO - throw new UnsupportedOperationException(); + final List result = new ArrayList<>(); + for (T element : list) { + result.add(function.apply(element)); + } + return result; } public LazyMapHelper map(Function f) { - // TODO - throw new UnsupportedOperationException(); + Function newFunction = function.andThen(f); + return new LazyMapHelper<>(list, newFunction); } } @@ -165,8 +183,6 @@ public LazyFlatMapHelper flatMap(Function> f) { } } - - @Test public void lazy_mapping() { final List employees = @@ -193,12 +209,10 @@ public void lazy_mapping() { final List mappedEmployees = LazyMapHelper.from(employees) - /* - .map(TODO) // change name to John - .map(TODO) // add 1 year to experience duration - .map(TODO) // replace qa with QA - * */ - .force(); + .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) + .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) + .map(e -> e.withJobHistory(replaceQaToUpperCase(e.getJobHistory()))) + .force(); final List expectedResult = Arrays.asList(