From a0e155a70547870f3c699c3346cb8cdccf1032cd Mon Sep 17 00:00:00 2001 From: Filipp Kuzmin Date: Mon, 3 Jul 2017 18:22:46 +0300 Subject: [PATCH 1/2] Task 1 by Filipp Kuzmin --- .../part1/exercise/Lambdas01Exercise.java | 21 +++++++++++++++++-- .../part1/exercise/Lambdas02Exercise.java | 7 +++++-- .../part1/exercise/Lambdas03Exercise.java | 17 ++++++++++----- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index b8656b7..cb82756 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -1,5 +1,8 @@ package lambda.part1.exercise; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import data.Person; import org.junit.Test; @@ -21,7 +24,12 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, new Comparator() { + @Override + public int compare(Person o1, Person o2) { + return o1.getAge() - o2.getAge(); + } + }); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -40,7 +48,16 @@ public void findFirstWithAge30() { Person person = null; - // TODO use FluentIterable + final Optional personOptional = FluentIterable.from(persons).firstMatch(new Predicate() { + @Override + public boolean apply(Person person) { + return person.getAge() == 30; + } + }); + + if (personOptional.isPresent()) { + person = personOptional.get(); + } assertEquals(person, new Person("name 1", "lastName 2", 30)); } diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index da29209..cf0b6a4 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,9 +1,12 @@ package lambda.part1.exercise; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import data.Person; import org.junit.Test; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import static org.junit.Assert.assertArrayEquals; @@ -18,7 +21,7 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, Comparator.comparing(p -> p.getAge())); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -37,7 +40,7 @@ public void findFirstWithAge30() { Person person = null; - // TODO use FluentIterable + person = FluentIterable.from(persons).firstMatch(p -> p.getAge() == 30).get(); assertEquals(person, new Person("name 1", "lastName 2", 30)); } diff --git a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java index 59d3972..7244f2b 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java @@ -18,21 +18,28 @@ default T twice(T t) { @Test public void generic0() { - final GenericProduct prod = null; // Use anonymous class + final GenericProduct prod = new GenericProduct() { + @Override + public Integer prod(Integer a, int i) { + return a * i; + } + }; // Use anonymous class assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test public void generic1() { - final GenericProduct prod = null; // Use statement lambda + final GenericProduct prod = (a, i) -> { + return a * i; + }; // Use statement lambda assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test public void generic2() { - final GenericProduct prod = null; // Use expression lambda + final GenericProduct prod = (a, i) -> a * i; // Use expression lambda assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @@ -47,7 +54,7 @@ private static String stringProd(String s, int i) { @Test public void strSum() { - final GenericProduct prod = null; // use stringProd; + final GenericProduct prod = Lambdas03Exercise::stringProd; // use stringProd; assertEquals(prod.prod("a", 2), "aa"); } @@ -64,7 +71,7 @@ private String stringSumWithDelimeter(String s, int i) { @Test public void strSum2() { - final GenericProduct prod = null; // use stringSumWithDelimeter; + final GenericProduct prod = (a, i) -> this.stringSumWithDelimeter(a, i); // use stringSumWithDelimeter; assertEquals(prod.prod("a", 3), "a-a-a"); } From b5cafa126f9e2264e8267847f7a675293665aab8 Mon Sep 17 00:00:00 2001 From: Filipp Kuzmin Date: Wed, 5 Jul 2017 19:39:37 +0300 Subject: [PATCH 2/2] Task 2 by Filipp Kuzmin --- .../part2/exercise/ArrowNotationExercise.java | 21 ++++++++++---- .../exercise/FunctionCombinationExercise.java | 28 +++++++------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index c43edc4..ef7058f 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -14,7 +14,7 @@ public class ArrowNotationExercise { @Test public void getAge() { // Person -> Integer - final Function getAge = null; // TODO + final Function getAge = Person::getAge; // TODO assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); } @@ -24,25 +24,34 @@ public void compareAges() { // TODO use BiPredicate // compareAges: (Person, Person) -> boolean - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + final BiPredicate compareAges = ((person1, person2) -> person1.getAge() == person2.getAge()); + + assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); } // TODO // getFullName: Person -> String + public String getFullName(Person person) { + return person.getFirstName() + " " + person.getLastName(); + } // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // + public BiFunction ageOfPersonWithTheLongestFullName(Function getFullName) { + return (p1, p2) -> Integer.compare(getFullName.apply(p1).length(), getFullName.apply(p2).length()) > 0 + ? p1.getAge() : p2.getAge(); + } + @Test public void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = null; // TODO + final Function getFullName = this::getFullName; // TODO // (Person, Person) -> Integer // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = null; + final BiFunction ageOfPersonWithTheLongestFullName = + ageOfPersonWithTheLongestFullName(getFullName); assertEquals( Integer.valueOf(1), diff --git a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java index ce7080f..7f4b1c5 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -22,19 +22,13 @@ public void personHasNotEmptyLastNameAndFirstName0() { // TODO // negate1: (Person -> boolean) -> (Person -> boolean) private Predicate negate1(Predicate test) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + return p -> !test.test(p); } // TODO // validateFirstNameAndLastName: (Person -> boolean, Person -> boolean) -> (Person -> boolean) private Predicate validateFirstNameAndLastName(Predicate t1, Predicate t2) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + return p -> t1.test(p) && t2.test(p); } @Test @@ -55,15 +49,13 @@ public void personHasNotEmptyLastNameAndFirstName1() { // TODO // negate: (T -> boolean) -> (T -> boolean) private Predicate negate(Predicate test) { - // TODO - throw new UnsupportedOperationException(); + return t -> !test.test(t); } // TODO // and: (T -> boolean, T -> boolean) -> (T -> boolean) private Predicate and(Predicate t1, Predicate t2) { - // TODO - throw new UnsupportedOperationException(); + return (x) -> t1.test(x) && t2.test(x); } @Test @@ -71,10 +63,10 @@ public void personHasNotEmptyLastNameAndFirstName2() { final Predicate hasEmptyFirstName = p -> p.getFirstName().isEmpty(); final Predicate hasEmptyLastName = p -> p.getLastName().isEmpty(); - final Predicate validateFirstName = null; // TODO use negate - final Predicate validateLastName = null; // TODO use negate + final Predicate validateFirstName = negate(hasEmptyFirstName); // TODO use negate + final Predicate validateLastName = negate(hasEmptyLastName); // TODO use negate - final Predicate validate = null; // TODO use and + final Predicate validate = and(validateFirstName, validateLastName); // TODO use and assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0))); @@ -86,10 +78,10 @@ public void personHasNotEmptyLastNameAndFirstName3() { final Predicate hasEmptyFirstName = p -> p.getFirstName().isEmpty(); final Predicate hasEmptyLastName = p -> p.getLastName().isEmpty(); - final Predicate validateFirstName = null; // TODO use Predicate::negate - final Predicate validateLastName = null; // TODO use Predicate::negate + final Predicate validateFirstName = hasEmptyFirstName.negate(); // TODO use Predicate::negate + final Predicate validateLastName = hasEmptyLastName.negate(); // TODO use Predicate::negate - final Predicate validate = null; // TODO use Predicate::and + final Predicate validate = and(validateFirstName, validateLastName); // TODO use Predicate::and assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0)));