Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 5e1c6dc

Browse files
committed
Override equals and hashCode methods
1 parent fbb6c8b commit 5e1c6dc

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/main/java/com/scorpiac/javarant/Comment.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ static Comment fromJson(JsonObject json) {
1717
Image.fromJson(json.get("attached_image"))
1818
);
1919
}
20+
21+
@Override
22+
public boolean equals(Object obj) {
23+
return obj instanceof Comment && ((Comment) obj).getId() == getId();
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return getId();
29+
}
2030
}

src/main/java/com/scorpiac/javarant/Image.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ static Image fromJson(JsonElement json) {
2828
return new Image(obj.get("url").getAsString(), obj.get("width").getAsInt(), obj.get("height").getAsInt());
2929
}
3030

31+
@Override
32+
public boolean equals(Object obj) {
33+
return obj instanceof Image && ((Image) obj).getUrl().equals(url);
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return url.hashCode();
39+
}
40+
3141
/**
3242
* Get the image url.
3343
*

src/main/java/com/scorpiac/javarant/Rant.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public String link() {
111111
return DevRant.link(DevRant.RANT_URL + "/" + getId());
112112
}
113113

114+
@Override
115+
public boolean equals(Object obj) {
116+
return obj instanceof Rant && ((Rant) obj).getId() == getId();
117+
}
118+
119+
@Override
120+
public int hashCode() {
121+
return getId();
122+
}
123+
114124
/**
115125
* Get the tags from this rant.
116126
*/

src/main/java/com/scorpiac/javarant/User.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ public String avatarLink() {
176176
return DevRant.AVATARS_URL + "/" + avatar;
177177
}
178178

179+
@Override
180+
public boolean equals(Object obj) {
181+
return obj instanceof User && ((User) obj).getId() == id;
182+
}
183+
184+
@Override
185+
public int hashCode() {
186+
return id;
187+
}
188+
179189
/**
180190
* Get the user id.
181191
*/

0 commit comments

Comments
 (0)