From dbdb32da463d8a59c92f31c30fb02336d184bb28 Mon Sep 17 00:00:00 2001 From: dupirefr Date: Wed, 29 Apr 2020 10:05:02 +0200 Subject: [PATCH 1/3] [BAEL-3981] Code for article * Equality operators * Object#equals method * Objects#equals static method * Comparable interface * Comparator interface * Apache Commons features * Guava features --- core-java-modules/core-java-lang-2/pom.xml | 9 +- .../java/com/baeldung/comparing/Person.java | 217 ++++++++++++++++++ .../ApacheCommonsObjectUtilsUnitTest.java | 59 +++++ .../ComparableInterfaceUnitTest.java | 107 +++++++++ .../ComparatorInterfaceUnitTest.java | 81 +++++++ .../comparing/EqualityOperatorUnitTest.java | 116 ++++++++++ .../comparing/EqualsMethodUnitTest.java | 73 ++++++ .../com/baeldung/comparing/GuavaUnitTest.java | 80 +++++++ .../ObjectsEqualsStaticMethodUnitTest.java | 50 ++++ 9 files changed, 791 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index 5aa80ce3df..449b700560 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -20,7 +20,12 @@ org.apache.commons commons-lang3 - 3.9 + ${commons-lang3.version} + + + com.google.guava + guava + ${guava.version} commons-beanutils @@ -65,6 +70,8 @@ 1.19 3.12.2 1.9.4 + 3.9 + 29.0-jre diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java new file mode 100644 index 0000000000..7c7088a0c7 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java @@ -0,0 +1,217 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Comparator; +import java.util.Objects; + +public class Person { + public static class PersonWithoutEquals { + private String firstName; + private String lastName; + + public PersonWithoutEquals(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + } + + public static class PersonWithEquals { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEquals(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEquals(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + public String firstName() { + return firstName; + } + + public String lastName() { + return lastName; + } + + public LocalDate birthDate() { + return birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEquals that = (PersonWithEquals) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + } + + public static class PersonWithEqualsAndWrongComparable implements Comparable { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEqualsAndWrongComparable(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEqualsAndWrongComparable(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEqualsAndWrongComparable that = (PersonWithEqualsAndWrongComparable) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + + @Override + public int compareTo(PersonWithEqualsAndWrongComparable o) { + return this.lastName.compareTo(o.lastName); + } + } + + public static class PersonWithEqualsAndComparable implements Comparable { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEqualsAndComparable(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEqualsAndComparable(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEqualsAndComparable that = (PersonWithEqualsAndComparable) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + + @Override + public int compareTo(PersonWithEqualsAndComparable o) { + int lastNamesComparison = this.lastName.compareTo(o.lastName); + if (lastNamesComparison == 0) { + int firstNamesComparison = this.firstName.compareTo(o.firstName); + if (firstNamesComparison == 0) { + if (this.birthDate != null && o.birthDate != null) { + return this.birthDate.compareTo(o.birthDate); + } else if (this.birthDate != null) { + return 1; + } else if (o.birthDate != null) { + return -1; + } else { + return 0; + } + } else { + return firstNamesComparison; + } + } else { + return lastNamesComparison; + } + } + } + + public static class PersonWithEqualsAndComparableUsingComparator implements Comparable { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + public String firstName() { + return firstName; + } + + public String lastName() { + return lastName; + } + + public LocalDate birthDate() { + return birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEqualsAndComparableUsingComparator that = (PersonWithEqualsAndComparableUsingComparator) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + + @Override + public int compareTo(PersonWithEqualsAndComparableUsingComparator o) { + return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName) + .thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName) + .thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder())) + .compare(this, o); + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java new file mode 100644 index 0000000000..33b8dcb3fc --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.comparing; + +import org.apache.commons.lang3.ObjectUtils; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ApacheCommonsObjectUtilsUnitTest { + + @Test + void givenTwoStringsWithSameValues_whenApacheCommonsEqualityMethods_thenEqualsTrueNotEqualsFalse() { + String a = new String("Hello!"); + String b = new String("Hello!"); + + assertThat(ObjectUtils.equals(a, b)).isTrue(); + assertThat(ObjectUtils.notEqual(a, b)).isFalse(); + } + + @Test + void givenTwoStringsWithDifferentValues_whenApacheCommonsEqualityMethods_thenEqualsFalseNotEqualsTrue() { + String a = new String("Hello!"); + String b = new String("Hello World!"); + + assertThat(ObjectUtils.equals(a, b)).isFalse(); + assertThat(ObjectUtils.notEqual(a, b)).isTrue(); + } + + @Test + void givenTwoStringsWithConsecutiveValues_whenApacheCommonsCompare_thenNegative() { + String first = new String("Hello!"); + String second = new String("How are you?"); + + assertThat(ObjectUtils.compare(first, second)).isNegative(); + } + + @Test + void givenTwoStringsWithSameValues_whenApacheCommonsEqualityMethods_thenEqualsFalseNotEqualsTrue() { + String first = new String("Hello!"); + String second = new String("Hello!"); + + assertThat(ObjectUtils.compare(first, second)).isZero(); + } + + @Test + void givenTwoStringsWithConsecutiveValues_whenApacheCommonsCompareReversed_thenPositive() { + String first = new String("Hello!"); + String second = new String("How are you?"); + + assertThat(ObjectUtils.compare(second, first)).isPositive(); + } + + @Test + void givenTwoStringsOneNull_whenApacheCommonsCompare_thenPositive() { + String first = new String("Hello!"); + String second = null; + + assertThat(ObjectUtils.compare(first, second, false)).isPositive(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java new file mode 100644 index 0000000000..6e10188852 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java @@ -0,0 +1,107 @@ +package com.baeldung.comparing; + +import org.junit.jupiter.api.Test; + +import java.util.SortedSet; +import java.util.TreeSet; + +import static org.assertj.core.api.Assertions.assertThat; + +class ComparableInterfaceUnitTest { + + @Test + void givenTwoConsecutiveStrings_whenCompareTo_thenNegative() { + String first = "Google"; + String second = "Microsoft"; + + assertThat(first.compareTo(second)).isNegative(); + } + + @Test + void givenTwoEqualsStrings_whenCompareTo_thenZero() { + String first = "Google"; + String second = "Google"; + + assertThat(first.compareTo(second)).isZero(); + } + + @Test + void givenTwoConsecutiveStrings_whenReversedCompareTo_thenPositive() { + String first = "Google"; + String second = "Microsoft"; + + assertThat(second.compareTo(first)).isPositive(); + } + + @Test + void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() { + Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndWrongComparable joe = new Person.PersonWithEqualsAndWrongComparable("Joe", "Portman"); + + assertThat(richard.compareTo(joe)).isNegative(); + } + + @Test + void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenReversedCompareTo_thenZero() { + Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndWrongComparable mike = new Person.PersonWithEqualsAndWrongComparable("Mike", "Jefferson"); + + assertThat(richard.compareTo(mike)).isZero(); + } + + @Test + void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() { + Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndWrongComparable joe = new Person.PersonWithEqualsAndWrongComparable("Joe", "Portman"); + + assertThat(joe.compareTo(richard)).isPositive(); + } + + @Test + void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenSortedSet_thenProblem() { + Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndWrongComparable mike = new Person.PersonWithEqualsAndWrongComparable("Mike", "Jefferson"); + + SortedSet people = new TreeSet<>(); + people.add(richard); + people.add(mike); + + assertThat(people).containsExactly(richard); + } + + @Test + void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() { + Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparable joe = new Person.PersonWithEqualsAndComparable("Joe", "Portman"); + + assertThat(richard.compareTo(joe)).isNegative(); + } + + @Test + void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenReversedCompareTo_thenZero() { + Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparable mike = new Person.PersonWithEqualsAndComparable("Mike", "Jefferson"); + + assertThat(richard.compareTo(mike)).isPositive(); + } + + @Test + void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() { + Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparable joe = new Person.PersonWithEqualsAndComparable("Joe", "Portman"); + + assertThat(joe.compareTo(richard)).isPositive(); + } + + @Test + void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenSortedSet_thenProblem() { + Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparable mike = new Person.PersonWithEqualsAndComparable("Mike", "Jefferson"); + + SortedSet people = new TreeSet<>(); + people.add(richard); + people.add(mike); + + assertThat(people).containsExactly(mike, richard); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java new file mode 100644 index 0000000000..a406b12752 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java @@ -0,0 +1,81 @@ +package com.baeldung.comparing; + +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static org.assertj.core.api.Assertions.assertThat; + +class ComparatorInterfaceUnitTest { + + @Test + void givenListOfTwoPersonWithEqualsAndComparatorByFirstName_whenSort_thenSortedByFirstNames() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals allan = new Person.PersonWithEquals("Allan", "Dale"); + + List people = new ArrayList<>(); + people.add(joe); + people.add(allan); + + Comparator compareByFirstNames = new Comparator() { + @Override + public int compare(Person.PersonWithEquals o1, Person.PersonWithEquals o2) { + return o1.firstName().compareTo(o2.firstName()); + } + }; + people.sort(compareByFirstNames); + + assertThat(people).containsExactly(allan, joe); + } + + @Test + void givenListOfTwoPersonWithEqualsAndComparatorByFirstNameFunctionalStyle_whenSort_thenSortedByFirstNames() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals allan = new Person.PersonWithEquals("Allan", "Dale"); + + List people = new ArrayList<>(); + people.add(joe); + people.add(allan); + + Comparator compareByFirstNames = Comparator.comparing(Person.PersonWithEquals::firstName); + people.sort(compareByFirstNames); + + assertThat(people).containsExactly(allan, joe); + } + + @Test + void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenCompareTo_thenNegative() { + Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparableUsingComparator joe = new Person.PersonWithEqualsAndComparableUsingComparator("Joe", "Portman"); + + assertThat(richard.compareTo(joe)).isNegative(); + } + + @Test + void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenReversedCompareTo_thenZero() { + Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparableUsingComparator mike = new Person.PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson"); + + assertThat(richard.compareTo(mike)).isPositive(); + } + + @Test + void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() { + Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparableUsingComparator joe = new Person.PersonWithEqualsAndComparableUsingComparator("Joe", "Portman"); + + assertThat(joe.compareTo(richard)).isPositive(); + } + + @Test + void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenSortedSet_thenProblem() { + Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + Person.PersonWithEqualsAndComparableUsingComparator mike = new Person.PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson"); + + SortedSet people = new TreeSet<>(); + people.add(richard); + people.add(mike); + + assertThat(people).containsExactly(mike, richard); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java new file mode 100644 index 0000000000..ebcf83ef5b --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java @@ -0,0 +1,116 @@ +package com.baeldung.comparing; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class EqualityOperatorUnitTest { + + @Test + void givenTwoIntsWithSameValues_whenEqualityOperators_thenConsideredSame() { + int a = 1; + int b = 1; + + assertThat(a == b).isTrue(); + assertThat(a != b).isFalse(); + } + + @Test + void givenTwoIntsWithDifferentValues_whenEqualityOperators_thenNotConsideredSame() { + int a = 1; + int b = 2; + + assertThat(a == b).isFalse(); + assertThat(a != b).isTrue(); + } + + @Test + void givenTwoIntsWithSameValuesOneWrapped_whenEqualityOperators_thenConsideredSame() { + int a = 1; + Integer b = new Integer(1); + + assertThat(a == b).isTrue(); + assertThat(a != b).isFalse(); + } + + @Test + void givenTwoIntsWithDifferentValuesOneWrapped_whenEqualityOperators_thenNotConsideredSame() { + int a = 1; + Integer b = new Integer(2); + + assertThat(a == b).isFalse(); + assertThat(a != b).isTrue(); + } + + @Test + void givenTwoIntegersWithSameValues_whenEqualityOperators_thenNotConsideredSame() { + Integer a = new Integer(1); + Integer b = new Integer(1); + + assertThat(a == b).isFalse(); + assertThat(a != b).isTrue(); + } + + @Test + void givenTwoIntegersWithDifferentValues_whenEqualityOperators_thenNotConsideredSame() { + Integer a = new Integer(1); + Integer b = new Integer(2); + + assertThat(a == b).isFalse(); + assertThat(a != b).isTrue(); + } + + @Test + void givenTwoIntegersWithSameReference_whenEqualityOperators_thenConsideredSame() { + Integer a = new Integer(1); + Integer b = a; + + assertThat(a == b).isTrue(); + assertThat(a != b).isFalse(); + } + + @Test + void givenTwoIntegersFromValueOfWithSameValues_whenEqualityOperators_thenConsideredSame() { + Integer a = Integer.valueOf(1); + Integer b = Integer.valueOf(1); + + assertThat(a == b).isTrue(); + assertThat(a != b).isFalse(); + } + + @Test + void givenTwoStringsWithSameValues_whenEqualityOperators_thenNotConsideredSame() { + String a = new String("Hello!"); + String b = new String("Hello!"); + + assertThat(a == b).isFalse(); + assertThat(a != b).isTrue(); + } + + @Test + void givenTwoStringsFromLiteralsWithSameValues_whenEqualityOperators_thenConsideredSame() { + String a = "Hello!"; + String b = "Hello!"; + + assertThat(a == b).isTrue(); + assertThat(a != b).isFalse(); + } + + @Test + void givenTwoNullObjects_whenEqualityOperators_thenConsideredSame() { + Object a = null; + Object b = null; + + assertThat(a == b).isTrue(); + assertThat(a != b).isFalse(); + } + + @Test + void givenTwoObjectsOneNull_whenEqualityOperators_thenNotConsideredSame() { + Object a = null; + Object b = "Hello!"; + + assertThat(a == b).isFalse(); + assertThat(a != b).isTrue(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java new file mode 100644 index 0000000000..4775bc3de1 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.comparing; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class EqualsMethodUnitTest { + + @Test + void givenTwoIntegersWithSameValue_whenEquals_thenTrue() { + Integer a = new Integer(1); + Integer b = new Integer(1); + + assertThat(a.equals(b)).isTrue(); + } + + @Test + void givenTwoStringsWithSameValue_whenEquals_thenTrue() { + String a = new String("Hello!"); + String b = new String("Hello!"); + + assertThat(a.equals(b)).isTrue(); + } + + @Test + void givenTwoStringsWithDifferentValue_whenEquals_thenFalse() { + String a = new String("Hello!"); + String b = new String("Hello World!"); + + assertThat(a.equals(b)).isFalse(); + } + + @Test + void givenTwoObjectsFirstNull_whenEquals_thenNullPointerExceptionThrown() { + Object a = null; + Object b = new String("Hello!"); + + assertThrows(NullPointerException.class, () -> a.equals(b)); + } + + @Test + void givenTwoObjectsSecondNull_whenEquals_thenFalse() { + Object a = new String("Hello!"); + Object b = null; + + assertThat(a.equals(b)).isFalse(); + } + + @Test + void givenTwoPersonWithoutEqualsWithSameNames_whenEquals_thenFalse() { + Person.PersonWithoutEquals joe = new Person.PersonWithoutEquals("Joe", "Portman"); + Person.PersonWithoutEquals joeAgain = new Person.PersonWithoutEquals("Joe", "Portman"); + + assertThat(joe.equals(joeAgain)).isFalse(); + } + + @Test + void givenTwoPersonWithEqualsWithSameNames_whenEquals_thenTrue() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals joeAgain = new Person.PersonWithEquals("Joe", "Portman"); + + assertThat(joe.equals(joeAgain)).isTrue(); + } + + @Test + void givenTwoPersonWittEqualsWithDifferentNames_whenEquals_thenFalse() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman"); + + assertThat(joe.equals(nathalie)).isFalse(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java new file mode 100644 index 0000000000..0a4d0dd8d3 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.comparing; + +import com.google.common.base.Objects; +import com.google.common.base.Strings; +import com.google.common.collect.ComparisonChain; +import com.google.common.primitives.Booleans; +import com.google.common.primitives.Bytes; +import com.google.common.primitives.Ints; +import com.google.common.primitives.Shorts; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class GuavaUnitTest { + + @Nested + class ObjectsEqualMethod { + @Test + void givenTwoStringsWithSameValues_whenObjectsEqualMethods_thenTrue() { + String a = new String("Hello!"); + String b = new String("Hello!"); + + assertThat(Objects.equal(a, b)).isTrue(); + } + + @Test + void givenTwoStringsWithDifferentValues_whenObjectsEqualMethods_thenFalse() { + String a = new String("Hello!"); + String b = new String("Hello World!"); + + assertThat(Objects.equal(a, b)).isFalse(); + } + } + + @Nested + class ComparisonMethods { + @Test + void givenTwoIntsWithConsecutiveValues_whenIntsCompareMethods_thenNegative() { + int first = 1; + int second = 2; + assertThat(Ints.compare(first, second)).isNegative(); + } + + @Test + void givenTwoIntsWithSameValues_whenIntsCompareMethods_thenZero() { + int first = 1; + int second = 1; + + assertThat(Ints.compare(first, second)).isZero(); + } + + @Test + void givenTwoIntsWithConsecutiveValues_whenIntsCompareMethodsReversed_thenNegative() { + int first = 1; + int second = 2; + + assertThat(Ints.compare(second, first)).isPositive(); + } + } + + @Nested + class ComparisonChainClass { + @Test + void givenTwoPersonWithEquals_whenComparisonChainByLastNameThenFirstName_thenSortedJoeFirstAndNathalieSecond() { + Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman"); + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + + int comparisonResult = ComparisonChain.start() + .compare(nathalie.lastName(), joe.lastName()) + .compare(nathalie.firstName(), joe.firstName()) + .result(); + + assertThat(comparisonResult).isPositive(); + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java new file mode 100644 index 0000000000..c26cb4e08c --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.comparing; + +import org.junit.jupiter.api.Test; + +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +class ObjectsEqualsStaticMethodUnitTest { + + @Test + void givenTwoPersonWithEqualsWithSameNames_whenObjectsEquals_thenTrue() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals joeAgain = new Person.PersonWithEquals("Joe", "Portman"); + + assertThat(Objects.equals(joe, joeAgain)).isTrue(); + } + + @Test + void givenTwoPersonWithEqualsWithDifferentNames_whenObjectsEquals_thenFalse() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman"); + + assertThat(Objects.equals(joe, nathalie)).isFalse(); + } + + @Test + void givenTwoPersonWithEqualsFirstNull_whenObjectsEquals_thenFalse() { + Person.PersonWithEquals nobody = null; + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + + assertThat(Objects.equals(nobody, joe)).isFalse(); + } + + @Test + void givenTwoObjectsSecondtNull_whenObjectsEquals_thenFalse() { + Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + Person.PersonWithEquals nobody = null; + + assertThat(Objects.equals(joe, nobody)).isFalse(); + } + + @Test + void givenTwoObjectsNull_whenObjectsEquals_thenTrue() { + Person.PersonWithEquals nobody = null; + Person.PersonWithEquals nobodyAgain = null; + + assertThat(Objects.equals(nobody, nobodyAgain)).isTrue(); + } +} From 24a88c11ac6eb27cb12249a5e7be574894e1168b Mon Sep 17 00:00:00 2001 From: dupirefr Date: Wed, 13 May 2020 19:17:02 +0200 Subject: [PATCH 2/3] [BAEL-3981] Moved inner classes to upper level --- .../java/com/baeldung/comparing/Person.java | 217 ------------------ .../baeldung/comparing/PersonWithEquals.java | 51 ++++ .../PersonWithEqualsAndComparable.java | 62 +++++ ...ithEqualsAndComparableUsingComparator.java | 60 +++++ .../PersonWithEqualsAndWrongComparable.java | 44 ++++ .../comparing/PersonWithoutEquals.java | 11 + .../ComparableInterfaceUnitTest.java | 36 +-- .../ComparatorInterfaceUnitTest.java | 36 +-- .../comparing/EqualsMethodUnitTest.java | 12 +- .../com/baeldung/comparing/GuavaUnitTest.java | 11 +- .../ObjectsEqualsStaticMethodUnitTest.java | 20 +- 11 files changed, 282 insertions(+), 278 deletions(-) delete mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java deleted file mode 100644 index 7c7088a0c7..0000000000 --- a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java +++ /dev/null @@ -1,217 +0,0 @@ -package com.baeldung.comparing; - -import java.time.LocalDate; -import java.util.Comparator; -import java.util.Objects; - -public class Person { - public static class PersonWithoutEquals { - private String firstName; - private String lastName; - - public PersonWithoutEquals(String firstName, String lastName) { - this.firstName = firstName; - this.lastName = lastName; - } - } - - public static class PersonWithEquals { - private String firstName; - private String lastName; - private LocalDate birthDate; - - public PersonWithEquals(String firstName, String lastName) { - if (firstName == null || lastName == null) { - throw new NullPointerException("Names can't be null"); - } - this.firstName = firstName; - this.lastName = lastName; - } - - public PersonWithEquals(String firstName, String lastName, LocalDate birthDate) { - this(firstName, lastName); - - this.birthDate = birthDate; - } - - public String firstName() { - return firstName; - } - - public String lastName() { - return lastName; - } - - public LocalDate birthDate() { - return birthDate; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - PersonWithEquals that = (PersonWithEquals) o; - return firstName.equals(that.firstName) && - lastName.equals(that.lastName) && - Objects.equals(birthDate, that.birthDate); - } - - @Override - public int hashCode() { - return Objects.hash(firstName, lastName); - } - } - - public static class PersonWithEqualsAndWrongComparable implements Comparable { - private String firstName; - private String lastName; - private LocalDate birthDate; - - public PersonWithEqualsAndWrongComparable(String firstName, String lastName) { - if (firstName == null || lastName == null) { - throw new NullPointerException("Names can't be null"); - } - this.firstName = firstName; - this.lastName = lastName; - } - - public PersonWithEqualsAndWrongComparable(String firstName, String lastName, LocalDate birthDate) { - this(firstName, lastName); - - this.birthDate = birthDate; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - PersonWithEqualsAndWrongComparable that = (PersonWithEqualsAndWrongComparable) o; - return firstName.equals(that.firstName) && - lastName.equals(that.lastName) && - Objects.equals(birthDate, that.birthDate); - } - - @Override - public int hashCode() { - return Objects.hash(firstName, lastName); - } - - @Override - public int compareTo(PersonWithEqualsAndWrongComparable o) { - return this.lastName.compareTo(o.lastName); - } - } - - public static class PersonWithEqualsAndComparable implements Comparable { - private String firstName; - private String lastName; - private LocalDate birthDate; - - public PersonWithEqualsAndComparable(String firstName, String lastName) { - if (firstName == null || lastName == null) { - throw new NullPointerException("Names can't be null"); - } - this.firstName = firstName; - this.lastName = lastName; - } - - public PersonWithEqualsAndComparable(String firstName, String lastName, LocalDate birthDate) { - this(firstName, lastName); - - this.birthDate = birthDate; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - PersonWithEqualsAndComparable that = (PersonWithEqualsAndComparable) o; - return firstName.equals(that.firstName) && - lastName.equals(that.lastName) && - Objects.equals(birthDate, that.birthDate); - } - - @Override - public int hashCode() { - return Objects.hash(firstName, lastName); - } - - @Override - public int compareTo(PersonWithEqualsAndComparable o) { - int lastNamesComparison = this.lastName.compareTo(o.lastName); - if (lastNamesComparison == 0) { - int firstNamesComparison = this.firstName.compareTo(o.firstName); - if (firstNamesComparison == 0) { - if (this.birthDate != null && o.birthDate != null) { - return this.birthDate.compareTo(o.birthDate); - } else if (this.birthDate != null) { - return 1; - } else if (o.birthDate != null) { - return -1; - } else { - return 0; - } - } else { - return firstNamesComparison; - } - } else { - return lastNamesComparison; - } - } - } - - public static class PersonWithEqualsAndComparableUsingComparator implements Comparable { - private String firstName; - private String lastName; - private LocalDate birthDate; - - public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName) { - if (firstName == null || lastName == null) { - throw new NullPointerException("Names can't be null"); - } - this.firstName = firstName; - this.lastName = lastName; - } - - public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName, LocalDate birthDate) { - this(firstName, lastName); - - this.birthDate = birthDate; - } - - public String firstName() { - return firstName; - } - - public String lastName() { - return lastName; - } - - public LocalDate birthDate() { - return birthDate; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - PersonWithEqualsAndComparableUsingComparator that = (PersonWithEqualsAndComparableUsingComparator) o; - return firstName.equals(that.firstName) && - lastName.equals(that.lastName) && - Objects.equals(birthDate, that.birthDate); - } - - @Override - public int hashCode() { - return Objects.hash(firstName, lastName); - } - - @Override - public int compareTo(PersonWithEqualsAndComparableUsingComparator o) { - return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName) - .thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName) - .thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder())) - .compare(this, o); - } - } -} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java new file mode 100644 index 0000000000..e3a61fc05a --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java @@ -0,0 +1,51 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Objects; + +public class PersonWithEquals { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEquals(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEquals(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + public String firstName() { + return firstName; + } + + public String lastName() { + return lastName; + } + + public LocalDate birthDate() { + return birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEquals that = (PersonWithEquals) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java new file mode 100644 index 0000000000..5611ce8a09 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java @@ -0,0 +1,62 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Objects; + +public class PersonWithEqualsAndComparable implements Comparable { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEqualsAndComparable(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEqualsAndComparable(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEqualsAndComparable that = (PersonWithEqualsAndComparable) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + + @Override + public int compareTo(PersonWithEqualsAndComparable o) { + int lastNamesComparison = this.lastName.compareTo(o.lastName); + if (lastNamesComparison == 0) { + int firstNamesComparison = this.firstName.compareTo(o.firstName); + if (firstNamesComparison == 0) { + if (this.birthDate != null && o.birthDate != null) { + return this.birthDate.compareTo(o.birthDate); + } else if (this.birthDate != null) { + return 1; + } else if (o.birthDate != null) { + return -1; + } else { + return 0; + } + } else { + return firstNamesComparison; + } + } else { + return lastNamesComparison; + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java new file mode 100644 index 0000000000..ed322cb353 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java @@ -0,0 +1,60 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Comparator; +import java.util.Objects; + +public class PersonWithEqualsAndComparableUsingComparator implements Comparable { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + public String firstName() { + return firstName; + } + + public String lastName() { + return lastName; + } + + public LocalDate birthDate() { + return birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEqualsAndComparableUsingComparator that = (PersonWithEqualsAndComparableUsingComparator) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + + @Override + public int compareTo(PersonWithEqualsAndComparableUsingComparator o) { + return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName) + .thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName) + .thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder())) + .compare(this, o); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java new file mode 100644 index 0000000000..e0bdaa413a --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java @@ -0,0 +1,44 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Objects; + +public class PersonWithEqualsAndWrongComparable implements Comparable { + private String firstName; + private String lastName; + private LocalDate birthDate; + + public PersonWithEqualsAndWrongComparable(String firstName, String lastName) { + if (firstName == null || lastName == null) { + throw new NullPointerException("Names can't be null"); + } + this.firstName = firstName; + this.lastName = lastName; + } + + public PersonWithEqualsAndWrongComparable(String firstName, String lastName, LocalDate birthDate) { + this(firstName, lastName); + + this.birthDate = birthDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonWithEqualsAndWrongComparable that = (PersonWithEqualsAndWrongComparable) o; + return firstName.equals(that.firstName) && + lastName.equals(that.lastName) && + Objects.equals(birthDate, that.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName); + } + + @Override + public int compareTo(PersonWithEqualsAndWrongComparable o) { + return this.lastName.compareTo(o.lastName); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java new file mode 100644 index 0000000000..bb4c6b958b --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java @@ -0,0 +1,11 @@ +package com.baeldung.comparing; + +public class PersonWithoutEquals { + private String firstName; + private String lastName; + + public PersonWithoutEquals(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java index 6e10188852..281c4a0201 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java @@ -35,34 +35,34 @@ class ComparableInterfaceUnitTest { @Test void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() { - Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndWrongComparable joe = new Person.PersonWithEqualsAndWrongComparable("Joe", "Portman"); + PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + PersonWithEqualsAndWrongComparable joe = new PersonWithEqualsAndWrongComparable("Joe", "Portman"); assertThat(richard.compareTo(joe)).isNegative(); } @Test void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenReversedCompareTo_thenZero() { - Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndWrongComparable mike = new Person.PersonWithEqualsAndWrongComparable("Mike", "Jefferson"); + PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + PersonWithEqualsAndWrongComparable mike = new PersonWithEqualsAndWrongComparable("Mike", "Jefferson"); assertThat(richard.compareTo(mike)).isZero(); } @Test void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() { - Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndWrongComparable joe = new Person.PersonWithEqualsAndWrongComparable("Joe", "Portman"); + PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + PersonWithEqualsAndWrongComparable joe = new PersonWithEqualsAndWrongComparable("Joe", "Portman"); assertThat(joe.compareTo(richard)).isPositive(); } @Test void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenSortedSet_thenProblem() { - Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndWrongComparable mike = new Person.PersonWithEqualsAndWrongComparable("Mike", "Jefferson"); + PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson"); + PersonWithEqualsAndWrongComparable mike = new PersonWithEqualsAndWrongComparable("Mike", "Jefferson"); - SortedSet people = new TreeSet<>(); + SortedSet people = new TreeSet<>(); people.add(richard); people.add(mike); @@ -71,34 +71,34 @@ class ComparableInterfaceUnitTest { @Test void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() { - Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparable joe = new Person.PersonWithEqualsAndComparable("Joe", "Portman"); + PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson"); + PersonWithEqualsAndComparable joe = new PersonWithEqualsAndComparable("Joe", "Portman"); assertThat(richard.compareTo(joe)).isNegative(); } @Test void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenReversedCompareTo_thenZero() { - Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparable mike = new Person.PersonWithEqualsAndComparable("Mike", "Jefferson"); + PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson"); + PersonWithEqualsAndComparable mike = new PersonWithEqualsAndComparable("Mike", "Jefferson"); assertThat(richard.compareTo(mike)).isPositive(); } @Test void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() { - Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparable joe = new Person.PersonWithEqualsAndComparable("Joe", "Portman"); + PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson"); + PersonWithEqualsAndComparable joe = new PersonWithEqualsAndComparable("Joe", "Portman"); assertThat(joe.compareTo(richard)).isPositive(); } @Test void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenSortedSet_thenProblem() { - Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparable mike = new Person.PersonWithEqualsAndComparable("Mike", "Jefferson"); + PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson"); + PersonWithEqualsAndComparable mike = new PersonWithEqualsAndComparable("Mike", "Jefferson"); - SortedSet people = new TreeSet<>(); + SortedSet people = new TreeSet<>(); people.add(richard); people.add(mike); diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java index a406b12752..769ae60bed 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java @@ -10,16 +10,16 @@ class ComparatorInterfaceUnitTest { @Test void givenListOfTwoPersonWithEqualsAndComparatorByFirstName_whenSort_thenSortedByFirstNames() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals allan = new Person.PersonWithEquals("Allan", "Dale"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals allan = new PersonWithEquals("Allan", "Dale"); - List people = new ArrayList<>(); + List people = new ArrayList<>(); people.add(joe); people.add(allan); - Comparator compareByFirstNames = new Comparator() { + Comparator compareByFirstNames = new Comparator() { @Override - public int compare(Person.PersonWithEquals o1, Person.PersonWithEquals o2) { + public int compare(PersonWithEquals o1, PersonWithEquals o2) { return o1.firstName().compareTo(o2.firstName()); } }; @@ -30,14 +30,14 @@ class ComparatorInterfaceUnitTest { @Test void givenListOfTwoPersonWithEqualsAndComparatorByFirstNameFunctionalStyle_whenSort_thenSortedByFirstNames() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals allan = new Person.PersonWithEquals("Allan", "Dale"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals allan = new PersonWithEquals("Allan", "Dale"); - List people = new ArrayList<>(); + List people = new ArrayList<>(); people.add(joe); people.add(allan); - Comparator compareByFirstNames = Comparator.comparing(Person.PersonWithEquals::firstName); + Comparator compareByFirstNames = Comparator.comparing(PersonWithEquals::firstName); people.sort(compareByFirstNames); assertThat(people).containsExactly(allan, joe); @@ -45,34 +45,34 @@ class ComparatorInterfaceUnitTest { @Test void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenCompareTo_thenNegative() { - Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparableUsingComparator joe = new Person.PersonWithEqualsAndComparableUsingComparator("Joe", "Portman"); + PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + PersonWithEqualsAndComparableUsingComparator joe = new PersonWithEqualsAndComparableUsingComparator("Joe", "Portman"); assertThat(richard.compareTo(joe)).isNegative(); } @Test void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenReversedCompareTo_thenZero() { - Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparableUsingComparator mike = new Person.PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson"); + PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + PersonWithEqualsAndComparableUsingComparator mike = new PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson"); assertThat(richard.compareTo(mike)).isPositive(); } @Test void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() { - Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparableUsingComparator joe = new Person.PersonWithEqualsAndComparableUsingComparator("Joe", "Portman"); + PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + PersonWithEqualsAndComparableUsingComparator joe = new PersonWithEqualsAndComparableUsingComparator("Joe", "Portman"); assertThat(joe.compareTo(richard)).isPositive(); } @Test void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenSortedSet_thenProblem() { - Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); - Person.PersonWithEqualsAndComparableUsingComparator mike = new Person.PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson"); + PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson"); + PersonWithEqualsAndComparableUsingComparator mike = new PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson"); - SortedSet people = new TreeSet<>(); + SortedSet people = new TreeSet<>(); people.add(richard); people.add(mike); diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java index 4775bc3de1..6ae243c0ff 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java @@ -49,24 +49,24 @@ class EqualsMethodUnitTest { @Test void givenTwoPersonWithoutEqualsWithSameNames_whenEquals_thenFalse() { - Person.PersonWithoutEquals joe = new Person.PersonWithoutEquals("Joe", "Portman"); - Person.PersonWithoutEquals joeAgain = new Person.PersonWithoutEquals("Joe", "Portman"); + PersonWithoutEquals joe = new PersonWithoutEquals("Joe", "Portman"); + PersonWithoutEquals joeAgain = new PersonWithoutEquals("Joe", "Portman"); assertThat(joe.equals(joeAgain)).isFalse(); } @Test void givenTwoPersonWithEqualsWithSameNames_whenEquals_thenTrue() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals joeAgain = new Person.PersonWithEquals("Joe", "Portman"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals joeAgain = new PersonWithEquals("Joe", "Portman"); assertThat(joe.equals(joeAgain)).isTrue(); } @Test void givenTwoPersonWittEqualsWithDifferentNames_whenEquals_thenFalse() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman"); assertThat(joe.equals(nathalie)).isFalse(); } diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java index 0a4d0dd8d3..42d3a606a0 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java @@ -1,18 +1,11 @@ package com.baeldung.comparing; import com.google.common.base.Objects; -import com.google.common.base.Strings; import com.google.common.collect.ComparisonChain; -import com.google.common.primitives.Booleans; -import com.google.common.primitives.Bytes; import com.google.common.primitives.Ints; -import com.google.common.primitives.Shorts; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; class GuavaUnitTest { @@ -66,8 +59,8 @@ class GuavaUnitTest { class ComparisonChainClass { @Test void givenTwoPersonWithEquals_whenComparisonChainByLastNameThenFirstName_thenSortedJoeFirstAndNathalieSecond() { - Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman"); - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); int comparisonResult = ComparisonChain.start() .compare(nathalie.lastName(), joe.lastName()) diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java index c26cb4e08c..0e24305d87 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java @@ -10,40 +10,40 @@ class ObjectsEqualsStaticMethodUnitTest { @Test void givenTwoPersonWithEqualsWithSameNames_whenObjectsEquals_thenTrue() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals joeAgain = new Person.PersonWithEquals("Joe", "Portman"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals joeAgain = new PersonWithEquals("Joe", "Portman"); assertThat(Objects.equals(joe, joeAgain)).isTrue(); } @Test void givenTwoPersonWithEqualsWithDifferentNames_whenObjectsEquals_thenFalse() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman"); + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman"); assertThat(Objects.equals(joe, nathalie)).isFalse(); } @Test void givenTwoPersonWithEqualsFirstNull_whenObjectsEquals_thenFalse() { - Person.PersonWithEquals nobody = null; - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); + PersonWithEquals nobody = null; + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); assertThat(Objects.equals(nobody, joe)).isFalse(); } @Test void givenTwoObjectsSecondtNull_whenObjectsEquals_thenFalse() { - Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman"); - Person.PersonWithEquals nobody = null; + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals nobody = null; assertThat(Objects.equals(joe, nobody)).isFalse(); } @Test void givenTwoObjectsNull_whenObjectsEquals_thenTrue() { - Person.PersonWithEquals nobody = null; - Person.PersonWithEquals nobodyAgain = null; + PersonWithEquals nobody = null; + PersonWithEquals nobodyAgain = null; assertThat(Objects.equals(nobody, nobodyAgain)).isTrue(); } From a990d574074ca020cd1cbb1ae97668e5bfb76537 Mon Sep 17 00:00:00 2001 From: dupirefr Date: Wed, 20 May 2020 23:33:04 +0200 Subject: [PATCH 3/3] [BAEL-3981] Fixes from Josh's review * Upgraded Apache Commons to 3.10 * Replaced Nathalie by Natalie --- core-java-modules/core-java-lang-2/pom.xml | 2 +- .../java/com/baeldung/comparing/EqualsMethodUnitTest.java | 4 ++-- .../test/java/com/baeldung/comparing/GuavaUnitTest.java | 8 ++++---- .../comparing/ObjectsEqualsStaticMethodUnitTest.java | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index 449b700560..21a63d8091 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -70,7 +70,7 @@ 1.19 3.12.2 1.9.4 - 3.9 + 3.10 29.0-jre diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java index 6ae243c0ff..a69ac38916 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java @@ -66,8 +66,8 @@ class EqualsMethodUnitTest { @Test void givenTwoPersonWittEqualsWithDifferentNames_whenEquals_thenFalse() { PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); - PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman"); + PersonWithEquals natalie = new PersonWithEquals("Natalie", "Portman"); - assertThat(joe.equals(nathalie)).isFalse(); + assertThat(joe.equals(natalie)).isFalse(); } } diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java index 42d3a606a0..5c8591e134 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java @@ -58,13 +58,13 @@ class GuavaUnitTest { @Nested class ComparisonChainClass { @Test - void givenTwoPersonWithEquals_whenComparisonChainByLastNameThenFirstName_thenSortedJoeFirstAndNathalieSecond() { - PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman"); + void givenTwoPersonWithEquals_whenComparisonChainByLastNameThenFirstName_thenSortedJoeFirstAndNatalieSecond() { + PersonWithEquals natalie = new PersonWithEquals("Natalie", "Portman"); PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); int comparisonResult = ComparisonChain.start() - .compare(nathalie.lastName(), joe.lastName()) - .compare(nathalie.firstName(), joe.firstName()) + .compare(natalie.lastName(), joe.lastName()) + .compare(natalie.firstName(), joe.firstName()) .result(); assertThat(comparisonResult).isPositive(); diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java index 0e24305d87..5ac89da2be 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java @@ -19,9 +19,9 @@ class ObjectsEqualsStaticMethodUnitTest { @Test void givenTwoPersonWithEqualsWithDifferentNames_whenObjectsEquals_thenFalse() { PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); - PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman"); + PersonWithEquals natalie = new PersonWithEquals("Natalie", "Portman"); - assertThat(Objects.equals(joe, nathalie)).isFalse(); + assertThat(Objects.equals(joe, natalie)).isFalse(); } @Test