diff --git a/src/main/java/data/Generator.java b/src/main/java/data/Generator.java index cbd46ea..2584f86 100644 --- a/src/main/java/data/Generator.java +++ b/src/main/java/data/Generator.java @@ -60,7 +60,8 @@ public static Employee generateEmployee() { } public static List generateEmployeeList() { - // TODO - throw new UnsupportedOperationException(); + return Stream.generate(Generator::generateEmployee) + .limit(15) + .collect(toList()); } } diff --git a/src/test/java/part3/exercise/CollectorCombination.java b/src/test/java/part3/exercise/CollectorCombination.java index 15a42d5..2d5b8d6 100755 --- a/src/test/java/part3/exercise/CollectorCombination.java +++ b/src/test/java/part3/exercise/CollectorCombination.java @@ -5,12 +5,18 @@ import part2.exercise.CollectorsExercise2.Key; import part2.exercise.CollectorsExercise2.Value; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collector; import static java.util.stream.Collectors.*; +import static org.junit.Assert.assertEquals; public class CollectorCombination { @@ -30,12 +36,66 @@ public A getA() { public B getB() { return b; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Pair pair = (Pair) o; + + if (!a.equals(pair.a)) return false; + return b.equals(pair.b); + } + + @Override + public int hashCode() { + int result = a.hashCode(); + result = 31 * result + b.hashCode(); + return result; + } } private static Collector, Pair> paired(Collector c1, Collector c2) { - // TODO - throw new UnsupportedOperationException(); + return new Collector, Pair>() { + @Override + public Supplier> supplier() { + return () -> new Pair<>( + c1.supplier().get(), + c2.supplier().get() + ); + } + + @Override + public BiConsumer, T> accumulator() { + return (m1M2Pair, t) -> { + c1.accumulator().accept(m1M2Pair.getA(), t); + c2.accumulator().accept(m1M2Pair.getB(), t); + }; + } + + @Override + public BinaryOperator> combiner() { + return (m1M2Pair1, m1M2Pair2) -> new Pair<>( + c1.combiner().apply(m1M2Pair1.getA(), m1M2Pair2.getA()), + c2.combiner().apply(m1M2Pair1.getB(), m1M2Pair2.getB()) + ); + } + + @Override + public Function, Pair> finisher() { + return m1M2Pair -> new Pair<>( + c1.finisher().apply(m1M2Pair.getA()), + c2.finisher().apply(m1M2Pair.getB()) + ); + } + + @Override + public Set characteristics() { + return new HashSet<>(); + } + }; } @Test @@ -48,17 +108,27 @@ public void collectKeyValueMap() { // Перепишите решение в слещующем виде: final List pairs = CollectorsExercise2.generatePairs(10, 100); - final Pair, Map>> res2 = pairs.stream() - .collect( - paired( - mapping(CollectorsExercise2.Pair::getKey, toMap(Key::getId, Function.identity(), (x, y) -> x)), - mapping(CollectorsExercise2.Pair::getValue, groupingBy(Value::getKeyId)) - ) - ); + final Pair, Map>> res1; + Map collect = pairs.stream().collect(mapping(CollectorsExercise2.Pair::getKey, toMap(Key::getId, + Function.identity(), + (x, y) -> x)) + ); + + Map> collect1 = pairs.stream().collect(mapping(CollectorsExercise2.Pair::getValue, + groupingBy(Value::getKeyId))); + + res1 = new Pair<>(collect, collect1); + + final Pair, Map>> res2 = pairs.stream() + .collect(paired(mapping(CollectorsExercise2.Pair::getKey, + toMap(Key::getId, + Function.identity(), + (x, y) -> x)), + mapping(CollectorsExercise2.Pair::getValue, + groupingBy(Value::getKeyId)))); - // TODO tests - throw new UnsupportedOperationException(); + assertEquals(res1, res2); } } diff --git a/src/test/java/part3/exercise/lambda/LambdaExercise.java b/src/test/java/part3/exercise/lambda/LambdaExercise.java index 752e1f2..1091c27 100755 --- a/src/test/java/part3/exercise/lambda/LambdaExercise.java +++ b/src/test/java/part3/exercise/lambda/LambdaExercise.java @@ -15,21 +15,27 @@ public class LambdaExercise { public void supply() { final Person person = new Person("John", "Galt", 30); - final Supplier getPerson = null; // TODO return person from Supplier + final Supplier getPerson = () -> person; assertEquals(person, getPerson.get()); } @Test public void function() { - final Function getPersonName1 = null; // TODO get the name of person using expression lambda + final Person person = new Person("John", "Galt", 30); - final Function getPersonName2 = null; // TODO get the name of person using method reference + final Function getPersonName1 = + p -> person.getFirstName(); - // TODO get the name of person and log it to System.out using statement lambda: {} - final Function getPersonNameAndLogIt = null; + final Function getPersonName2 = + Person::getFirstName; + + final Function getPersonNameAndLogIt = p -> { + final String firstName = p.getFirstName(); + System.out.println(firstName); + return firstName; + }; - final Person person = new Person("John", "Galt", 30); assertEquals(person.getFirstName(), getPersonName1.apply(person)); assertEquals(person.getFirstName(), getPersonName2.apply(person)); @@ -38,21 +44,20 @@ public void function() { @Test public void combineFunctions() { - final Function getPersonName = null; // TODO get the name of person + final Person person = new Person("John", "Galt", 30); + + final Function getPersonName = Person::getFirstName; assertEquals("John", getPersonName.apply(new Person("John", "Galt", 30))); - final Function getStringLength = null; // TODO get string length + final Function getStringLength = String::length; assertEquals(Integer.valueOf(3), getStringLength.apply("ABC")); - // TODO get person name length using getPersonName and getStringLength without andThen - final Function getPersonNameLength1 = null; + final Function getPersonNameLength1 = p -> getStringLength.apply(getPersonName.apply(p)); - // TODO get person name length using getPersonName and getStringLength with andThen - final Function getPersonNameLength2 = null; + final Function getPersonNameLength2 = getPersonName.andThen(getStringLength); - final Person person = new Person("John", "Galt", 30); assertEquals(Integer.valueOf(4), getPersonNameLength1.apply(person)); assertEquals(Integer.valueOf(4), getPersonNameLength2.apply(person)); @@ -68,26 +73,24 @@ private Person createPerson(PersonFactory pf) { // ((T -> R), (R -> boolean)) -> (T -> boolean) private Predicate combine(Function f, Predicate p) { - // TODO - throw new UnsupportedOperationException(); + return t -> p.test(f.apply(t)); } @Test public void methodReference() { - // TODO use only method reverences here. - final Person person = createPerson(null); // TODO + final Person person = createPerson(Person::new); assertEquals(new Person("John", "Galt", 66), person); - final Function getPersonName = null; // TODO + final Function getPersonName = Person::getFirstName; assertEquals("John", getPersonName.apply(person)); - final Predicate isJohnString = null; // TODO using method reference check that "John" equals string parameter + final Predicate isJohnString = "John"::equals; final Predicate isJohnPerson = combine(getPersonName, isJohnString); assertEquals(true, isJohnPerson.test(person)); } -} +} \ No newline at end of file diff --git a/src/test/java/part3/exercise/stream/StreamsExercise.java b/src/test/java/part3/exercise/stream/StreamsExercise.java index 4e2d54b..2b99848 100755 --- a/src/test/java/part3/exercise/stream/StreamsExercise.java +++ b/src/test/java/part3/exercise/stream/StreamsExercise.java @@ -8,9 +8,15 @@ import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import static java.util.stream.Collectors.*; import static org.junit.Assert.assertEquals; public class StreamsExercise { @@ -19,7 +25,8 @@ public class StreamsExercise { public void getAllJobHistoryEntries() { final List employees = getEmployees(); - final List jobHistoryEntries = null; // TODO + final List jobHistoryEntries = + employees.stream().flatMap(j -> j.getJobHistory().stream()).collect(toList()); assertEquals(22, jobHistoryEntries.size()); } @@ -29,7 +36,10 @@ public void getSumDuration() { // sum all durations for all persons final List employees = getEmployees(); - final int sumDurations = 0; // TODO + final int sumDurations = employees.stream() + .flatMap(j -> j.getJobHistory().stream()) + .mapToInt(JobHistoryEntry::getDuration) + .sum(); assertEquals(72, sumDurations); } @@ -64,7 +74,10 @@ public String toString() { public void indexPersonsByEmployer1() { final List employees = getEmployees(); - final Map> index = null; // TODO + final Map> index = employees.stream() + .flatMap(e -> e.getJobHistory().stream() + .map(j -> new PersonEmployer(e.getPerson(), j.getEmployer()))) + .collect(groupingBy(PersonEmployer::getEmployer)); assertEquals(11, index.get("epam").size()); } @@ -73,7 +86,9 @@ public void indexPersonsByEmployer1() { public void indexPersonsByEmployer2() { final List employees = getEmployees(); - final Map> index = null; // TODO + final Map> index = employees.stream() + .flatMap(e -> e.getJobHistory().stream() + .map(j -> new PersonEmployer(e.getPerson(), j.getEmployer()))).collect(groupingBy(PersonEmployer::getEmployer, mapping(PersonEmployer::getPerson, toList()))); assertEquals(11, index.get("epam").size()); } @@ -106,7 +121,10 @@ public String toString() { private PersonDuration sumAllPersonDurations(Employee e) { // TODO - throw new UnsupportedOperationException(); + final int totalDuration = e.getJobHistory().stream() + .mapToInt(JobHistoryEntry::getDuration) + .sum(); + return new PersonDuration(e.getPerson(), totalDuration); } @Test @@ -114,7 +132,9 @@ public void getSumPersonDuration() { // sum all durations for each person final List employees = getEmployees(); - final Map personDuration = null; // TODO use sumAllPersonDurations + final Map personDuration = employees.stream() + .map(this::sumAllPersonDurations) + .collect(toMap(PersonDuration::getPerson, PersonDuration::getDuration)); assertEquals(Integer.valueOf(8), personDuration.get(new Person("John", "Doe", 24))); } @@ -139,14 +159,19 @@ public Map getDurationByPositionIndex() { private static PersonPositionIndex getPersonPositionIndex(Employee e) { // TODO - throw new UnsupportedOperationException(); + final Map collect = e.getJobHistory().stream() + .collect(groupingBy(JobHistoryEntry::getPosition, summingInt(JobHistoryEntry::getDuration))); + return new PersonPositionIndex(e.getPerson(), collect); } @Test public void getSumDurationsForPersonByPosition() { final List employees = getEmployees(); - final List personIndexes = null; // TODO use getPersonPositionIndex + final List personIndexes = + employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .collect(toList()); assertEquals(1, personIndexes.get(3).getDurationByPositionIndex().size()); } @@ -175,12 +200,20 @@ public int getDuration() { } } + private Stream getPersonPositionDurationStream(List employees) { + return employees.stream() + .map(StreamsExercise::getPersonPositionIndex) + .flatMap(e -> e.getDurationByPositionIndex().entrySet().stream() + .map(p -> new PersonPositionDuration(e.person, p.getKey(), p.getValue())) + ); + } + @Test public void getDurationsForEachPersonByPosition() { final List employees = getEmployees(); - final List personPositionDurations = null; // TODO - + final List personPositionDurations = + getPersonPositionDurationStream(employees).collect(toList()); assertEquals(17, personPositionDurations.size()); } @@ -190,8 +223,11 @@ public void getCoolestPersonByPosition1() { // Get person with max duration on given position final List employees = getEmployees(); - final Map coolestPersonByPosition = null;// TODO - + final Map coolestPersonByPosition = + getPersonPositionDurationStream(employees) + .collect(toMap(PersonPositionDuration::getPosition, + Function.identity(), + BinaryOperator.maxBy(Comparator.comparingInt(PersonPositionDuration::getDuration)))); assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA").getPerson()); } @@ -201,8 +237,11 @@ public void getCoolestPersonByPosition2() { // Get person with max duration on given position final List employees = getEmployees(); - final Map coolestPersonByPosition = null; // TODO - + final Map coolestPersonByPosition = + getPersonPositionDurationStream(employees) + .collect(groupingBy(PersonPositionDuration::getPosition, + collectingAndThen(maxBy(Comparator.comparingInt(PersonPositionDuration::getDuration)), + p -> p.get().getPerson()))); assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA")); } @@ -282,4 +321,4 @@ private List getEmployees() { } -} +} \ No newline at end of file