-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathArrowNotationExercise.java
More file actions
62 lines (49 loc) · 2.05 KB
/
ArrowNotationExercise.java
File metadata and controls
62 lines (49 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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<Person, Integer> getAge = Person::getAge;
assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33)));
}
@Test
public void compareAges() {
// TODO use BiPredicate
// compareAges: (Person, Person) -> boolean
BiPredicate<Person,Person> compareAges = ((person, person2) ->
person.getAge() <= person2.getAge());
assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
}
// TODO
// getFullName: Person -> String
private String getFullName(Person person){
return person.getFirstName() + " " + person.getLastName();
}
// TODO
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int
//
private BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName(Function<Person,String> function){
return (p1,p2) -> function.apply(p1).compareTo(function.apply(p2)) > 0
? p1.getAge() : p2.getAge();
}
@Test
public void getAgeOfPersonWithTheLongestFullName() {
// Person -> String
final Function<Person, String> getFullName = this::getFullName; // TODO
// (Person, Person) -> Integer
// TODO use ageOfPersonWithTheLongestFullName(getFullName)
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName =
ageOfPersonWithTheLongestFullName(getFullName);
assertEquals(
Integer.valueOf(1),
ageOfPersonWithTheLongestFullName.apply(
new Person("a", "b", 2),
new Person("aa", "b", 1)));
}
}