Skip to content

Commit b811212

Browse files
author
fdpro
committed
Added BiFunction examples
1 parent 0836ed7 commit b811212

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.fdpro.trainings.functionaljava;
2+
3+
import java.util.function.BiFunction;
4+
5+
public class BiFunctionExamples {
6+
7+
public static <T> T getInfo(Person personA, Person personB, BiFunction<Person, Person, T> infoCollector) {
8+
return infoCollector.apply(personA, personB);
9+
}
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fdpro.trainings.functionaljava;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.function.BiFunction;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class BiFunctionExamplesTest {
11+
12+
private Person robert;
13+
private Person anna;
14+
15+
@BeforeEach
16+
void initPeople() {
17+
robert = new Person("Robert", 50);
18+
anna = new Person("Anna", 43);
19+
}
20+
21+
@Test
22+
void getInfo_MaxAge() {
23+
BiFunction<Person, Person, Integer> getMaxAge = (p1, p2) -> Math.max(p1.getAge(), p2.getAge());
24+
25+
Integer maxAge = BiFunctionExamples.getInfo(robert, anna, getMaxAge);
26+
assertThat(maxAge).isEqualTo(50);
27+
}
28+
29+
@Test
30+
void getInfo_PersonWithSmallestName() {
31+
BiFunction<Person, Person, Person> getPersonWithSmallestName = (p1, p2) -> p1.getName().length() <= p2.getName().length() ? p1 : p2;
32+
33+
Person personWithSmallestName = BiFunctionExamples.getInfo(robert, anna, getPersonWithSmallestName);
34+
assertThat(personWithSmallestName).isEqualTo(anna);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)