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<T> getList() { // [T] -> (T -> R) -> [R] // [T1, T2, T3] -> (T -> R) -> [R1, R2, R3] public <R> MapHelper<R> map(Function<T, R> f) { - // TODO - throw new UnsupportedOperationException(); + final List<R> 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<Employee> 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<Employee> expectedResult = Arrays.asList( @@ -108,10 +106,27 @@ public void mapping() { assertEquals(mappedEmployees, expectedResult); } + private List<JobHistoryEntry> addOneYear(List<JobHistoryEntry> jobHistoryEntries) { + MapHelper<JobHistoryEntry> mapHelper = new MapHelper<>(jobHistoryEntries); + return mapHelper + .map(entry -> entry.withDuration(entry.getDuration() + 1)) + .getList(); + } + + private List<JobHistoryEntry> replaceQaToUpperCase(List<JobHistoryEntry> jobHistoryEntries) { + MapHelper<JobHistoryEntry> mapHelper = new MapHelper<>(jobHistoryEntries); + return mapHelper + .map(entry -> entry.getPosition().equals("qa") ? entry.withPosition("QA") : entry) + .getList(); + } private static class LazyMapHelper<T, R> { + private final List<T> list; + private final Function<T,R> function; public LazyMapHelper(List<T> list, Function<T, R> function) { + this.list = list; + this.function = function; } public static <T> LazyMapHelper<T, T> from(List<T> list) { @@ -119,13 +134,16 @@ public static <T> LazyMapHelper<T, T> from(List<T> list) { } public List<R> force() { - // TODO - throw new UnsupportedOperationException(); + final List<R> result = new ArrayList<>(); + for (T element : list) { + result.add(function.apply(element)); + } + return result; } public <R2> LazyMapHelper<T, R2> map(Function<R, R2> f) { - // TODO - throw new UnsupportedOperationException(); + Function<T, R2> newFunction = function.andThen(f); + return new LazyMapHelper<>(list, newFunction); } } @@ -165,8 +183,6 @@ public <R2> LazyFlatMapHelper<T, R2> flatMap(Function<R, List<R2>> f) { } } - - @Test public void lazy_mapping() { final List<Employee> employees = @@ -193,12 +209,10 @@ public void lazy_mapping() { final List<Employee> 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<Employee> expectedResult = Arrays.asList(