diff --git a/build.gradle b/build.gradle index 145d1e4..cdbed5f 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,6 @@ dependencies { compileOnly 'org.projectlombok:lombok:+', 'edu.washington.cs.types.checker:checker-framework:+' - testCompile 'org.junit.jupiter:junit-jupiter-api:+', + testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2', 'org.hamcrest:java-hamcrest:+' } diff --git a/src/test/java/lambda/part1/example/Lambdas01.java b/src/test/java/lambda/part1/example/Lambdas01.java index 5537ffa..02b16eb 100644 --- a/src/test/java/lambda/part1/example/Lambdas01.java +++ b/src/test/java/lambda/part1/example/Lambdas01.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableList; import data.Person; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.Arrays; import java.util.Comparator; @@ -19,8 +20,8 @@ // JSR-335 Lambda Expressions for the Java Programming Language // https://github.com/Vyacheslav-Lapin/lambda - -class Lambdas01 { +//@ExtendWith() +class Lambdas01{ @Test void sortPersons() { diff --git a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java index 14cfc0d..d1885db 100644 --- a/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java +++ b/src/test/java/lambda/part1/exercise/Lambdas01Exercise.java @@ -1,13 +1,21 @@ 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.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; class Lambdas01Exercise { @@ -19,7 +27,20 @@ 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[]{ new Person("name 3", "lastName 3", 20), @@ -38,8 +59,20 @@ void findFirstWithAge30() { Person person = null; - // TODO use FluentIterable + final Optional optional = FluentIterable.from(persons) + .firstMatch(new Predicate() { + @Override + public boolean apply(Person p) { + return p.getAge() == 30; + } + }); - assertEquals(person, new Person("name 1", "lastName 2", 30)); +// assertEquals(person, new Person("name 1", "lastName 2", 30)); + if(optional.isPresent()){ + assertThat(optional.get(),is(new Person("name 1", "lastName 2", 30))); + } + else { + fail(""); + } } } diff --git a/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java b/src/test/java/lambda/part1/exercise/Lambdas02Exercise.java index 8f47d34..f82be58 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.jupiter.api.Test; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -18,7 +22,7 @@ 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,8 +41,13 @@ void findFirstWithAge30() { Person person = null; - // TODO use FluentIterable + Optional optional = FluentIterable.from(persons) + .firstMatch(p -> p.getAge() == 30); - assertEquals(person, new Person("name 1", "lastName 2", 30)); + if(optional.isPresent()){ + optional.get().print(); + } + + assertEquals(optional.get(), new Person("name 1", "lastName 2", 30)); } } diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index 75b1d82..e25be37 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import java.util.function.BiFunction; +import java.util.function.BiPredicate; import java.util.function.Function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -13,35 +14,36 @@ class ArrowNotationExercise { @Test void getAge() { // Person -> Integer - final Function getAge = null; // TODO + final Function getAge = p -> p.getAge(); assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); } @Test void compareAges() { - // TODO use BiPredicate + final BiPredicate compareAges = (p1, p2) -> p1.getAge() == p2.getAge(); // compareAges: (Person, Person) -> boolean - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); } - // TODO // getFullName: Person -> String + private static final Function getFullName = person -> + person.getFirstName() + " " + person.getLastName(); - // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // + private BiFunction ageOfPersonWithTheLongestFullName(Function f){ + return (p1, p2) -> f.apply(p1).length() > f.apply(p2).length() ? p1.getAge() : p2.getAge(); + } @Test void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = null; // TODO + final Function getFullName = ArrowNotationExercise.getFullName; // (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/part3/exercise/Mapping.java b/src/test/java/lambda/part3/exercise/Mapping.java index 308faa8..03252cc 100644 --- a/src/test/java/lambda/part3/exercise/Mapping.java +++ b/src/test/java/lambda/part3/exercise/Mapping.java @@ -29,8 +29,12 @@ public List getList() { // [T] -> (T -> R) -> [R] // [T1, T2, T3] -> (T -> R) -> [R1, R2, R3] public MapHelper map(Function f) { - // TODO - throw new UnsupportedOperationException(); + final List res = new ArrayList<>(); + for (T t : list) { + res.add(f.apply(t)); + } + + return new MapHelper(res); } // [T] -> (T -> [R]) -> [R] @@ -47,6 +51,15 @@ public MapHelper flatMap(Function> f) { } } + private List ageOneYear(List jobHistory) { + return new MapHelper<>(jobHistory).map(e -> e.withDuration(e.getDuration() + 1)).getList(); + } + + private List replaceQa(List jobHistory) { + return new MapHelper<>(jobHistory) + .map(e -> e.getPosition().equals("qa") ? e.withPosition("QA") : e).getList(); + } + @Test void mapping() { final List employees = @@ -73,12 +86,10 @@ void mapping() { final List mappedEmployees = new MapHelper<>(employees) - /* - .map(TODO) // change name to John .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) - .map(TODO) // add 1 year to experience duration .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) - .map(TODO) // replace qa with QA - * */ - .getList(); + .map(employee -> employee.withPerson(employee.getPerson().withFirstName("John"))) + .map(employee -> employee.withJobHistory(ageOneYear(employee.getJobHistory()))) + .map(employee -> employee.withJobHistory(replaceQa(employee.getJobHistory()))) + .getList(); final List expectedResult = Arrays.asList( @@ -108,21 +119,28 @@ void mapping() { private static class LazyMapHelper { + private final List list; + private final Function function; + LazyMapHelper(List list, Function function) { + this.list = list; + this.function = function; } static LazyMapHelper from(List list) { return new LazyMapHelper<>(list, Function.identity()); } - List force() { - // TODO - throw new UnsupportedOperationException(); + public List force() { + final List res = new ArrayList<>(); + for(T t : list){ + res.add(function.apply(t)); + } + return res; } public LazyMapHelper map(Function f) { - // TODO - throw new UnsupportedOperationException(); + return new LazyMapHelper<>(list,function.andThen(f)); } } @@ -163,7 +181,6 @@ LazyFlatMapHelper flatMap(Function> f) { } - @Test void lazy_mapping() { final List employees = @@ -190,12 +207,10 @@ void lazy_mapping() { final List mappedEmployees = LazyMapHelper.from(employees) - /* - .map(TODO) // change name to John - .map(TODO) // add 1 year to experience duration - .map(TODO) // replace qa with QA - * */ - .force(); + .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) + .map(e -> e.withJobHistory(ageOneYear(e.getJobHistory()))) + .map(e -> e.withJobHistory(replaceQa(e.getJobHistory()))) + .force(); final List expectedResult = Arrays.asList(