Skip to content

Commit 7df2401

Browse files
authored
Merge pull request eugenp#9224 from dupirefr/author/BAEL-3981-comparing_objects
[BAEL-3981] Comparing in Java
2 parents 5d7b774 + bb1ccff commit 7df2401

13 files changed

+795
-1
lines changed

core-java-modules/core-java-lang-2/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
<dependency>
2121
<groupId>org.apache.commons</groupId>
2222
<artifactId>commons-lang3</artifactId>
23-
<version>3.9</version>
23+
<version>${commons-lang3.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.guava</groupId>
27+
<artifactId>guava</artifactId>
28+
<version>${guava.version}</version>
2429
</dependency>
2530
<dependency>
2631
<groupId>commons-beanutils</groupId>
@@ -65,6 +70,8 @@
6570
<jmh-generator.version>1.19</jmh-generator.version>
6671
<assertj.version>3.12.2</assertj.version>
6772
<commons.beanutils.version>1.9.4</commons.beanutils.version>
73+
<commons-lang3.version>3.10</commons-lang3.version>
74+
<guava.version>29.0-jre</guava.version>
6875
</properties>
6976

7077
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.comparing;
2+
3+
import java.time.LocalDate;
4+
import java.util.Objects;
5+
6+
public class PersonWithEquals {
7+
private String firstName;
8+
private String lastName;
9+
private LocalDate birthDate;
10+
11+
public PersonWithEquals(String firstName, String lastName) {
12+
if (firstName == null || lastName == null) {
13+
throw new NullPointerException("Names can't be null");
14+
}
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
}
18+
19+
public PersonWithEquals(String firstName, String lastName, LocalDate birthDate) {
20+
this(firstName, lastName);
21+
22+
this.birthDate = birthDate;
23+
}
24+
25+
public String firstName() {
26+
return firstName;
27+
}
28+
29+
public String lastName() {
30+
return lastName;
31+
}
32+
33+
public LocalDate birthDate() {
34+
return birthDate;
35+
}
36+
37+
@Override
38+
public boolean equals(Object o) {
39+
if (this == o) return true;
40+
if (o == null || getClass() != o.getClass()) return false;
41+
PersonWithEquals that = (PersonWithEquals) o;
42+
return firstName.equals(that.firstName) &&
43+
lastName.equals(that.lastName) &&
44+
Objects.equals(birthDate, that.birthDate);
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return Objects.hash(firstName, lastName);
50+
}
51+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.comparing;
2+
3+
import java.time.LocalDate;
4+
import java.util.Objects;
5+
6+
public class PersonWithEqualsAndComparable implements Comparable<PersonWithEqualsAndComparable> {
7+
private String firstName;
8+
private String lastName;
9+
private LocalDate birthDate;
10+
11+
public PersonWithEqualsAndComparable(String firstName, String lastName) {
12+
if (firstName == null || lastName == null) {
13+
throw new NullPointerException("Names can't be null");
14+
}
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
}
18+
19+
public PersonWithEqualsAndComparable(String firstName, String lastName, LocalDate birthDate) {
20+
this(firstName, lastName);
21+
22+
this.birthDate = birthDate;
23+
}
24+
25+
@Override
26+
public boolean equals(Object o) {
27+
if (this == o) return true;
28+
if (o == null || getClass() != o.getClass()) return false;
29+
PersonWithEqualsAndComparable that = (PersonWithEqualsAndComparable) o;
30+
return firstName.equals(that.firstName) &&
31+
lastName.equals(that.lastName) &&
32+
Objects.equals(birthDate, that.birthDate);
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
return Objects.hash(firstName, lastName);
38+
}
39+
40+
@Override
41+
public int compareTo(PersonWithEqualsAndComparable o) {
42+
int lastNamesComparison = this.lastName.compareTo(o.lastName);
43+
if (lastNamesComparison == 0) {
44+
int firstNamesComparison = this.firstName.compareTo(o.firstName);
45+
if (firstNamesComparison == 0) {
46+
if (this.birthDate != null && o.birthDate != null) {
47+
return this.birthDate.compareTo(o.birthDate);
48+
} else if (this.birthDate != null) {
49+
return 1;
50+
} else if (o.birthDate != null) {
51+
return -1;
52+
} else {
53+
return 0;
54+
}
55+
} else {
56+
return firstNamesComparison;
57+
}
58+
} else {
59+
return lastNamesComparison;
60+
}
61+
}
62+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.comparing;
2+
3+
import java.time.LocalDate;
4+
import java.util.Comparator;
5+
import java.util.Objects;
6+
7+
public class PersonWithEqualsAndComparableUsingComparator implements Comparable<PersonWithEqualsAndComparableUsingComparator> {
8+
private String firstName;
9+
private String lastName;
10+
private LocalDate birthDate;
11+
12+
public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName) {
13+
if (firstName == null || lastName == null) {
14+
throw new NullPointerException("Names can't be null");
15+
}
16+
this.firstName = firstName;
17+
this.lastName = lastName;
18+
}
19+
20+
public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName, LocalDate birthDate) {
21+
this(firstName, lastName);
22+
23+
this.birthDate = birthDate;
24+
}
25+
26+
public String firstName() {
27+
return firstName;
28+
}
29+
30+
public String lastName() {
31+
return lastName;
32+
}
33+
34+
public LocalDate birthDate() {
35+
return birthDate;
36+
}
37+
38+
@Override
39+
public boolean equals(Object o) {
40+
if (this == o) return true;
41+
if (o == null || getClass() != o.getClass()) return false;
42+
PersonWithEqualsAndComparableUsingComparator that = (PersonWithEqualsAndComparableUsingComparator) o;
43+
return firstName.equals(that.firstName) &&
44+
lastName.equals(that.lastName) &&
45+
Objects.equals(birthDate, that.birthDate);
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return Objects.hash(firstName, lastName);
51+
}
52+
53+
@Override
54+
public int compareTo(PersonWithEqualsAndComparableUsingComparator o) {
55+
return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName)
56+
.thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName)
57+
.thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder()))
58+
.compare(this, o);
59+
}
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.comparing;
2+
3+
import java.time.LocalDate;
4+
import java.util.Objects;
5+
6+
public class PersonWithEqualsAndWrongComparable implements Comparable<PersonWithEqualsAndWrongComparable> {
7+
private String firstName;
8+
private String lastName;
9+
private LocalDate birthDate;
10+
11+
public PersonWithEqualsAndWrongComparable(String firstName, String lastName) {
12+
if (firstName == null || lastName == null) {
13+
throw new NullPointerException("Names can't be null");
14+
}
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
}
18+
19+
public PersonWithEqualsAndWrongComparable(String firstName, String lastName, LocalDate birthDate) {
20+
this(firstName, lastName);
21+
22+
this.birthDate = birthDate;
23+
}
24+
25+
@Override
26+
public boolean equals(Object o) {
27+
if (this == o) return true;
28+
if (o == null || getClass() != o.getClass()) return false;
29+
PersonWithEqualsAndWrongComparable that = (PersonWithEqualsAndWrongComparable) o;
30+
return firstName.equals(that.firstName) &&
31+
lastName.equals(that.lastName) &&
32+
Objects.equals(birthDate, that.birthDate);
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
return Objects.hash(firstName, lastName);
38+
}
39+
40+
@Override
41+
public int compareTo(PersonWithEqualsAndWrongComparable o) {
42+
return this.lastName.compareTo(o.lastName);
43+
}
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.comparing;
2+
3+
public class PersonWithoutEquals {
4+
private String firstName;
5+
private String lastName;
6+
7+
public PersonWithoutEquals(String firstName, String lastName) {
8+
this.firstName = firstName;
9+
this.lastName = lastName;
10+
}
11+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.comparing;
2+
3+
import org.apache.commons.lang3.ObjectUtils;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class ApacheCommonsObjectUtilsUnitTest {
9+
10+
@Test
11+
void givenTwoStringsWithSameValues_whenApacheCommonsEqualityMethods_thenEqualsTrueNotEqualsFalse() {
12+
String a = new String("Hello!");
13+
String b = new String("Hello!");
14+
15+
assertThat(ObjectUtils.equals(a, b)).isTrue();
16+
assertThat(ObjectUtils.notEqual(a, b)).isFalse();
17+
}
18+
19+
@Test
20+
void givenTwoStringsWithDifferentValues_whenApacheCommonsEqualityMethods_thenEqualsFalseNotEqualsTrue() {
21+
String a = new String("Hello!");
22+
String b = new String("Hello World!");
23+
24+
assertThat(ObjectUtils.equals(a, b)).isFalse();
25+
assertThat(ObjectUtils.notEqual(a, b)).isTrue();
26+
}
27+
28+
@Test
29+
void givenTwoStringsWithConsecutiveValues_whenApacheCommonsCompare_thenNegative() {
30+
String first = new String("Hello!");
31+
String second = new String("How are you?");
32+
33+
assertThat(ObjectUtils.compare(first, second)).isNegative();
34+
}
35+
36+
@Test
37+
void givenTwoStringsWithSameValues_whenApacheCommonsEqualityMethods_thenEqualsFalseNotEqualsTrue() {
38+
String first = new String("Hello!");
39+
String second = new String("Hello!");
40+
41+
assertThat(ObjectUtils.compare(first, second)).isZero();
42+
}
43+
44+
@Test
45+
void givenTwoStringsWithConsecutiveValues_whenApacheCommonsCompareReversed_thenPositive() {
46+
String first = new String("Hello!");
47+
String second = new String("How are you?");
48+
49+
assertThat(ObjectUtils.compare(second, first)).isPositive();
50+
}
51+
52+
@Test
53+
void givenTwoStringsOneNull_whenApacheCommonsCompare_thenPositive() {
54+
String first = new String("Hello!");
55+
String second = null;
56+
57+
assertThat(ObjectUtils.compare(first, second, false)).isPositive();
58+
}
59+
}

0 commit comments

Comments
 (0)