Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/test/java/lambda/part1/exercise/Lambdas01Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ void findFirstWithAge30() {

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
}
}
34 changes: 25 additions & 9 deletions src/test/java/lambda/part1/exercise/Lambdas02Exercise.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -35,10 +47,14 @@ void findFirstWithAge30() {
new Person("name 2", "lastName 1", 30)
);

Person person = null;
final Optional<Person> 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("");
}
}
}