Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 85 additions & 10 deletions src/test/java/lambda/part3/exercise/FilterMap.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package lambda.part3.exercise;

import data.Employee;
import data.JobHistoryEntry;
import data.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import static lambda.part3.exercise.Mapping.addOneYear;
import static lambda.part3.exercise.Mapping.changeToQa;
import static org.junit.Assert.assertEquals;

public class FilterMap {

public static class Container<T, R> {
Expand Down Expand Up @@ -32,31 +43,95 @@ public Function<T, R> getFunction() {

public static class LazyCollectionHelper<T> {
private final List<Container<Object, Object>> actions;
private final List<T> list;
private final List list;

public LazyCollectionHelper(List<T> list, List<Container<Object, Object>> actions) {
private LazyCollectionHelper(List list, List<Container<Object, Object>> actions) {
this.actions = actions;
this.list = list;
}

public LazyCollectionHelper(List<T> list) {
this(list, new ArrayList<>());
this( list, new ArrayList<>());
}

public LazyCollectionHelper<T> filter(Predicate<T> condition) {
List<Container<Object, Object>> newActions = new ArrayList<>(actions);
newActions.add(new Container<>((Predicate<Object>) condition));
return new LazyCollectionHelper<>(list, newActions);
actions.add(new Container<>((Predicate<Object>) condition));
return this;
}

public <R> LazyCollectionHelper<R> map(Function<T, R> function) {
// TODO
throw new UnsupportedOperationException();
actions.add(new Container<>((Function<Object, Object>) function));
return new LazyCollectionHelper<R>(list, actions);
}

public List<T> force() {
// TODO
throw new UnsupportedOperationException();
List<Object> result = new ArrayList<>(list);
for (Container<Object, Object> action : actions) {
List<Object> tmp = new ArrayList<>();
if (action.getFunction() != null)
result.forEach(element ->
tmp.add(action.getFunction().apply(element)));
else result.forEach(element -> {
if (action.getPredicate().test(element))
tmp.add(element);
});
result = tmp;
}
final ArrayList<T> ret = new ArrayList<>();
result.forEach(e -> ret.add((T) e));
return ret;
}
}

@Test
public void createEmployeesAndGetEpamEmployeesHistory() {
final List<Employee> employees =
Arrays.asList(
new Employee(
new Person("a", "Galt", 30),
Arrays.asList(
new JobHistoryEntry(2, "dev", "epam"),
new JobHistoryEntry(1, "dev", "google")
)),
new Employee(
new Person("b", "Doe", 40),
Arrays.asList(
new JobHistoryEntry(3, "qa", "yandex"),
new JobHistoryEntry(1, "qa", "epam"),
new JobHistoryEntry(1, "dev", "abc")
)),
new Employee(
new Person("c", "White", 50),
Collections.singletonList(
new JobHistoryEntry(5, "qa", "JoshkinaLolo")
))
);

final List<Employee> mappedEmployees =
new LazyCollectionHelper(employees)
.map(e -> ((Employee)e).withPerson(((Employee)e).getPerson().withFirstName("John")))
.map(e -> ((Employee)e).withJobHistory(addOneYear(((Employee)e).getJobHistory())))
.map(e -> ((Employee)e).withJobHistory(changeToQa(((Employee)e).getJobHistory())))
.filter(e -> ((Employee)e).getJobHistory().stream().anyMatch(j -> j.getEmployer().equals("epam")))
.force();

final List<Employee> expectedResult =
Arrays.asList(
new Employee(
new Person("John", "Galt", 30),
Arrays.asList(
new JobHistoryEntry(3, "dev", "epam"),
new JobHistoryEntry(2, "dev", "google")
)),
new Employee(
new Person("John", "Doe", 40),
Arrays.asList(
new JobHistoryEntry(4, "QA", "yandex"),
new JobHistoryEntry(2, "QA", "epam"),
new JobHistoryEntry(2, "dev", "abc")
))
);

assertEquals(mappedEmployees, expectedResult);
}
}
106 changes: 46 additions & 60 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +30,9 @@ public List<T> getList() {
// [T1, T2, T3] -> (T -> R) -> [R1, R2, R3]
public <R> MapHelper<R> map(Function<T, R> f) {
// TODO
throw new UnsupportedOperationException();
final ArrayList<R> result = new ArrayList<>();
this.list.forEach(e -> result.add(f.apply(e)));
return new MapHelper<>(result);
}

// [T] -> (T -> [R]) -> [R]
Expand All @@ -46,10 +45,22 @@ public <R> MapHelper<R> flatMap(Function<T, List<R>> f) {
f.apply(t).forEach(result::add)
);

return new MapHelper<R>(result);
return new MapHelper<>(result);
}
}

public static List<JobHistoryEntry> addOneYear(List<JobHistoryEntry> list){
return new MapHelper<>(list)
.map(e -> e.withDuration(e.getDuration() + 1) ).getList();
}

public static List<JobHistoryEntry> changeToQa(List<JobHistoryEntry> list) {
return new MapHelper<>(list)
.map(e -> e.getPosition().equals("qa") ?
e.withPosition("QA") : e)
.getList();
}

@Test
public void mapping() {
final List<Employee> employees =
Expand All @@ -76,12 +87,15 @@ 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(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
* */
.map(e -> e.withPerson(e.getPerson().withFirstName("John")))
.map(e -> e.withJobHistory(addOneYear(e.getJobHistory())))
.map(e -> e.withJobHistory(changeToQa(e.getJobHistory())))
.getList();

final List<Employee> expectedResult =
Arrays.asList(
Expand All @@ -105,68 +119,35 @@ public void mapping() {
))
);

assertEquals(mappedEmployees, expectedResult);
assertEquals(expectedResult, mappedEmployees);
}


private static class LazyMapHelper<T, R> {
private List<T> list;
private 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) {
return new LazyMapHelper<>(list, Function.identity());
}

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
if (function == null) throw new NullPointerException("Function is NULL");
return new MapHelper<>(list)
.map(function)
.getList();
}

public <R2> LazyMapHelper<T, R2> map(Function<R, R2> f) {
// TODO
throw new UnsupportedOperationException();
}

}

private static class LazyFlatMapHelper<T, R> {

public LazyFlatMapHelper(List<T> list, Function<T, List<R>> function) {
return new LazyMapHelper<>(list, f.compose(function));
}

public static <T> LazyFlatMapHelper<T, T> from(List<T> list) {
throw new UnsupportedOperationException();
}

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
}

// TODO filter
// (T -> boolean) -> (T -> [T])
// filter: [T1, T2] -> (T -> boolean) -> [T2]
// flatMap": [T1, T2] -> (T -> [T]) -> [T2]

public <R2> LazyFlatMapHelper<T, R2> map(Function<R, R2> f) {
final Function<R, List<R2>> listFunction = rR2TorListR2(f);
return flatMap(listFunction);
}

// (R -> R2) -> (R -> [R2])
private <R2> Function<R, List<R2>> rR2TorListR2(Function<R, R2> f) {
throw new UnsupportedOperationException();
}

// TODO *
public <R2> LazyFlatMapHelper<T, R2> flatMap(Function<R, List<R2>> f) {
throw new UnsupportedOperationException();
}
}



@Test
public void lazy_mapping() {
final List<Employee> employees =
Expand All @@ -193,12 +174,15 @@ 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(TODO) // change name to John
.map(TODO) // add 1 year to experience duration
.map(TODO) // replace qa with QA
* */
.map(e -> e.withPerson(e.getPerson().withFirstName("John")))
.map(e -> e.withJobHistory(addOneYear(e.getJobHistory())))
.map(e -> e.withJobHistory(changeToQa(e.getJobHistory())))
.force();

final List<Employee> expectedResult =
Arrays.asList(
Expand All @@ -224,4 +208,6 @@ public void lazy_mapping() {

assertEquals(mappedEmployees, expectedResult);
}


}
Loading