diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index b8656b7..3b13dbb 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 p1, Person p2) { + return Integer.compare(p1.getAge(), p2.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..9081173 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,9 +1,13 @@ package lambda.part1.exercise; +import com.google.common.base.Optional; +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 +22,7 @@ public void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, Comparator.comparingInt(p -> p.getAge())); assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), @@ -37,7 +41,11 @@ public void findFirstWithAge30() { Person person = null; - // TODO use FluentIterable + final Optional personOptional = + FluentIterable.from(persons).firstMatch(p -> p.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/Lambdas03Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java index 59d3972..40ce7a7 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas03Exercise.java @@ -17,22 +17,29 @@ default T twice(T t) { } @Test - public void generic0() { - final GenericProduct prod = null; // Use anonymous class + public void generic0() { // Use anonymous class + final GenericProduct prod = new GenericProduct() { + @Override + public Integer prod(Integer a, int i) { + return a * i; + } + }; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test - public void generic1() { - final GenericProduct prod = null; // Use statement lambda + public void generic1() { // Use statement lambda + final GenericProduct prod = (a, i) -> { + return a * i; + }; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @Test - public void generic2() { - final GenericProduct prod = null; // Use expression lambda + public void generic2() { // Use expression lambda + final GenericProduct prod = (a, i) -> a * i; assertEquals(prod.prod(3, 2), Integer.valueOf(6)); } @@ -46,8 +53,8 @@ private static String stringProd(String s, int i) { } @Test - public void strSum() { - final GenericProduct prod = null; // use stringProd; + public void strSum() { // Use stringProd + final GenericProduct prod = Lambdas03Exercise::stringProd; assertEquals(prod.prod("a", 2), "aa"); } @@ -63,8 +70,8 @@ private String stringSumWithDelimeter(String s, int i) { } @Test - public void strSum2() { - final GenericProduct prod = null; // use stringSumWithDelimeter; + public void strSum2() { // Use stringSumWithDelimeter + final GenericProduct prod = this::stringSumWithDelimeter; assertEquals(prod.prod("a", 3), "a-a-a"); }