From 86f2ae1456c9cf7cac20b2ecb627398719535270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD=20=D0=9D=D0=B0=D0=B7=D0=B0?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= Date: Tue, 4 Jul 2017 18:07:54 +0300 Subject: [PATCH] part-2 done --- .../part2/exercise/ArrowNotationExercise.java | 23 ++++++++------ .../exercise/FunctionCombinationExercise.java | 31 ++++++------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index c43edc4..9446ea6 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -1,6 +1,7 @@ package lambda.part2.exercise; import data.Person; +import lambda.part2.example.FunctionCombination; import org.junit.Test; import java.util.function.BiFunction; @@ -14,35 +15,37 @@ public class ArrowNotationExercise { @Test public void getAge() { // Person -> Integer - final Function getAge = null; // TODO + final Function getAge = p -> p.getAge(); assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); } @Test public void compareAges() { - // TODO use BiPredicate // compareAges: (Person, Person) -> boolean + final BiFunction compareAges = (o1, o2) -> o1.getAge() == o2.getAge(); - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + assertEquals(true, compareAges.apply(new Person("a", "b", 22), new Person("c", "d", 22))); } - // TODO // getFullName: Person -> String + private Function getFullName() { + return p -> p.getLastName() + " " + p.getFirstName(); + } - // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // + private BiFunction ageOfPersonWithTheLongestFullName(Function func) { + return (o1, o2) -> func.apply(o1).length() > func.apply(o2).length() ? o1.getAge() : o2.getAge(); + } @Test public void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = null; // TODO + final Function getFullName = this.getFullName(); // (Person, Person) -> Integer - // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = null; + final BiFunction ageOfPersonWithTheLongestFullName + = this.ageOfPersonWithTheLongestFullName(getFullName); assertEquals( Integer.valueOf(1), diff --git a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java index ce7080f..8442f01 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -22,19 +22,12 @@ public void personHasNotEmptyLastNameAndFirstName0() { // TODO // negate1: (Person -> boolean) -> (Person -> boolean) private Predicate negate1(Predicate test) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + return p -> !test.test(p); } - // TODO // validateFirstNameAndLastName: (Person -> boolean, Person -> boolean) -> (Person -> boolean) private Predicate validateFirstNameAndLastName(Predicate t1, Predicate t2) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + return p -> t1.test(p) && t2.test(p); } @Test @@ -52,18 +45,14 @@ public void personHasNotEmptyLastNameAndFirstName1() { assertEquals(false, validate.test(new Person("a", "", 0))); } - // TODO // negate: (T -> boolean) -> (T -> boolean) private Predicate negate(Predicate test) { - // TODO - throw new UnsupportedOperationException(); + return p -> !test.test(p); } - // TODO // and: (T -> boolean, T -> boolean) -> (T -> boolean) private Predicate and(Predicate t1, Predicate t2) { - // TODO - throw new UnsupportedOperationException(); + return p -> t1.test(p) && t2.test(p); } @Test @@ -71,10 +60,10 @@ public void personHasNotEmptyLastNameAndFirstName2() { final Predicate hasEmptyFirstName = p -> p.getFirstName().isEmpty(); final Predicate hasEmptyLastName = p -> p.getLastName().isEmpty(); - final Predicate validateFirstName = null; // TODO use negate - final Predicate validateLastName = null; // TODO use negate + final Predicate validateFirstName = this.negate(hasEmptyFirstName); + final Predicate validateLastName = this.negate(hasEmptyLastName); - final Predicate validate = null; // TODO use and + final Predicate validate = this.validateFirstNameAndLastName(validateFirstName, validateLastName); assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0))); @@ -86,10 +75,10 @@ public void personHasNotEmptyLastNameAndFirstName3() { final Predicate hasEmptyFirstName = p -> p.getFirstName().isEmpty(); final Predicate hasEmptyLastName = p -> p.getLastName().isEmpty(); - final Predicate validateFirstName = null; // TODO use Predicate::negate - final Predicate validateLastName = null; // TODO use Predicate::negate + final Predicate validateFirstName = hasEmptyFirstName.negate(); + final Predicate validateLastName = hasEmptyLastName.negate(); - final Predicate validate = null; // TODO use Predicate::and + final Predicate validate = validateFirstName.and(validateLastName); assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0)));