Skip to content

Commit 0ce80e4

Browse files
author
dupirefr
committed
[BAEL-3981] Moved inner classes to upper level
1 parent d1e64fb commit 0ce80e4

11 files changed

Lines changed: 282 additions & 278 deletions

core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/Person.java

Lines changed: 0 additions & 217 deletions
This file was deleted.
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+
}
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+
}

0 commit comments

Comments
 (0)