|
| 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 | +} |
0 commit comments