less verbose comparing logic

This commit is contained in:
eugenp 2015-01-03 18:12:00 +02:00
parent 081b926939
commit 7a6439c8e5
1 changed files with 9 additions and 1 deletions

View File

@ -63,7 +63,7 @@ public class Java8SortUnitTest {
}
@Test
public final void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
public final void givenCompositionVerbose_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
final Comparator<Human> byName = (h1, h2) -> h1.getName().compareTo(h2.getName());
final Comparator<Human> byAge = (h1, h2) -> Ints.compare(h1.getAge(), h2.getAge());
@ -72,6 +72,14 @@ public class Java8SortUnitTest {
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
}
@Test
public final void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
}
@Test
public final void whenSortingEntitiesByAge_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));