-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathLambdaExercise.java
More file actions
executable file
·101 lines (70 loc) · 3.63 KB
/
LambdaExercise.java
File metadata and controls
executable file
·101 lines (70 loc) · 3.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package part3.exercise.lambda;
import data.Person;
import org.junit.Test;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static org.junit.Assert.assertEquals;
public class LambdaExercise {
@Test
public void supply() {
final Person person = new Person("John", "Galt", 30);
final Supplier<Person> getPerson = () -> person;
assertEquals(person, getPerson.get());
}
@Test
public void function() {
final Function<Person, String> getPersonName1 = person -> person.getFirstName(); // TODO get the name of person using expression lambda
final Function<Person, String> getPersonName2 = Person::getFirstName; // TODO get the name of person using method reference
// TODO get the name of person and log it to System.out using statement lambda: {}
final Function<Person, String> getPersonNameAndLogIt = person -> {
String firstName = person.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));
assertEquals(person.getFirstName(), getPersonNameAndLogIt.apply(person));
}
@Test
public void combineFunctions() {
final Function<Person, String> getPersonName = Person::getFirstName; // TODO get the name of person
assertEquals("John", getPersonName.apply(new Person("John", "Galt", 30)));
final Function<String, Integer> getStringLength = s -> s.length(); // TODO get string length
assertEquals(Integer.valueOf(3), getStringLength.apply("ABC"));
// TODO get person name length using getPersonName and getStringLength without andThen
final Function<Person, Integer> getPersonNameLength1 =
person -> getStringLength.apply(getPersonName.apply(person));
// TODO get person name length using getPersonName and getStringLength with andThen
final Function<Person, Integer> 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));
}
private interface PersonFactory {
Person create(String name, String lastName, int age);
}
private Person createPerson(PersonFactory pf) {
return pf.create("John", "Galt", 66);
}
// ((T -> R), (R -> boolean)) -> (T -> boolean)
private <T, R> Predicate<T> combine(Function<T, R> f, Predicate<R> p) {
// TODO
return t -> p.test(f.apply(t));
}
@Test
public void methodReference() {
// TODO use only method reverences here.
final Person person = createPerson(Person::new); // TODO
assertEquals(new Person("John", "Galt", 66), person);
final Function<Person, String> getPersonName = Person::getFirstName; // TODO
assertEquals("John", getPersonName.apply(person));
final Predicate<String> isJohnString = this::johnTest; // TODO using method reference check that "John" equals string parameter
final Predicate<Person> isJohnPerson = combine(getPersonName, isJohnString);
assertEquals(true, isJohnPerson.test(person));
}
private boolean johnTest(String name){
return name.equals("John");
}
}