diff --git a/src/main/java/com/fdpro/trainings/functionaljava/HomemadeFunction.java b/src/main/java/com/fdpro/trainings/functionaljava/HomemadeFunction.java index 1b5ec42..6697992 100644 --- a/src/main/java/com/fdpro/trainings/functionaljava/HomemadeFunction.java +++ b/src/main/java/com/fdpro/trainings/functionaljava/HomemadeFunction.java @@ -1,7 +1,4 @@ package com.fdpro.trainings.functionaljava; -@FunctionalInterface public interface HomemadeFunction { - - U apply(T input); } diff --git a/src/test/java/com/fdpro/trainings/functionaljava/Comparatorss.java b/src/test/java/com/fdpro/trainings/functionaljava/Comparatorss.java index 2d7403d..dee0984 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/Comparatorss.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/Comparatorss.java @@ -26,7 +26,6 @@ void initPeople() { @Test void givenPeople_whenSort_Ages_thenResult() { - people.sort(Comparator.comparing(Person::getAge)); assertThat(people).containsExactly( new Person("Kevin", 16), @@ -39,10 +38,6 @@ void givenPeople_whenSort_Ages_thenResult() { @Test void givenPeople_whenSort_AgesThenNamesLength_thenResult() { - people.sort( - Comparator.comparing(Person::getAge) - .thenComparing(person -> person.getName().length()) - ); assertThat(people).containsExactly( new Person("Mike", 16), @@ -55,11 +50,6 @@ void givenPeople_whenSort_AgesThenNamesLength_thenResult() { @Test void givenPeople_whenSort_AgesThenNamesReversed_thenResult() { - people.sort( - Comparator.comparing(Person::getAge) - .thenComparing(person -> person.getName().length()) - .reversed() - ); assertThat(people).containsExactly( new Person("Robert", 50), diff --git a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Consumer.java b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Consumer.java index 1280cf7..9446277 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Consumer.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Consumer.java @@ -1,27 +1,21 @@ package com.fdpro.trainings.functionaljava; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.assertj.core.api.Assertions.assertThat; - class FunctionalInterfaces_Consumer { - private List people; - - @BeforeEach - void initPeople() { - people = Arrays.asList( + public static void main(String[] args) { + List people = Arrays.asList( new Person("Robert", 50), new Person("Anna", 43), new Person("Kevin", 16), new Person("Amelia", 30) ); + + //TODO Prints people names + //TODO Prints people names and gather into a list } private void consumePeopleInfo(List people, Consumer consumer) { @@ -29,21 +23,4 @@ private void consumePeopleInfo(List people, Consumer consumer) { consumer.accept(person); } } - - @Test - void givenPeople_whenConsumePeopleInfo_PrintNames_thenNamesPrinter() { - Consumer printName = person -> System.out.println(person.getName()); - consumePeopleInfo(people, printName); - } - - @Test - void givenPeople_whenConsumePeopleInfo_PrintNamesAndGather_thenNamesPrintedAndResult() { - List gatheredPeople = new ArrayList<>(); - - Consumer printName = person -> System.out.println(person.getName()); - Consumer gather = gatheredPeople::add; - consumePeopleInfo(people, printName.andThen(gather)); - - assertThat(gatheredPeople).isEqualTo(people); - } } diff --git a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Functions.java b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Functions.java index f8e09b0..d41f816 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Functions.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Functions.java @@ -1,27 +1,22 @@ package com.fdpro.trainings.functionaljava; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.time.Year; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; -import static org.assertj.core.api.Assertions.assertThat; - class FunctionalInterfaces_Functions { - private List people; - - @BeforeEach - void initPeople() { - people = Arrays.asList( + public static void main(String[] args) { + List people = Arrays.asList( new Person("Robert", 50), new Person("Anna", 43), new Person("Kevin", 16) ); + + //TODO Get people ages + //TODO Get people year of birth with andThen + //TODO Get people year of birth with compose } private List getPeopleInfo(List people, Function function) { @@ -31,30 +26,4 @@ private List getPeopleInfo(List people, Function funct } return result; } - - @Test - void givenPeople_whenGetPeopleInfo_Ages_thenResult() { - Function getAge = Person::getAge; - List peopleAges = getPeopleInfo(people, getAge); - - assertThat(peopleAges).containsExactly(50, 43, 16); - } - - @Test - void givenPeople_whenGetPeopleInfo_YearBorn_AndThen_thenResult() { - Function getAge = Person::getAge; - Function getYearBorn = age -> Year.now().minusYears(age); - List peopleAges = getPeopleInfo(people, getAge.andThen(getYearBorn)); - - assertThat(peopleAges).containsExactly(Year.of(1969), Year.of(1976), Year.of(2003)); - } - - @Test - void givenPeople_whenGetPeopleInfo_YearBorn_Compose_thenResult() { - Function getAge = Person::getAge; - Function getYearBorn = age -> Year.now().minusYears(age); - List peopleAges = getPeopleInfo(people, getYearBorn.compose(getAge)); - - assertThat(peopleAges).containsExactly(Year.of(1969), Year.of(1976), Year.of(2003)); - } } diff --git a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Predicate.java b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Predicate.java index b004d69..89206ab 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Predicate.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Predicate.java @@ -1,27 +1,24 @@ package com.fdpro.trainings.functionaljava; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; -import static org.assertj.core.api.Assertions.assertThat; - class FunctionalInterfaces_Predicate { - private List people; - - @BeforeEach - void initPeople() { - people = Arrays.asList( + public static void main(String[] args) { + List people = Arrays.asList( new Person("Robert", 50), new Person("Anna", 43), new Person("Kevin", 16), new Person("Amelia", 30) ); + + //TODO Get people older than forty + //TODO Get people older than forty and having short names + //TODO Get people older than forty or having short names + //TODO Get people younger than forty } private List getPeopleMatching(List people, Predicate predicate) { @@ -33,50 +30,4 @@ private List getPeopleMatching(List people, Predicate pr } return result; } - - @Test - void givenPeople_whenGetPeopleMatching_AgeAboveForty_thenResult() { - Predicate ageAboveForty = person -> person.getAge() >= 40; - List peopleAges = getPeopleMatching(people, ageAboveForty); - - assertThat(peopleAges).containsExactly( - new Person("Robert", 50), - new Person("Anna", 43) - ); - } - - @Test - void givenPeople_whenGetPeopleMatching_AgeAboveFortyAndShortName_thenResult() { - Predicate ageAboveForty = person -> person.getAge() >= 40; - Predicate shortName = person -> person.getName().length() <= 5; - List peopleAges = getPeopleMatching(people, ageAboveForty.and(shortName)); - - assertThat(peopleAges).containsExactly( - new Person("Anna", 43) - ); - } - - @Test - void givenPeople_whenGetPeopleMatching_AgeAboveFortyOrShortName_thenResult() { - Predicate ageAboveForty = person -> person.getAge() >= 40; - Predicate shortName = person -> person.getName().length() <= 5; - List peopleAges = getPeopleMatching(people, ageAboveForty.or(shortName)); - - assertThat(peopleAges).containsExactly( - new Person("Robert", 50), - new Person("Anna", 43), - new Person("Kevin", 16) - ); - } - - @Test - void givenPeople_whenGetPeopleMatching_AgeBesideForty_thenResult() { - Predicate ageAboveForty = person -> person.getAge() >= 40; - List peopleAges = getPeopleMatching(people, ageAboveForty.negate()); - - assertThat(peopleAges).containsExactly( - new Person("Kevin", 16), - new Person("Amelia", 30) - ); - } } diff --git a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Supplier.java b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Supplier.java index 084ed49..d0d205f 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Supplier.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalInterfaces_Supplier.java @@ -1,28 +1,21 @@ package com.fdpro.trainings.functionaljava; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Random; import java.util.function.Supplier; -import static org.assertj.core.api.Assertions.assertThat; - class FunctionalInterfaces_Supplier { - private List peopleNames; - - @BeforeEach - void initPeople() { - peopleNames = Arrays.asList( + public static void main(String[] args) { + List peopleNames = Arrays.asList( "Robert", "Anna", "Kevin", "Amelia" ); + + //TODO Generate people with random ages } private List giveAges(List peopleNames, Supplier supplier) { @@ -32,12 +25,4 @@ private List giveAges(List peopleNames, Supplier suppli } return people; } - - @Test - void givenPeople_whenGiveAges_thenResult() { - Supplier generateAge = () -> new Random().nextInt(75); - List people = giveAges(peopleNames, generateAge); - - assertThat(people).allMatch(person -> person.getAge() >= 0 && person.getAge() <= 74); - } -} +} \ No newline at end of file diff --git a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalProgramming.java b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalProgramming.java index da46a15..ba60531 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/FunctionalProgramming.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/FunctionalProgramming.java @@ -1,126 +1,21 @@ package com.fdpro.trainings.functionaljava; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.function.Function; - -import static org.assertj.core.api.Assertions.assertThat; class FunctionalProgramming { - private List people; - - @BeforeEach - void initPeople() { - people = Arrays.asList( + public static void main(String[] args) { + List people = Arrays.asList( new Person("Robert", 50), new Person("Anna", 43), new Person("Kevin", 16) ); - } - - private List getPeopleNames(List people) { - List result = new ArrayList<>(); - for (Person person : people) { - result.add(person.getName()); - } - return result; - } - - @Test - void givenPeople_whenGetPeopleNames_thenResult() { - List peopleNames = getPeopleNames(people); - - assertThat(peopleNames).containsExactly("Robert", "Anna", "Kevin"); - } - - private List getPeopleAges(List people) { - List result = new ArrayList<>(); - for (Person person : people) { - result.add(person.getAge()); - } - return result; - } - - @Test - void givenPeople_whenGetPeopleAges_thenResult() { - List peopleAges = getPeopleAges(people); - - assertThat(peopleAges).containsExactly(50, 43, 16); - } - - private List getPeopleInfo(List people, HomemadeFunction function) { - List result = new ArrayList<>(); - for (Person person : people) { - result.add(function.apply(person)); - } - return result; - } - - @Test - void givenPeople_whenGetPeopleInfo_Names_thenResult() { - HomemadeFunction getName = new HomemadeFunction() { - @Override - public String apply(Person input) { - return input.getName(); - } - }; - List peopleNames = getPeopleInfo(people, getName); - - assertThat(peopleNames).containsExactly("Robert", "Anna", "Kevin"); - } - - @Test - void givenPeople_whenGetPeopleInfo_Ages_thenResult() { - HomemadeFunction getAge = new HomemadeFunction() { - @Override - public Integer apply(Person input) { - return input.getAge(); - } - }; - List peopleAges = getPeopleInfo(people, getAge); - - assertThat(peopleAges).containsExactly(50, 43, 16); - } - - private List getPeopleInfoWithFunction(List people, Function function) { - List result = new ArrayList<>(); - for (Person person : people) { - result.add(function.apply(person)); - } - return result; - } - - @Test - void givenPeople_whenGetPeopleInfoWithFunction_Ages_thenResult() { - Function getAge = new Function() { - @Override - public Integer apply(Person input) { - return input.getAge(); - } - }; - List peopleAges = getPeopleInfoWithFunction(people, getAge); - - assertThat(peopleAges).containsExactly(50, 43, 16); - } - - @Test - void givenPeople_whenGetPeopleInfoWithFunction_Ages_Lambda_thenResult() { - Function getAge = input -> input.getAge(); - List peopleAges = getPeopleInfoWithFunction(people, getAge); - - assertThat(peopleAges).containsExactly(50, 43, 16); - } - - @Test - void givenPeople_whenGetPeopleInfoWithFunction_Ages_MethodReference_thenResult() { - Function getAge = Person::getAge; - List peopleAges = getPeopleInfoWithFunction(people, getAge); - assertThat(peopleAges).containsExactly(50, 43, 16); + //TODO Get people names old style + //TODO Get people ages old style + //TODO Get people names with homemade function + //TODO Get people ages with homemade function + //TODO Get people ages with Java Function } -} +} \ No newline at end of file diff --git a/src/test/java/com/fdpro/trainings/functionaljava/Optionals.java b/src/test/java/com/fdpro/trainings/functionaljava/Optionals.java index 62c8945..7c9c067 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/Optionals.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/Optionals.java @@ -23,7 +23,7 @@ void initPeople() { @Test void givenPerson_whenOptionalOf_thenResult() { - Optional somebody = Optional.of(robert); + Optional somebody = null; assertThat(somebody) .isPresent() @@ -32,7 +32,7 @@ void givenPerson_whenOptionalOf_thenResult() { @Test void givenPerson_whenOptionalOfNullable_Filled_thenResult() { - Optional somebody = Optional.ofNullable(robert); + Optional somebody = null; assertThat(somebody) .isPresent() @@ -41,22 +41,21 @@ void givenPerson_whenOptionalOfNullable_Filled_thenResult() { @Test void givenPerson_whenOptionalOfNullable_Empty_thenResult() { - Optional somebody = Optional.ofNullable(null); + Optional somebody = null; assertThat(somebody).isEmpty(); } @Test void givenPerson_whenOptionalEmpty_thenResult() { - Optional somebody = Optional.empty(); + Optional somebody = null; assertThat(somebody).isEmpty(); } @Test void givenPerson_whenOptionalFilter_Match_thenResult() { - Optional somebody = Optional.of(robert) - .filter(person -> person.getAge() >= 40); + Optional somebody = null; assertThat(somebody) .isPresent() @@ -65,16 +64,14 @@ void givenPerson_whenOptionalFilter_Match_thenResult() { @Test void givenPerson_whenOptionalFilter_NoMatch_thenResult() { - Optional somebody = Optional.of(robert) - .filter(person -> person.getAge() < 40); + Optional somebody = null; assertThat(somebody).isEmpty(); } @Test void givenPerson_whenOptionalMap_thenResult() { - Optional somebody = Optional.of(robert) - .map(Person::getAge); + Optional somebody = null; assertThat(somebody) .isPresent() @@ -83,77 +80,65 @@ void givenPerson_whenOptionalMap_thenResult() { @Test void givenPerson_whenOptionalOrElse_Filled_thenResult() { - Person somebody = Optional.of(robert).orElse(anna); + Person somebody = null; assertThat(somebody).isEqualTo(robert); } @Test void givenPerson_whenOptionalOrElse_Empty_thenResult() { - Person somebody = Optional.empty().orElse(anna); + Person somebody = null; assertThat(somebody).isEqualTo(anna); } @Test void givenPerson_whenOptionalOrElseGet_Filled_thenResult() { - Supplier personSupplier = () -> { - System.out.println("Supplied"); - return anna; - }; - Person somebody = Optional.of(robert).orElseGet(personSupplier); + Person somebody = null; assertThat(somebody).isEqualTo(robert); } @Test void givenPerson_whenOptionalOrElseGet_Empty_thenResult() { - Supplier personSupplier = () -> { - System.out.println("Supplied"); - return anna; - }; - Person somebody = Optional.empty().orElseGet(personSupplier); + Person somebody = null; assertThat(somebody).isEqualTo(anna); } @Test void givenPerson_whenOptionalOrElseThrow_Filled_thenResult() { - Person somebody = Optional.of(robert).orElseThrow(IllegalStateException::new); + Person somebody = null; assertThat(somebody).isEqualTo(robert); } @Test void givenPerson_whenOptionalOrElseThrow_Empty_thenResult() { - assertThatThrownBy(() -> Optional.empty().orElseThrow(IllegalStateException::new)) + assertThatThrownBy(null) .isInstanceOf(IllegalStateException.class); } @Test void givenPerson_whenOptionalGet_Filled_thenResult() { - Person somebody = Optional.of(robert).get(); + Person somebody = null; assertThat(somebody).isEqualTo(robert); } @Test void givenPerson_whenOptionalGet_Empty_thenResult() { - assertThatThrownBy(() -> Optional.empty().get()) + assertThatThrownBy(null) .isInstanceOf(NoSuchElementException.class); } @Test void givenPerson_whenOptionalIsPresent_Filled_thenResult() { - Optional somebody = Optional.of(robert); - if (somebody.isPresent()) { - System.out.println("Robert's here!"); - } + } @Test void givenPerson_whenOptionalIfPresent_Filled_thenResult() { - Optional somebody = Optional.of(robert); - somebody.ifPresent(person -> System.out.println("Robert's here!")); + } } diff --git a/src/test/java/com/fdpro/trainings/functionaljava/Streams.java b/src/test/java/com/fdpro/trainings/functionaljava/Streams.java index 9af3e58..4d69aa6 100644 --- a/src/test/java/com/fdpro/trainings/functionaljava/Streams.java +++ b/src/test/java/com/fdpro/trainings/functionaljava/Streams.java @@ -24,9 +24,7 @@ void initPeople() { @Test void givenPeople_whenStreamFilter_thenResult() { - List peopleOlderThanForty = people.stream() - .filter(person -> person.getAge() >= 40) - .collect(Collectors.toList()); + List peopleOlderThanForty = null; assertThat(peopleOlderThanForty).containsExactly( new Person("Robert", 50), @@ -36,9 +34,7 @@ void givenPeople_whenStreamFilter_thenResult() { @Test void givenPeople_whenStreamMap_thenResult() { - List peopleNames = people.stream() - .map(Person::getName) - .collect(Collectors.toList()); + List peopleNames = null; assertThat(peopleNames).containsExactly( "Robert", @@ -48,22 +44,18 @@ void givenPeople_whenStreamMap_thenResult() { @Test void givenPeople_whenStreamCollect_thenResult() { - Set peopleSet = this.people.stream() - .collect(Collectors.toSet()); + Set peopleSet = null; assertThat(peopleSet).containsAll(people); } @Test void givenPeople_whenStreamForEach_thenResultPrinted() { - people.stream() - .forEach(System.out::println); } @Test void givenPeople_whenStreamMin_thenResult() { - Optional youngestPerson = people.stream() - .min(Comparator.comparing(Person::getAge)); + Optional youngestPerson = null; assertThat(youngestPerson) .isPresent() @@ -72,23 +64,16 @@ void givenPeople_whenStreamMin_thenResult() { @Test void givenPeople_whenStreamMax_thenResult() { - Optional youngestPerson = people.stream() - .max(Comparator.comparing(Person::getAge)); + Optional oldestPerson = null; - assertThat(youngestPerson) + assertThat(oldestPerson) .isPresent() .contains(new Person("Robert", 50)); } @Test void givenPeople_whenStreamReduce_thenResult() { - Double averageAge = people.stream() - .map(Person::getAge) - .reduce( - 0d, - (result, age) -> result + ((double) age / people.size()), - (result1, result2) -> (result1 + result2) / 2 - ); + double averageAge = 0d; assertThat(averageAge).isEqualTo(34.75); }