[BAEL-3981] Code for article

* Equality operators

* Object#equals method

* Objects#equals static method

* Comparable interface

* Comparator interface

* Apache Commons features

* Guava features
This commit is contained in:
dupirefr
2020-04-29 10:05:02 +02:00
parent 49092634a3
commit d1e64fb5db
9 changed files with 791 additions and 1 deletions

View File

@@ -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();
}
}

View File

@@ -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<Person.PersonWithEqualsAndWrongComparable> 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<Person.PersonWithEqualsAndComparable> people = new TreeSet<>();
people.add(richard);
people.add(mike);
assertThat(people).containsExactly(mike, richard);
}
}

View File

@@ -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<Person.PersonWithEquals> people = new ArrayList<>();
people.add(joe);
people.add(allan);
Comparator<Person.PersonWithEquals> compareByFirstNames = new Comparator<Person.PersonWithEquals>() {
@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<Person.PersonWithEquals> people = new ArrayList<>();
people.add(joe);
people.add(allan);
Comparator<Person.PersonWithEquals> 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<Person.PersonWithEqualsAndComparableUsingComparator> people = new TreeSet<>();
people.add(richard);
people.add(mike);
assertThat(people).containsExactly(mike, richard);
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}