Skip to content

Commit 3393e22

Browse files
author
fdpro
committed
Removed test methods
1 parent 3ee04b2 commit 3393e22

6 files changed

Lines changed: 75 additions & 221 deletions

File tree

src/main/java/com/fdpro/trainings/functionaljava/HomemadeFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
public interface HomemadeFunction<T, U> {
55

66
U apply(T input);
7+
8+
default <V> HomemadeFunction<T, V> andThen(HomemadeFunction<U, V> after) {
9+
return input -> after.apply(this.apply(input));
10+
}
711
}
Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,32 @@
11
package com.fdpro.trainings.functionaljava;
22

3-
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.Test;
5-
63
import java.util.ArrayList;
74
import java.util.Arrays;
85
import java.util.List;
96
import java.util.function.Consumer;
107

11-
import static org.assertj.core.api.Assertions.assertThat;
12-
138
class FunctionalInterfaces_Consumer {
149

15-
private List<Person> people;
16-
17-
@BeforeEach
18-
void initPeople() {
19-
people = Arrays.asList(
10+
public static void main(String[] args) {
11+
List<Person> people = Arrays.asList(
2012
new Person("Robert", 50),
2113
new Person("Anna", 43),
2214
new Person("Kevin", 16),
2315
new Person("Amelia", 30)
2416
);
25-
}
26-
27-
private void consumePeopleInfo(List<Person> people, Consumer<Person> consumer) {
28-
for (Person person : people) {
29-
consumer.accept(person);
30-
}
31-
}
3217

33-
@Test
34-
void givenPeople_whenConsumePeopleInfo_PrintNames_thenNamesPrinter() {
3518
Consumer<Person> printName = person -> System.out.println(person.getName());
3619
consumePeopleInfo(people, printName);
37-
}
3820

39-
@Test
40-
void givenPeople_whenConsumePeopleInfo_PrintNamesAndGather_thenNamesPrintedAndResult() {
4121
List<Person> gatheredPeople = new ArrayList<>();
42-
43-
Consumer<Person> printName = person -> System.out.println(person.getName());
4422
Consumer<Person> gather = gatheredPeople::add;
4523
consumePeopleInfo(people, printName.andThen(gather));
24+
System.out.println(gatheredPeople);
25+
}
4626

47-
assertThat(gatheredPeople).isEqualTo(people);
27+
private static void consumePeopleInfo(List<Person> people, Consumer<Person> consumer) {
28+
for (Person person : people) {
29+
consumer.accept(person);
30+
}
4831
}
4932
}
Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,33 @@
11
package com.fdpro.trainings.functionaljava;
22

3-
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.Test;
5-
63
import java.time.Year;
74
import java.util.ArrayList;
85
import java.util.Arrays;
96
import java.util.List;
107
import java.util.function.Function;
118

12-
import static org.assertj.core.api.Assertions.assertThat;
13-
149
class FunctionalInterfaces_Functions {
1510

16-
private List<Person> people;
17-
18-
@BeforeEach
19-
void initPeople() {
20-
people = Arrays.asList(
11+
public static void main(String[] args) {
12+
List<Person> people = Arrays.asList(
2113
new Person("Robert", 50),
2214
new Person("Anna", 43),
2315
new Person("Kevin", 16)
2416
);
17+
18+
Function<Person, Integer> getAge = Person::getAge;
19+
Function<Integer, Year> getYearOfBirth = age -> Year.now().minusYears(age);
20+
List<Year> years = getPeopleInfo(people, getAge.andThen(getYearOfBirth));
21+
22+
years = getPeopleInfo(people, getYearOfBirth.compose(getAge));
23+
System.out.println(years);
2524
}
2625

27-
private <T> List<T> getPeopleInfo(List<Person> people, Function<Person, T> function) {
26+
private static <T> List<T> getPeopleInfo(List<Person> people, Function<Person, T> function) {
2827
List<T> result = new ArrayList<>();
2928
for (Person person : people) {
3029
result.add(function.apply(person));
3130
}
3231
return result;
3332
}
34-
35-
@Test
36-
void givenPeople_whenGetPeopleInfo_Ages_thenResult() {
37-
Function<Person, Integer> getAge = Person::getAge;
38-
List<Integer> peopleAges = getPeopleInfo(people, getAge);
39-
40-
assertThat(peopleAges).containsExactly(50, 43, 16);
41-
}
42-
43-
@Test
44-
void givenPeople_whenGetPeopleInfo_YearBorn_AndThen_thenResult() {
45-
Function<Person, Integer> getAge = Person::getAge;
46-
Function<Integer, Year> getYearBorn = age -> Year.now().minusYears(age);
47-
List<Year> peopleAges = getPeopleInfo(people, getAge.andThen(getYearBorn));
48-
49-
assertThat(peopleAges).containsExactly(Year.of(1969), Year.of(1976), Year.of(2003));
50-
}
51-
52-
@Test
53-
void givenPeople_whenGetPeopleInfo_YearBorn_Compose_thenResult() {
54-
Function<Person, Integer> getAge = Person::getAge;
55-
Function<Integer, Year> getYearBorn = age -> Year.now().minusYears(age);
56-
List<Year> peopleAges = getPeopleInfo(people, getYearBorn.compose(getAge));
57-
58-
assertThat(peopleAges).containsExactly(Year.of(1969), Year.of(1976), Year.of(2003));
59-
}
6033
}
Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
package com.fdpro.trainings.functionaljava;
22

3-
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.Test;
5-
63
import java.util.ArrayList;
74
import java.util.Arrays;
85
import java.util.List;
96
import java.util.function.Predicate;
107

11-
import static org.assertj.core.api.Assertions.assertThat;
12-
138
class FunctionalInterfaces_Predicate {
149

15-
private List<Person> people;
16-
17-
@BeforeEach
18-
void initPeople() {
19-
people = Arrays.asList(
10+
public static void main(String[] args) {
11+
List<Person> people = Arrays.asList(
2012
new Person("Robert", 50),
2113
new Person("Anna", 43),
2214
new Person("Kevin", 16),
2315
new Person("Amelia", 30)
2416
);
17+
18+
Predicate<Person> olderThanForty = person -> person.getAge() >= 40;
19+
List<Person> peopleOlderThanForty = getPeopleMatching(people, olderThanForty);
20+
21+
Predicate<Person> hasShortName = person -> person.getName().length() <= 5;
22+
23+
List<Person> peopleOlderThanFortyWithShortNames = getPeopleMatching(people, olderThanForty.and(hasShortName));
24+
25+
List<Person> peopleOlderThanFortyOrWithShortNames = getPeopleMatching(people, olderThanForty.or(hasShortName));
26+
27+
List<Person> peopleYoungerThanForty = getPeopleMatching(people, olderThanForty.negate());
28+
System.out.println(peopleYoungerThanForty);
2529
}
2630

27-
private List<Person> getPeopleMatching(List<Person> people, Predicate<Person> predicate) {
31+
private static List<Person> getPeopleMatching(List<Person> people, Predicate<Person> predicate) {
2832
List<Person> result = new ArrayList<>();
2933
for (Person person : people) {
3034
if (predicate.test(person)) {
@@ -33,50 +37,4 @@ private List<Person> getPeopleMatching(List<Person> people, Predicate<Person> pr
3337
}
3438
return result;
3539
}
36-
37-
@Test
38-
void givenPeople_whenGetPeopleMatching_AgeAboveForty_thenResult() {
39-
Predicate<Person> ageAboveForty = person -> person.getAge() >= 40;
40-
List<Person> peopleAges = getPeopleMatching(people, ageAboveForty);
41-
42-
assertThat(peopleAges).containsExactly(
43-
new Person("Robert", 50),
44-
new Person("Anna", 43)
45-
);
46-
}
47-
48-
@Test
49-
void givenPeople_whenGetPeopleMatching_AgeAboveFortyAndShortName_thenResult() {
50-
Predicate<Person> ageAboveForty = person -> person.getAge() >= 40;
51-
Predicate<Person> shortName = person -> person.getName().length() <= 5;
52-
List<Person> peopleAges = getPeopleMatching(people, ageAboveForty.and(shortName));
53-
54-
assertThat(peopleAges).containsExactly(
55-
new Person("Anna", 43)
56-
);
57-
}
58-
59-
@Test
60-
void givenPeople_whenGetPeopleMatching_AgeAboveFortyOrShortName_thenResult() {
61-
Predicate<Person> ageAboveForty = person -> person.getAge() >= 40;
62-
Predicate<Person> shortName = person -> person.getName().length() <= 5;
63-
List<Person> peopleAges = getPeopleMatching(people, ageAboveForty.or(shortName));
64-
65-
assertThat(peopleAges).containsExactly(
66-
new Person("Robert", 50),
67-
new Person("Anna", 43),
68-
new Person("Kevin", 16)
69-
);
70-
}
71-
72-
@Test
73-
void givenPeople_whenGetPeopleMatching_AgeBesideForty_thenResult() {
74-
Predicate<Person> ageAboveForty = person -> person.getAge() >= 40;
75-
List<Person> peopleAges = getPeopleMatching(people, ageAboveForty.negate());
76-
77-
assertThat(peopleAges).containsExactly(
78-
new Person("Kevin", 16),
79-
new Person("Amelia", 30)
80-
);
81-
}
8240
}
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
package com.fdpro.trainings.functionaljava;
22

3-
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.Test;
5-
63
import java.util.ArrayList;
74
import java.util.Arrays;
85
import java.util.List;
96
import java.util.Random;
107
import java.util.function.Supplier;
118

12-
import static org.assertj.core.api.Assertions.assertThat;
13-
149
class FunctionalInterfaces_Supplier {
1510

16-
private List<String> peopleNames;
17-
18-
@BeforeEach
19-
void initPeople() {
20-
peopleNames = Arrays.asList(
11+
public static void main(String[] args) {
12+
List<String> peopleNames = Arrays.asList(
2113
"Robert",
2214
"Anna",
2315
"Kevin",
2416
"Amelia"
2517
);
18+
19+
Random random = new Random();
20+
Supplier<Integer> generateAge = () -> random.nextInt(75);
21+
List<Person> people = giveAges(peopleNames, generateAge);
22+
System.out.println(people);
2623
}
2724

28-
private List<Person> giveAges(List<String> peopleNames, Supplier<Integer> supplier) {
25+
private static List<Person> giveAges(List<String> peopleNames, Supplier<Integer> supplier) {
2926
List<Person> people = new ArrayList<>();
3027
for (String personName : peopleNames) {
3128
people.add(new Person(personName, supplier.get()));
3229
}
3330
return people;
3431
}
35-
36-
@Test
37-
void givenPeople_whenGiveAges_thenResult() {
38-
Supplier<Integer> generateAge = () -> new Random().nextInt(75);
39-
List<Person> people = giveAges(peopleNames, generateAge);
40-
41-
assertThat(people).allMatch(person -> person.getAge() >= 0 && person.getAge() <= 74);
42-
}
43-
}
32+
}

0 commit comments

Comments
 (0)