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

Commit 5e3a51c

Browse files
committed
Add vote state to RantContent
Closes #9
1 parent 8ccd60b commit 5e3a51c

5 files changed

Lines changed: 53 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class Collab extends Rant {
1616
private String teamSize;
1717
private String url;
1818

19-
protected Collab(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String projectType, String summary, int commentCount) {
20-
super(devRant, id, user, upvotes, downvotes, score, summary, null, Collections.emptyList(), commentCount);
19+
protected Collab(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, int voteState, String projectType, String summary, int commentCount) {
20+
super(devRant, id, user, upvotes, downvotes, score, voteState, summary, null, Collections.emptyList(), commentCount);
2121
this.projectType = projectType;
2222
}
2323

@@ -33,6 +33,7 @@ static Collab fromJson(DevRant devRant, JsonObject json) {
3333
json.get("num_upvotes").getAsInt(),
3434
json.get("num_downvotes").getAsInt(),
3535
json.get("score").getAsInt(),
36+
json.get("vote_state").getAsInt(),
3637
json.get("c_type_long").getAsString(),
3738
json.get("text").getAsString(),
3839
json.get("num_comments").getAsInt()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.google.gson.JsonObject;
44

55
public class Comment extends RantContent {
6-
protected Comment(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String content, Image image) {
7-
super(devRant, id, user, upvotes, downvotes, score, content, image);
6+
protected Comment(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, int voteState, String content, Image image) {
7+
super(devRant, id, user, upvotes, downvotes, score, voteState, content, image);
88
}
99

1010
static Comment fromJson(DevRant devRant, JsonObject json) {
@@ -15,6 +15,7 @@ static Comment fromJson(DevRant devRant, JsonObject json) {
1515
json.get("num_upvotes").getAsInt(),
1616
json.get("num_downvotes").getAsInt(),
1717
json.get("score").getAsInt(),
18+
json.get("vote_state").getAsInt(),
1819
json.get("body").getAsString(),
1920
Image.fromJson(json.get("attached_image"))
2021
);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class Rant extends RantContent {
1212
private int commentCount;
1313
private List<Comment> comments;
1414

15-
protected Rant(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String text, Image image, List<String> tags, int commentCount) {
16-
super(devRant, id, user, upvotes, downvotes, score, text, image);
15+
protected Rant(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, int voteState, String text, Image image, List<String> tags, int commentCount) {
16+
super(devRant, id, user, upvotes, downvotes, score, voteState, text, image);
1717
this.tags = tags;
1818
this.commentCount = commentCount;
1919
}
@@ -26,6 +26,7 @@ static Rant fromJson(DevRant devRant, JsonObject json) {
2626
json.get("num_upvotes").getAsInt(),
2727
json.get("num_downvotes").getAsInt(),
2828
json.get("score").getAsInt(),
29+
json.get("vote_state").getAsInt(),
2930
json.get("text").getAsString(),
3031
Image.fromJson(json.get("attached_image")),
3132
Util.jsonToList(json.getAsJsonArray("tags"), JsonElement::getAsString),

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ public abstract class RantContent extends DevRantHolder {
66
private int upvotes;
77
private int downvotes;
88
private int score;
9+
private VoteState voteState;
910
private String content;
1011
private Image image;
1112

12-
protected RantContent(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String content, Image image) {
13+
protected RantContent(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, int voteState, String content, Image image) {
1314
super(devRant);
1415
this.id = id;
1516
this.user = user;
1617
this.upvotes = upvotes;
1718
this.downvotes = downvotes;
1819
this.score = score;
20+
this.voteState = VoteState.fromValue(voteState);
1921
this.content = content;
2022
this.image = image;
2123
}
@@ -65,6 +67,13 @@ public int getScore() {
6567
return score;
6668
}
6769

70+
/**
71+
* Get the vote state.
72+
*/
73+
public VoteState getVoteState() {
74+
return voteState;
75+
}
76+
6877
/**
6978
* Get the content (text).
7079
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.scorpiac.javarant;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public enum VoteState {
7+
UP,
8+
NONE,
9+
DOWN;
10+
11+
private static final Map<Integer, VoteState> states = new HashMap<>();
12+
13+
static {
14+
states.put(1, UP);
15+
states.put(0, NONE);
16+
states.put(-1, DOWN);
17+
}
18+
19+
/**
20+
* Get the {@link VoteState} corresponding with a number value.
21+
*
22+
* @param value The value to get the state for.
23+
* @return The {@link VoteState} belonging to the value.
24+
*/
25+
public static VoteState fromValue(int value) {
26+
// Clamp the value between -1 and 1.
27+
if (value > 1)
28+
value = 1;
29+
if (value < -1)
30+
value = -1;
31+
32+
return states.get(value);
33+
}
34+
}

0 commit comments

Comments
 (0)