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
28 changes: 21 additions & 7 deletions src/test/java/lambda/part2/exercise/ArrowNotationExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,49 @@ public class ArrowNotationExercise {
@Test
public void getAge() {
// Person -> Integer
final Function<Person, Integer> getAge = null; // TODO
final Function<Person, Integer> getAge = Person::getAge; // TODO

assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33)));
}

private static boolean sameAges(Person p1, Person p2) {
return p1.getAge() == p2.getAge();
}

@Test
public void compareAges() {
// TODO use BiPredicate
// compareAges: (Person, Person) -> boolean
final BiPredicate<Person, Person> compareAges = ArrowNotationExercise::sameAges;


throw new UnsupportedOperationException("Not implemented");
//assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
//throw new UnsupportedOperationException("Not implemented");
assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
}

// TODO
// getFullName: Person -> String

private String getFullName(Person p) {
return p.getFirstName() + " " + p.getLastName();
}
// TODO
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int
// ageOfPersonWithTheLongestFullName: (Person -> String) => ((Person, Person) -> int)
//
private BiFunction<Person, Person, Integer> ageOfPersonWithLongestFullName(Function<Person, String> fullName) {
return (Person p1, Person p2) -> fullName.apply(p1).length() > fullName.apply(p2).length()
? p1.getAge()
: p2.getAge();
}

@Test
public void getAgeOfPersonWithTheLongestFullName() {
// Person -> String
final Function<Person, String> getFullName = null; // TODO
final Function<Person, String> getFullName = this::getFullName; // TODO

// (Person, Person) -> Integer
// TODO use ageOfPersonWithTheLongestFullName(getFullName)
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName = null;
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName =
ageOfPersonWithLongestFullName(getFullName);

assertEquals(
Integer.valueOf(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ public void personHasNotEmptyLastNameAndFirstName0() {
assertEquals(false, validate.test(new Person("a", "", 0)));
}

// TODO
// negate1: (Person -> boolean) -> (Person -> boolean)
private Predicate<Person> negate1(Predicate<Person> test) {
return p -> {
// TODO
throw new UnsupportedOperationException();
};
return p -> !test.test(p);
}

// TODO
// validateFirstNameAndLastName: (Person -> boolean, Person -> boolean) -> (Person -> boolean)
private Predicate<Person> validateFirstNameAndLastName(Predicate<Person> t1, Predicate<Person> t2) {
return p -> {
// TODO
throw new UnsupportedOperationException();
};
return p -> t1.test(p) && t2.test(p);
}

@Test
Expand All @@ -52,29 +44,25 @@ public void personHasNotEmptyLastNameAndFirstName1() {
assertEquals(false, validate.test(new Person("a", "", 0)));
}

// TODO
// negate: (T -> boolean) -> (T -> boolean)
private <T> Predicate<T> negate(Predicate<T> test) {
// TODO
throw new UnsupportedOperationException();
return T -> !test.test(T);
}

// TODO
// and: (T -> boolean, T -> boolean) -> (T -> boolean)
private <T> Predicate<T> and(Predicate<T> t1, Predicate<T> t2) {
// TODO
throw new UnsupportedOperationException();
return T -> t1.test(T) && (t2.test(T));
}

@Test
public void personHasNotEmptyLastNameAndFirstName2() {
final Predicate<Person> hasEmptyFirstName = p -> p.getFirstName().isEmpty();
final Predicate<Person> hasEmptyLastName = p -> p.getLastName().isEmpty();

final Predicate<Person> validateFirstName = null; // TODO use negate
final Predicate<Person> validateLastName = null; // TODO use negate
final Predicate<Person> validateFirstName = negate(hasEmptyFirstName); // TODO use negate
final Predicate<Person> validateLastName = negate(hasEmptyLastName); // TODO use negate

final Predicate<Person> validate = null; // TODO use and
final Predicate<Person> validate = validateFirstName.and(validateLastName); // TODO use and

assertEquals(true, validate.test(new Person("a", "b", 0)));
assertEquals(false, validate.test(new Person("", "b", 0)));
Expand All @@ -86,10 +74,10 @@ public void personHasNotEmptyLastNameAndFirstName3() {
final Predicate<Person> hasEmptyFirstName = p -> p.getFirstName().isEmpty();
final Predicate<Person> hasEmptyLastName = p -> p.getLastName().isEmpty();

final Predicate<Person> validateFirstName = null; // TODO use Predicate::negate
final Predicate<Person> validateLastName = null; // TODO use Predicate::negate
final Predicate<Person> validateFirstName = hasEmptyFirstName.negate(); // TODO use Predicate::negate
final Predicate<Person> validateLastName = hasEmptyLastName.negate(); // TODO use Predicate::negate

final Predicate<Person> validate = null; // TODO use Predicate::and
final Predicate<Person> validate = validateFirstName.and(validateLastName); // TODO use Predicate::and

assertEquals(true, validate.test(new Person("a", "b", 0)));
assertEquals(false, validate.test(new Person("", "b", 0)));
Expand Down