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

Commit 6a79ccc

Browse files
committed
Replace arrays with (unmodifiable) lists
1 parent 26d68fb commit 6a79ccc

4 files changed

Lines changed: 47 additions & 42 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.google.gson.JsonObject;
55
import com.scorpiac.javarant.exceptions.NotACollabException;
66

7+
import java.util.Collections;
8+
79
public class Collab extends Rant {
810
private String projectType;
911

@@ -15,7 +17,7 @@ public class Collab extends Rant {
1517
private String url;
1618

1719
protected Collab(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String projectType, String summary, int commentCount) {
18-
super(devRant, id, user, upvotes, downvotes, score, summary, null, new String[0], commentCount);
20+
super(devRant, id, user, upvotes, downvotes, score, summary, null, Collections.emptyList(), commentCount);
1921
this.projectType = projectType;
2022
}
2123

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public boolean isLoggedIn() {
9696
* @param skip How many rants to skip.
9797
* @return An array of rants.
9898
*/
99-
public Rant[] rants(Sort sort, int limit, int skip) {
99+
public List<Rant> rants(Sort sort, int limit, int skip) {
100100
// Rants url, app id, sort, skip, limit.
101101
String url = String.format("%1$s?app=%2$s&sort=%3$s&skip=%4$d&limit=%5$d", API_RANTS, APP_ID, sort.toString(), skip, limit);
102102
JsonObject json = get(url);
@@ -105,7 +105,7 @@ public Rant[] rants(Sort sort, int limit, int skip) {
105105
if (!Util.jsonSuccess(json))
106106
return null;
107107

108-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject())).toArray(new Rant[0]);
108+
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
109109
}
110110

111111
/**
@@ -114,7 +114,7 @@ public Rant[] rants(Sort sort, int limit, int skip) {
114114
* @param term The term to search for.
115115
* @return An array of rants matching the search term.
116116
*/
117-
public Rant[] search(String term) {
117+
public List<Rant> search(String term) {
118118
// Search url, app id, term.
119119
String url = String.format("%1$s?app=%2$s&term=%3$s", API_SEARCH, APP_ID, term);
120120
JsonObject json = get(url);
@@ -123,7 +123,7 @@ public Rant[] search(String term) {
123123
if (!Util.jsonSuccess(json))
124124
return null;
125125

126-
return Util.jsonToList(json.get("results").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject())).toArray(new Rant[0]);
126+
return Util.jsonToList(json.get("results").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
127127
}
128128

129129
/**
@@ -148,7 +148,7 @@ public Rant surprise() {
148148
*
149149
* @return The weekly rants.
150150
*/
151-
public Rant[] weekly() {
151+
public List<Rant> weekly() {
152152
// Weekly url, app id.
153153
String url = String.format("%1$s?app=%2$s", API_WEEKLY, APP_ID);
154154
JsonObject json = get(url);
@@ -157,15 +157,15 @@ public Rant[] weekly() {
157157
if (!Util.jsonSuccess(json))
158158
return null;
159159

160-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject())).toArray(new Rant[0]);
160+
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
161161
}
162162

163163
/**
164164
* Get the collab rants.
165165
*
166166
* @return The collab rants.
167167
*/
168-
public Collab[] collabs() {
168+
public List<Collab> collabs() {
169169
// Collab url, app id.
170170
String url = String.format("%1$s?app=%2$s&", API_COLLABS, APP_ID);
171171
JsonObject json = get(url);
@@ -174,7 +174,7 @@ public Collab[] collabs() {
174174
if (!Util.jsonSuccess(json))
175175
return null;
176176

177-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Collab.fromJson(this, elem.getAsJsonObject())).toArray(new Collab[0]);
177+
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Collab.fromJson(this, elem.getAsJsonObject()));
178178
}
179179

180180
/**

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
66

7+
import java.util.Collections;
8+
import java.util.List;
9+
710
public class Rant extends RantContent {
8-
private String[] tags;
11+
private List<String> tags;
912
private int commentCount;
10-
private Comment[] comments;
13+
private List<Comment> comments;
1114

12-
protected Rant(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String text, Image image, String[] tags, int commentCount) {
15+
protected Rant(DevRant devRant, int id, User user, int upvotes, int downvotes, int score, String text, Image image, List<String> tags, int commentCount) {
1316
super(devRant, id, user, upvotes, downvotes, score, text, image);
1417
this.tags = tags;
1518
this.commentCount = commentCount;
@@ -25,7 +28,7 @@ static Rant fromJson(DevRant devRant, JsonObject json) {
2528
json.get("score").getAsInt(),
2629
json.get("text").getAsString(),
2730
Image.fromJson(json.get("attached_image")),
28-
Util.jsonToList(json.getAsJsonArray("tags"), JsonElement::getAsString).toArray(new String[0]),
31+
Util.jsonToList(json.getAsJsonArray("tags"), JsonElement::getAsString),
2932
json.get("num_comments").getAsInt()
3033
);
3134
}
@@ -42,21 +45,21 @@ static Rant fromJson(DevRant devRant, JsonObject rant, JsonArray comments) {
4245
* @param commentArray The JSON array to get the comments from.
4346
*/
4447
protected void commentsFromJson(JsonArray commentArray) {
45-
comments = Util.jsonToList(commentArray, elem -> Comment.fromJson(devRant, elem.getAsJsonObject())).toArray(new Comment[0]);
48+
comments = Util.jsonToList(commentArray, elem -> Comment.fromJson(devRant, elem.getAsJsonObject()));
4649
}
4750

4851
/**
4952
* Get the comments on this rant. If they are not yet retrieved, this will also fetch them.
5053
*
5154
* @return The comments.
5255
*/
53-
public Comment[] getComments() {
56+
public List<Comment> getComments() {
5457
fetchComments();
55-
return comments;
58+
return Collections.unmodifiableList(comments);
5659
}
5760

5861
/**
59-
* Fetch and store the comments on this rant. If the comments are already fetched, they will not be fetched again.
62+
* Fetch the comments on this rant. If the comments are already fetched, they will not be fetched again.
6063
*
6164
* @return Whether the data was fetched successfully.
6265
*/
@@ -65,9 +68,9 @@ public boolean fetchComments() {
6568
}
6669

6770
/**
68-
* Fetch and store the comments on this rant.
71+
* Fetch the comments on this rant.
6972
*
70-
* @param force Whether to fetch the data even if it was already fetched.
73+
* @param force Whether to fetch the comments even if it they are already fetched.
7174
* @return Whether the data was fetched successfully.
7275
*/
7376
public boolean fetchComments(boolean force) {
@@ -79,13 +82,10 @@ public boolean fetchComments(boolean force) {
7982
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS, getId(), DevRant.APP_ID);
8083
JsonObject json = devRant.get(url);
8184

82-
// Check for success.
8385
if (!Util.jsonSuccess(json))
8486
return false;
8587

86-
// Get the comments.
8788
commentsFromJson(json.getAsJsonArray("comments"));
88-
8989
return true;
9090
}
9191

@@ -102,14 +102,14 @@ public boolean equals(Object obj) {
102102
}
103103

104104
/**
105-
* Get the tags from this rant.
105+
* Get the tags.
106106
*/
107-
public String[] getTags() {
108-
return tags;
107+
public List<String> getTags() {
108+
return Collections.unmodifiableList(tags);
109109
}
110110

111111
/**
112-
* Get the amount of comments on this rant.
112+
* Get the amount of comments.
113113
*/
114114
public int getCommentCount() {
115115
return commentCount;

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.google.gson.JsonObject;
44
import com.scorpiac.javarant.exceptions.NoSuchUserException;
55

6+
import java.util.Collections;
7+
import java.util.List;
8+
69
public class User extends DevRantHolder {
710
// Data that is always available.
811
private int id;
@@ -15,10 +18,10 @@ public class User extends DevRantHolder {
1518
private String location;
1619
private String skills;
1720
private String github;
18-
private Rant[] rants;
19-
private Rant[] upvoted;
20-
private Comment[] comments;
21-
private Rant[] favorites;
21+
private List<Rant> rants;
22+
private List<Rant> upvoted;
23+
private List<Comment> comments;
24+
private List<Rant> favorites;
2225
private int rantsCount;
2326
private int upvotedCount;
2427
private int commentsCount;
@@ -106,10 +109,10 @@ public boolean fetchData(boolean force) {
106109
commentsCount = countsJson.get("comments").getAsInt();
107110
favoritesCount = countsJson.get("favorites").getAsInt();
108111

109-
rants = Util.jsonToList(subContentJson.get("rants").getAsJsonArray(), rant -> Rant.fromJson(devRant, rant.getAsJsonObject())).toArray(new Rant[0]);
110-
upvoted = Util.jsonToList(subContentJson.get("upvoted").getAsJsonArray(), rant -> Rant.fromJson(devRant, rant.getAsJsonObject())).toArray(new Rant[0]);
111-
comments = Util.jsonToList(subContentJson.get("comments").getAsJsonArray(), comment -> Comment.fromJson(devRant, comment.getAsJsonObject())).toArray(new Comment[0]);
112-
favorites = Util.jsonToList(subContentJson.get("favorites").getAsJsonArray(), rant -> Rant.fromJson(devRant, rant.getAsJsonObject())).toArray(new Rant[0]);
112+
rants = Util.jsonToList(subContentJson.get("rants").getAsJsonArray(), rant -> Rant.fromJson(devRant, rant.getAsJsonObject()));
113+
upvoted = Util.jsonToList(subContentJson.get("upvoted").getAsJsonArray(), rant -> Rant.fromJson(devRant, rant.getAsJsonObject()));
114+
comments = Util.jsonToList(subContentJson.get("comments").getAsJsonArray(), comment -> Comment.fromJson(devRant, comment.getAsJsonObject()));
115+
favorites = Util.jsonToList(subContentJson.get("favorites").getAsJsonArray(), rant -> Rant.fromJson(devRant, rant.getAsJsonObject()));
113116

114117
return true;
115118
}
@@ -202,33 +205,33 @@ public String getGithub() {
202205
/**
203206
* Get the rants that this user posted.
204207
*/
205-
public Rant[] getRants() {
208+
public List<Rant> getRants() {
206209
fetchData();
207-
return rants;
210+
return Collections.unmodifiableList(rants);
208211
}
209212

210213
/**
211214
* Get the rants that this user upvoted.
212215
*/
213-
public Rant[] getUpvoted() {
216+
public List<Rant> getUpvoted() {
214217
fetchData();
215-
return upvoted;
218+
return Collections.unmodifiableList(upvoted);
216219
}
217220

218221
/**
219222
* Get this user's comments.
220223
*/
221-
public Comment[] getComments() {
224+
public List<Comment> getComments() {
222225
fetchData();
223-
return comments;
226+
return Collections.unmodifiableList(comments);
224227
}
225228

226229
/**
227230
* Get this user's favorites.
228231
*/
229-
public Rant[] getFavorites() {
232+
public List<Rant> getFavorites() {
230233
fetchData();
231-
return favorites;
234+
return Collections.unmodifiableList(favorites);
232235
}
233236

234237
/**

0 commit comments

Comments
 (0)