From a0c70139fd2b1829d8b78700a79bcde0adc3703b Mon Sep 17 00:00:00 2001 From: Egor Kozyrev Date: Fri, 31 Mar 2017 15:22:32 +0300 Subject: [PATCH 1/3] exercise 1 --- .../part1/exercise/Lambdas01Exercise.java | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index 14cfc0d..57d8252 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -1,13 +1,19 @@ 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.jupiter.api.Test; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.fail; class Lambdas01Exercise { @@ -19,13 +25,23 @@ 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) { + if (o1.getAge() > o2.getAge()) + return 1; + else if (o1.getAge() < o2.getAge()) + return -1; + else + return 0; + } + }); - assertArrayEquals(persons, new Person[]{ + assertThat(persons, is(new Person[]{ new Person("name 3", "lastName 3", 20), new Person("name 2", "lastName 1", 30), new Person("name 1", "lastName 2", 40), - }); + })); } @Test @@ -36,10 +52,17 @@ void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; - - // TODO use FluentIterable + final Optional personOptional = + FluentIterable.from(persons) + .firstMatch(new Predicate() { + public boolean apply(Person p) { + return p.getAge() == 30; + } + }); - assertEquals(person, new Person("name 1", "lastName 2", 30)); + if (personOptional.isPresent()) + assertThat(personOptional.get(), is(new Person("name 1", "lastName 2", 30))); + else + fail(""); } -} +} \ No newline at end of file From 8dcb2f09c5e33e6dd1c6909efc7428aac0885475 Mon Sep 17 00:00:00 2001 From: Egor Kozyrev Date: Mon, 3 Apr 2017 13:59:02 +0300 Subject: [PATCH 2/3] reverted changes --- .../part1/exercise/Lambdas01Exercise.java | 41 ++++--------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index 57d8252..fd39fe9 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -1,19 +1,13 @@ 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.jupiter.api.Test; -import java.util.Arrays; -import java.util.Comparator; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; class Lambdas01Exercise { @@ -25,23 +19,13 @@ void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - Arrays.sort(persons, new Comparator() { - @Override - public int compare(Person o1, Person o2) { - if (o1.getAge() > o2.getAge()) - return 1; - else if (o1.getAge() < o2.getAge()) - return -1; - else - return 0; - } - }); + // TODO use Arrays.sort - assertThat(persons, is(new Person[]{ + assertArrayEquals(persons, new Person[]{ new Person("name 3", "lastName 3", 20), new Person("name 2", "lastName 1", 30), new Person("name 1", "lastName 2", 40), - })); + }); } @Test @@ -52,17 +36,10 @@ void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - final Optional personOptional = - FluentIterable.from(persons) - .firstMatch(new Predicate() { - public boolean apply(Person p) { - return p.getAge() == 30; - } - }); + Person person = null; + + // TODO use FluentIterable - if (personOptional.isPresent()) - assertThat(personOptional.get(), is(new Person("name 1", "lastName 2", 30))); - else - fail(""); + assertEquals(person, new Person("name 1", "lastName 2", 30)); } } \ No newline at end of file From 2b3faf2e2c5c0afbd437ee3bce27b633622c7b35 Mon Sep 17 00:00:00 2001 From: Egor Kozyrev Date: Mon, 3 Apr 2017 14:59:03 +0300 Subject: [PATCH 3/3] exercise 2 --- .../part1/exercise/Lambdas02Exercise.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index 8f47d34..83dbcd2 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java @@ -1,13 +1,17 @@ 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.jupiter.api.Test; +import java.util.Arrays; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.fail; class Lambdas02Exercise { @Test @@ -18,13 +22,21 @@ void sortPersonsByAge() { new Person("name 2", "lastName 1", 30) }; - // TODO use Arrays.sort + Arrays.sort(persons, (o1, o2) -> { + if (o1.getAge() > o2.getAge()) + return 1; + else if (o1.getAge() < o2.getAge()) + return -1; + else + return 0; + }); + - assertArrayEquals(persons, new Person[]{ + assertThat(persons, is(new Person[]{ new Person("name 3", "lastName 3", 20), new Person("name 2", "lastName 1", 30), new Person("name 1", "lastName 2", 40), - }); + })); } @Test @@ -35,10 +47,14 @@ void findFirstWithAge30() { new Person("name 2", "lastName 1", 30) ); - Person person = null; + final Optional personOptional = + FluentIterable.from(persons) + .firstMatch(p -> p.getAge() == 30); - // TODO use FluentIterable - assertEquals(person, new Person("name 1", "lastName 2", 30)); + if (personOptional.isPresent()) + assertThat(personOptional.get(), is(new Person("name 1", "lastName 2", 30))); + else + fail(""); } -} +} \ No newline at end of file