From 09bebd5351a9f9c56fdd0a3cc8066c61dd375eea Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 31 Jan 2017 15:47:53 +0100 Subject: [PATCH 1/2] BAEL-638 Guava Ordering --- .../org/baeldung/guava/GuavaOrderingTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java diff --git a/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java b/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java new file mode 100644 index 0000000000..aec9c1e393 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java @@ -0,0 +1,91 @@ +package org.baeldung.guava; + +import com.google.common.base.Function; +import com.google.common.collect.Ordering; +import com.google.common.primitives.Ints; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class GuavaOrderingTest { + @Test + public void givenListOfIntegers_whenCreateNaturalOrderOrdering_shouldSortProperly() { + //given + List integers = Arrays.asList(3, 2, 1); + + //when + integers.sort(Ordering.natural()); + + //then + assertEquals(Arrays.asList(1, 2, 3), integers); + } + + @Test + public void givenListOfPersonObject_whenSortedUsingCustomOrdering_shouldSortProperly() { + //given + List persons = Arrays.asList(new Person("Michael", 10), new Person("Alice", 3)); + Ordering orderingByName = new Ordering() { + @Override + public int compare(Person p1, Person p2) { + return Ints.compare(p1.age, p2.age); + } + }; + + //when + persons.sort(orderingByName); + + //then + assertEquals(Arrays.asList(new Person("Alice", 3), new Person("Michael", 10)), persons); + } + + @Test + public void givenListOfPersonObject_whenSortedUsingChainedOrdering_shouldSortPropely() { + //given + List persons = Arrays.asList(new Person("Michael", 10), new Person("Alice", 3), new Person("Thomas", null)); + Ordering ordering = Ordering.natural().nullsFirst().onResultOf(new Function() { + @Override + public Comparable apply(Person person) { + return person.age; + } + }); + + //when + persons.sort(ordering); + + //then + assertEquals(Arrays.asList(new Person("Thomas", null), new Person("Alice", 3), new Person("Michael", 10)), persons); + } + + + class Person { + private final String name; + private final Integer age; + + private Person(String name, Integer age) { + this.name = name; + this.age = age; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Person person = (Person) o; + + if (name != null ? !name.equals(person.name) : person.name != null) return false; + return age != null ? age.equals(person.age) : person.age == null; + + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (age != null ? age.hashCode() : 0); + return result; + } + } +} From 352b466bce5f3a2d6eb588f5be9ebdc603919f6e Mon Sep 17 00:00:00 2001 From: Predrag Maric Date: Wed, 1 Feb 2017 01:34:33 +0100 Subject: [PATCH 2/2] BAEL-638 Fixed one variable name --- guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java b/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java index aec9c1e393..5ecf4f048d 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaOrderingTest.java @@ -27,7 +27,7 @@ public class GuavaOrderingTest { public void givenListOfPersonObject_whenSortedUsingCustomOrdering_shouldSortProperly() { //given List persons = Arrays.asList(new Person("Michael", 10), new Person("Alice", 3)); - Ordering orderingByName = new Ordering() { + Ordering orderingByAge = new Ordering() { @Override public int compare(Person p1, Person p2) { return Ints.compare(p1.age, p2.age); @@ -35,7 +35,7 @@ public class GuavaOrderingTest { }; //when - persons.sort(orderingByName); + persons.sort(orderingByAge); //then assertEquals(Arrays.asList(new Person("Alice", 3), new Person("Michael", 10)), persons);