Skip to content

Commit d1e64fb

Browse files
author
dupirefr
committed
[BAEL-3981] Code for article
* Equality operators * Object#equals method * Objects#equals static method * Comparable interface * Comparator interface * Apache Commons features * Guava features
1 parent 4909263 commit d1e64fb

9 files changed

Lines changed: 791 additions & 1 deletion

File tree

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.9</commons-lang3.version>
74+
<guava.version>29.0-jre</guava.version>
6875
</properties>
6976

7077
</project>
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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 Person {
8+
public static class PersonWithoutEquals {
9+
private String firstName;
10+
private String lastName;
11+
12+
public PersonWithoutEquals(String firstName, String lastName) {
13+
this.firstName = firstName;
14+
this.lastName = lastName;
15+
}
16+
}
17+
18+
public static class PersonWithEquals {
19+
private String firstName;
20+
private String lastName;
21+
private LocalDate birthDate;
22+
23+
public PersonWithEquals(String firstName, String lastName) {
24+
if (firstName == null || lastName == null) {
25+
throw new NullPointerException("Names can't be null");
26+
}
27+
this.firstName = firstName;
28+
this.lastName = lastName;
29+
}
30+
31+
public PersonWithEquals(String firstName, String lastName, LocalDate birthDate) {
32+
this(firstName, lastName);
33+
34+
this.birthDate = birthDate;
35+
}
36+
37+
public String firstName() {
38+
return firstName;
39+
}
40+
41+
public String lastName() {
42+
return lastName;
43+
}
44+
45+
public LocalDate birthDate() {
46+
return birthDate;
47+
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o) return true;
52+
if (o == null || getClass() != o.getClass()) return false;
53+
PersonWithEquals that = (PersonWithEquals) o;
54+
return firstName.equals(that.firstName) &&
55+
lastName.equals(that.lastName) &&
56+
Objects.equals(birthDate, that.birthDate);
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return Objects.hash(firstName, lastName);
62+
}
63+
}
64+
65+
public static class PersonWithEqualsAndWrongComparable implements Comparable<PersonWithEqualsAndWrongComparable> {
66+
private String firstName;
67+
private String lastName;
68+
private LocalDate birthDate;
69+
70+
public PersonWithEqualsAndWrongComparable(String firstName, String lastName) {
71+
if (firstName == null || lastName == null) {
72+
throw new NullPointerException("Names can't be null");
73+
}
74+
this.firstName = firstName;
75+
this.lastName = lastName;
76+
}
77+
78+
public PersonWithEqualsAndWrongComparable(String firstName, String lastName, LocalDate birthDate) {
79+
this(firstName, lastName);
80+
81+
this.birthDate = birthDate;
82+
}
83+
84+
@Override
85+
public boolean equals(Object o) {
86+
if (this == o) return true;
87+
if (o == null || getClass() != o.getClass()) return false;
88+
PersonWithEqualsAndWrongComparable that = (PersonWithEqualsAndWrongComparable) o;
89+
return firstName.equals(that.firstName) &&
90+
lastName.equals(that.lastName) &&
91+
Objects.equals(birthDate, that.birthDate);
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return Objects.hash(firstName, lastName);
97+
}
98+
99+
@Override
100+
public int compareTo(PersonWithEqualsAndWrongComparable o) {
101+
return this.lastName.compareTo(o.lastName);
102+
}
103+
}
104+
105+
public static class PersonWithEqualsAndComparable implements Comparable<PersonWithEqualsAndComparable> {
106+
private String firstName;
107+
private String lastName;
108+
private LocalDate birthDate;
109+
110+
public PersonWithEqualsAndComparable(String firstName, String lastName) {
111+
if (firstName == null || lastName == null) {
112+
throw new NullPointerException("Names can't be null");
113+
}
114+
this.firstName = firstName;
115+
this.lastName = lastName;
116+
}
117+
118+
public PersonWithEqualsAndComparable(String firstName, String lastName, LocalDate birthDate) {
119+
this(firstName, lastName);
120+
121+
this.birthDate = birthDate;
122+
}
123+
124+
@Override
125+
public boolean equals(Object o) {
126+
if (this == o) return true;
127+
if (o == null || getClass() != o.getClass()) return false;
128+
PersonWithEqualsAndComparable that = (PersonWithEqualsAndComparable) o;
129+
return firstName.equals(that.firstName) &&
130+
lastName.equals(that.lastName) &&
131+
Objects.equals(birthDate, that.birthDate);
132+
}
133+
134+
@Override
135+
public int hashCode() {
136+
return Objects.hash(firstName, lastName);
137+
}
138+
139+
@Override
140+
public int compareTo(PersonWithEqualsAndComparable o) {
141+
int lastNamesComparison = this.lastName.compareTo(o.lastName);
142+
if (lastNamesComparison == 0) {
143+
int firstNamesComparison = this.firstName.compareTo(o.firstName);
144+
if (firstNamesComparison == 0) {
145+
if (this.birthDate != null && o.birthDate != null) {
146+
return this.birthDate.compareTo(o.birthDate);
147+
} else if (this.birthDate != null) {
148+
return 1;
149+
} else if (o.birthDate != null) {
150+
return -1;
151+
} else {
152+
return 0;
153+
}
154+
} else {
155+
return firstNamesComparison;
156+
}
157+
} else {
158+
return lastNamesComparison;
159+
}
160+
}
161+
}
162+
163+
public static class PersonWithEqualsAndComparableUsingComparator implements Comparable<PersonWithEqualsAndComparableUsingComparator> {
164+
private String firstName;
165+
private String lastName;
166+
private LocalDate birthDate;
167+
168+
public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName) {
169+
if (firstName == null || lastName == null) {
170+
throw new NullPointerException("Names can't be null");
171+
}
172+
this.firstName = firstName;
173+
this.lastName = lastName;
174+
}
175+
176+
public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName, LocalDate birthDate) {
177+
this(firstName, lastName);
178+
179+
this.birthDate = birthDate;
180+
}
181+
182+
public String firstName() {
183+
return firstName;
184+
}
185+
186+
public String lastName() {
187+
return lastName;
188+
}
189+
190+
public LocalDate birthDate() {
191+
return birthDate;
192+
}
193+
194+
@Override
195+
public boolean equals(Object o) {
196+
if (this == o) return true;
197+
if (o == null || getClass() != o.getClass()) return false;
198+
PersonWithEqualsAndComparableUsingComparator that = (PersonWithEqualsAndComparableUsingComparator) o;
199+
return firstName.equals(that.firstName) &&
200+
lastName.equals(that.lastName) &&
201+
Objects.equals(birthDate, that.birthDate);
202+
}
203+
204+
@Override
205+
public int hashCode() {
206+
return Objects.hash(firstName, lastName);
207+
}
208+
209+
@Override
210+
public int compareTo(PersonWithEqualsAndComparableUsingComparator o) {
211+
return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName)
212+
.thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName)
213+
.thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder()))
214+
.compare(this, o);
215+
}
216+
}
217+
}
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)