diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index c43edc4..9ae8280 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -1,53 +1,55 @@ -package lambda.part2.exercise; - -import data.Person; -import org.junit.Test; - -import java.util.function.BiFunction; -import java.util.function.BiPredicate; -import java.util.function.Function; - -import static org.junit.Assert.assertEquals; - -public class ArrowNotationExercise { - - @Test - public void getAge() { - // Person -> Integer - final Function getAge = null; // TODO - - assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); - } - - @Test - public void compareAges() { - // TODO use BiPredicate - // compareAges: (Person, Person) -> boolean - - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); - } - - // TODO - // getFullName: Person -> String - - // TODO - // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // - - @Test - public void getAgeOfPersonWithTheLongestFullName() { - // Person -> String - final Function getFullName = null; // TODO - - // (Person, Person) -> Integer - // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = null; - - assertEquals( - Integer.valueOf(1), - ageOfPersonWithTheLongestFullName.apply( - new Person("a", "b", 2), - new Person("aa", "b", 1))); - } -} +package lambda.part2.exercise; + +import data.Person; +import org.junit.Test; + +import java.util.function.BiPredicate; +import java.util.function.Function; +import java.util.function.ToIntBiFunction; + +import static org.junit.Assert.assertEquals; + +public class ArrowNotationExercise { + + @Test + public void getAge() { + // Person -> Integer + final Function getAge = Person::getAge; + + assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); + } + + @Test + public void compareAges() { + // compareAges: (Person, Person) -> boolean + BiPredicate compareAges = (person, person2) -> person.getAge() == person2.getAge(); + + assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + } + + // getFullName: Person -> String + public String getFullName(Person p) { + return String.format("%s %s", p.getFirstName(), p.getLastName()); + } + + // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int + public ToIntBiFunction ageOfPersonWithTheLongestFullName(Function fullName) { + return (p1, p2) -> fullName.apply(p1).length() > fullName.apply(p2).length() ? p1.getAge() : p2.getAge(); + } + + @Test + public void getAgeOfPersonWithTheLongestFullName() { + // Person -> String + final Function getFullName = this::getFullName; + + // (Person, Person) -> Integer + final ToIntBiFunction ageOfPersonWithTheLongestFullName = + ageOfPersonWithTheLongestFullName(getFullName); + + assertEquals( + 1, + ageOfPersonWithTheLongestFullName.applyAsInt( + new Person("a", "b", 2), + new Person("aa", "b", 1))); + } +} diff --git a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java index ce7080f..e79596f 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -19,22 +19,14 @@ public void personHasNotEmptyLastNameAndFirstName0() { assertEquals(false, validate.test(new Person("a", "", 0))); } - // TODO // negate1: (Person -> boolean) -> (Person -> boolean) - private Predicate negate1(Predicate test) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + private Predicate negate1(Predicate predicate) { + return p -> !predicate.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 +44,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(); + private Predicate negate(Predicate predicate) { + return t -> !predicate.test(t); } - // TODO // and: (T -> boolean, T -> boolean) -> (T -> boolean) private Predicate and(Predicate t1, Predicate t2) { - // TODO - throw new UnsupportedOperationException(); + return t -> t1.test(t) && t2.test(t); } @Test @@ -71,10 +59,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 = negate(hasEmptyFirstName); + final Predicate validateLastName = negate(hasEmptyLastName); - final Predicate validate = null; // TODO use and + final Predicate validate = and(validateFirstName, validateLastName); assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0))); @@ -86,10 +74,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 = and(validateFirstName, validateLastName); assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0)));