Skip to content

Commit 2ee97d5

Browse files
darxriggsfabienrenaud
authored andcommitted
Consistent equals() and hashCode() implementations
1 parent 1f1ba41 commit 2ee97d5

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

src/main/java/com/github/fabienrenaud/jjb/model/Clients.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ public class Clients {
2727

2828
@Override
2929
public boolean equals(Object o) {
30-
if (this == o) return true;
31-
if (!(o instanceof Clients)) return false;
30+
if (this == o) {
31+
return true;
32+
}
33+
if (!(o instanceof Clients)) {
34+
return false;
35+
}
3236

3337
Clients that = (Clients) o;
3438

35-
return clients != null ? clients.equals(that.clients) : that.clients == null;
39+
return Objects.equals(clients, that.clients);
3640
}
3741

3842
@Override
3943
public int hashCode() {
40-
return clients != null ? clients.hashCode() : 0;
44+
return Objects.hashCode(clients);
4145
}
4246

4347
@Override
@@ -104,7 +108,9 @@ public boolean equals(Object o) {
104108
if (!(o instanceof Client)) {
105109
return false;
106110
}
111+
107112
Client client = (Client) o;
113+
108114
return index == client.index &&
109115
isActive == client.isActive &&
110116
age == client.age &&
@@ -129,7 +135,8 @@ public boolean equals(Object o) {
129135

130136
@Override
131137
public int hashCode() {
132-
return Objects.hash(_id, index, guid, isActive, balance, picture, age, eyeColor, name, gender, company, emails, phones, address, about, registered, tags, partners);
138+
return Objects.hash(_id, index, guid, isActive, balance, picture, age, eyeColor, name, gender, company,
139+
Arrays.hashCode(emails), Arrays.hashCode(phones), address, about, registered, tags, partners);
133140
}
134141

135142
private String toStr(long[] nums) {
@@ -243,22 +250,23 @@ public static Partner create(long id, String name, OffsetDateTime since) {
243250

244251
@Override
245252
public boolean equals(Object o) {
246-
if (this == o) return true;
247-
if (!(o instanceof Partner)) return false;
253+
if (this == o) {
254+
return true;
255+
}
256+
if (!(o instanceof Partner)) {
257+
return false;
258+
}
248259

249260
Partner partner = (Partner) o;
250261

251-
if (id != partner.id) return false;
252-
if (since == null && partner.since != null || since != null && !since.isEqual(partner.since)) return false;
253-
return name != null ? name.equals(partner.name) : partner.name == null;
262+
return id == partner.id &&
263+
Objects.equals(since, partner.since) &&
264+
Objects.equals(name, partner.name);
254265
}
255266

256267
@Override
257268
public int hashCode() {
258-
int result = (int)id;
259-
result = 31 * result + (since != null ? since.hashCode() : 0);
260-
result = 31 * result + (name != null ? name.hashCode() : 0);
261-
return result;
269+
return Objects.hash(id, since, name);
262270
}
263271

264272
@Override

src/main/java/com/github/fabienrenaud/jjb/model/Users.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ public class Users {
2020

2121
@Override
2222
public boolean equals(Object o) {
23-
if (this == o) return true;
24-
if (!(o instanceof Users)) return false;
23+
if (this == o) {
24+
return true;
25+
}
26+
if (!(o instanceof Users)) {
27+
return false;
28+
}
2529

2630
Users that = (Users) o;
2731

28-
return users != null ? users.equals(that.users) : that.users == null;
32+
return Objects.equals(users, that.users);
2933
}
3034

3135
@Override
3236
public int hashCode() {
33-
return users != null ? users.hashCode() : 0;
37+
return Objects.hashCode(users);
3438
}
3539

3640
@Override
@@ -100,7 +104,9 @@ public boolean equals(Object o) {
100104
if (!(o instanceof User)) {
101105
return false;
102106
}
107+
103108
User user = (User) o;
109+
104110
return index == user.index &&
105111
isActive == user.isActive &&
106112
age == user.age &&
@@ -158,20 +164,22 @@ public static Friend create(String id, String name) {
158164

159165
@Override
160166
public boolean equals(Object o) {
161-
if (this == o) return true;
162-
if (!(o instanceof Friend)) return false;
167+
if (this == o) {
168+
return true;
169+
}
170+
if (!(o instanceof Friend)) {
171+
return false;
172+
}
163173

164174
Friend friend = (Friend) o;
165175

166-
if (id != null ? !id.equals(friend.id) : friend.id != null) return false;
167-
return name != null ? name.equals(friend.name) : friend.name == null;
176+
return Objects.equals(id, friend.id) &&
177+
Objects.equals(name, friend.name);
168178
}
169179

170180
@Override
171181
public int hashCode() {
172-
int result = id != null ? id.hashCode() : 0;
173-
result = 31 * result + (name != null ? name.hashCode() : 0);
174-
return result;
182+
return Objects.hash(id, name);
175183
}
176184

177185
@Override

src/main/java/com/github/fabienrenaud/jjb/support/Libapi.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.List;
5+
import java.util.Objects;
56

67
public class Libapi {
78

@@ -33,20 +34,22 @@ public List<Api> api() {
3334

3435
@Override
3536
public boolean equals(Object o) {
36-
if (this == o) return true;
37-
if (!(o instanceof Libapi)) return false;
37+
if (this == o) {
38+
return true;
39+
}
40+
if (!(o instanceof Libapi)) {
41+
return false;
42+
}
3843

3944
Libapi libapi = (Libapi) o;
4045

41-
if (lib != libapi.lib) return false;
42-
return api == libapi.api;
43-
46+
return active == libapi.active &&
47+
lib == libapi.lib &&
48+
Objects.equals(api, libapi.api);
4449
}
4550

4651
@Override
4752
public int hashCode() {
48-
int result = lib != null ? lib.hashCode() : 0;
49-
result = 31 * result + (api != null ? api.hashCode() : 0);
50-
return result;
53+
return Objects.hash(active, lib, api);
5154
}
5255
}

0 commit comments

Comments
 (0)