1616import material .user .BungieUser ;
1717import utils .HttpUtils ;
1818import utils .StringUtils ;
19+ import utils .framework .ContentFramework ;
1920
2021import java .util .*;
2122import java .util .concurrent .CompletableFuture ;
2223import java .util .concurrent .ExecutionException ;
2324
24- public class Clan {
25+ public class Clan extends ContentFramework {
2526
2627 private String apiKey = DestinyAPI .getApiKey ();
2728 private HttpUtils hu = new HttpUtils ();
28- private JsonObject jo ; // The entire Clan response
29- private JsonObject cjo ; // "detail"
29+ private JsonObject jo , cjo ; // The entire Clan response
3030
31- private long clanId ;
31+ private long clanId = - 1 ;
3232 private String clanName , clanDescription , motto ;
3333
3434 // Details about the clan (more or less in the order they appear in the JSON response)
3535 private Date creationDate ;
36- private int memberCount ;
37- private boolean isPublic , allowChat ;
36+ private int memberCount = -1 ;
3837
39- private BungieUser founder ;
4038 private List <BungieUser > admins , members ;
4139 private ClanManagement clanManagement ;
42- private JsonObject jso ;
43- private JsonArray ja ;
4440 private JsonObject jj ;
4541
4642 public Clan (long clanId ) {
43+ super ("https://www.bungie.net/platform/GroupV2/" + clanId + "/?components=200" , source -> {
44+ return source .getAsJsonObject ("Response" );
45+ });
4746 this .clanId = clanId ;
48- cjo = hu .urlRequestGET ("https://www.bungie.net/platform/GroupV2/" + clanId + "/?components=200" ).get ("Response" ).getAsJsonObject ().get ("detail" ).getAsJsonObject ();
49- assignValues ();
5047 }
5148
5249 public Clan (String clanName ) {
50+ super (("https://www.bungie.net/Platform/GroupV2/Name/" + clanName .replace (" " , "%20" )), source -> {
51+ return source .getAsJsonObject ("Response" );
52+ });
5353 this .clanName = clanName ;
54- jo = hu .urlRequestGET ("https://www.bungie.net/Platform/GroupV2/Name/" + clanName .replace (" " , "%20" ) + "/1/?components=200" ).get ("Response" ).getAsJsonObject ();
55- cjo = jo .getAsJsonObject ("detail" );
56- assignValues ();
5754 }
5855
5956 public Clan (long clanId , String clanName ) {
57+ super (("https://www.bungie.net/platform/GroupV2/" + clanId + "/?components=200" ), source -> {
58+ return source .getAsJsonObject ("Response" );
59+ });
6060 this .clanId = clanId ;
6161 this .clanName = clanName ;
62- cjo = hu .urlRequestGET ("https://www.bungie.net/platform/GroupV2/" + clanId + "/?components=200" ).get ("Response" ).getAsJsonObject ().get ("detail" ).getAsJsonObject ();
63- assignValues ();
64- }
65-
66- private void assignValues () {
67- if (clanName == null ) { // If the clan object was created via ID then the clanName would be null by default
68- clanName = cjo .get ("name" ).getAsString ();
69- } else { // Opposite of previous reason
70- clanId = cjo .get ("groupId" ).getAsLong ();
71- }
72- clanDescription = cjo .get ("about" ).getAsString ();
73- creationDate = StringUtils .valueOfZTime (cjo .get ("creationDate" ).getAsString ());
74- memberCount = cjo .get ("memberCount" ).getAsInt ();
75- isPublic = cjo .get ("isPublic" ).getAsBoolean ();
76- // motto = cjo.get("motto").getAsString();
77- allowChat = cjo .get ("allowChat" ).getAsBoolean ();
78-
79- }
80-
81- /**
82- * Search for all of the members in this clan that have the string in their name
83- */
84- public List <BungieUser > searchMembers (String name ) {
85- List <BungieUser > list = new LinkedList <>();
86-
87- for (BungieUser bungieUser : getMembers ()) {
88- if (bungieUser .getDisplayName ().contains (name )) {
89- list .add (bungieUser );
90- }
91- }
92-
93- return list ;
9462 }
9563
9664 public String getClanID () {
65+ if (clanId == -1 ) {
66+ clanId = getDetail ().get ("groupId" ).getAsLong ();
67+ }
9768 return clanId + "" ;
9869 }
9970
10071 public String getClanName () {
72+ if (clanName == null ) {
73+ getDetail ().get ("name" ).getAsString ();
74+ }
10175 return clanName ;
10276 }
10377
104- public String getClanDescription () { return clanDescription ; }
78+ public String getClanDescription () {
79+ if (clanDescription == null ) {
80+ clanDescription = getDetail ().get ("about" ).getAsString ();
81+ }
82+ return clanDescription ;
83+ }
10584
106- public Date getCreationDate () { return creationDate ; }
85+ public Date getCreationDate () {
86+ if (creationDate == null ) {
87+ creationDate = StringUtils .valueOfZTime (getDetail ().get ("creationDate" ).getAsString ());
88+ }
89+ return creationDate ;
90+ }
10791
108- public int getMemberCount () { return memberCount ; }
92+ public int getMemberCount () {
93+ if (memberCount == -1 ) {
94+ memberCount = getDetail ().get ("memberCount" ).getAsInt ();
95+ }
96+ return memberCount ;
97+ }
10998
110- public boolean isPublic () { return isPublic ; }
99+ public boolean isPublic () {
100+ return getDetail ().get ("isPublic" ).getAsBoolean ();
101+ }
111102
112- public String getMotto () { return motto ; }
103+ public String getMotto () {
104+ if (motto == null ) {
105+ motto = getDetail ().get ("motto" ).getAsString ();
106+ }
107+ return motto ;
108+ }
113109
114- public boolean isAllowChat () { return allowChat ; }
110+ public boolean isAllowChat () {
111+ return getDetail ().get ("allowChat" ).getAsBoolean ();
112+ }
115113
116- public BungieUser getFounder () { return new BungieUser (jo .getAsJsonObject ("founder" ).getAsJsonObject ("destinyUserInfo" ).get ("membershipId" ).getAsString ()); }
114+ public BungieUser getFounder () { return new BungieUser (getJO () .getAsJsonObject ("founder" ).getAsJsonObject ("destinyUserInfo" ).get ("membershipId" ).getAsString ()); }
117115
118116 /**
119117 * Returns a list of the founder and the admins of the clan
@@ -124,20 +122,21 @@ public List<BungieUser> getAdmins() {
124122 if (admins != null ) { return admins ; }
125123
126124 List <BungieUser > temp = new ArrayList <>();
127- JsonArray ja = hu .urlRequestGET ("https://www.bungie.net/Platform/GroupV2/" + clanId + "/AdminsAndFounder/?components=200" ).get ("Response" ).getAsJsonObject (). get ( "results" ). getAsJsonArray ( );
125+ JsonArray ja = hu .urlRequestGET ("https://www.bungie.net/Platform/GroupV2/" + clanId + "/AdminsAndFounder/?components=200" ).getAsJsonObject ("Response" ).getAsJsonArray ( "results" );
128126
129127 for (JsonElement je : ja ) {
130128 temp .add (new BungieUser (je .getAsJsonObject ().get ("destinyUserInfo" ).getAsJsonObject ().get ("membershipId" ).getAsString ()));
131129 }
132130
131+ admins = temp ; // Cache this information
133132 return temp ;
134133 }
135134
136135 /**
137136 * Returns the average number of days since all members last played
138137 */
139138 public double getAverageInactivityAmongMembers () {
140- ArrayList <Double > averages = new ArrayList <Double >();
139+ ArrayList <Double > averages = new ArrayList <>();
141140 int a = 0 ;
142141 for (BungieUser bu : this .getMembers ()) {
143142 averages .add (bu .getDaysSinceLastPlayed ());
@@ -178,39 +177,23 @@ public List<BungieUser> getMostInactiveMembers(int numberOfResults, String... ex
178177 }
179178
180179 /**
181- * Returns a list of all members of the clan
182- * Now deprecated in favor of getMembers()
180+ * Search for all of the members in this clan that have the string in their name
183181 */
184- @ Deprecated
185- public List <BungieUser > getMembersOld () {
186- List <BungieUser > temp = new ArrayList <>();
187-
188- if (members != null ) {
189- return members ;
190- }
191-
192- if (jj == null ) {
193- jj = hu .urlRequestGET ("https://www.bungie.net/Platform/GroupV2/" + clanId + "/Members/" ).get ("Response" ).getAsJsonObject ();
194- }
195-
196- for (JsonElement je : jj .getAsJsonArray ("results" )) {
197- CompletableFuture <BungieUser > cf = new CompletableFuture <>();
182+ public List <BungieUser > searchMembers (String name ) {
183+ List <BungieUser > list = new LinkedList <>();
198184
199- cf .completeAsync (() -> new BungieUser (je .getAsJsonObject ().getAsJsonObject ("destinyUserInfo" ).get ("membershipId" ).getAsString ()));
200- try {
201- temp .add (cf .get ());
202- } catch (InterruptedException | ExecutionException e ) {
203- System .out .println ("Returned a null list of users for the clan in Clan.getMembers()" );
204- return null ;
185+ for (BungieUser bungieUser : getMembers ()) {
186+ if (bungieUser .getDisplayName ().contains (name )) {
187+ list .add (bungieUser );
205188 }
206189 }
207- members = temp ;
208- return temp ;
190+
191+ return list ;
209192 }
210193
211194 /**
212- * Old getExperimental method
213- * Does not cache valus anymore, now the developer is responsible for that
195+ * Newly reworked
196+ * Does not cache values anymore, now the developer is responsible for that
214197 * Should be faster than before
215198 */
216199 public List <BungieUser > getMembers () {
@@ -245,17 +228,35 @@ public List<BungieUser> getMembers() {
245228 return source ;
246229 }
247230
248- private int [] splitIntoParts (int whole , int parts ) {
249- int [] arr = new int [parts ];
250- int remain = whole ;
251- int partsLeft = parts ;
252- for (int i = 0 ; partsLeft > 0 ; i ++) {
253- int size = (remain + partsLeft - 1 ) / partsLeft ; // rounded up, aka ceiling
254- arr [i ] = size ;
255- remain -= size ;
256- partsLeft --;
231+ /**
232+ * Returns a list of all members of the clan
233+ * Now deprecated in favor of getMembers()
234+ */
235+ @ Deprecated
236+ public List <BungieUser > getMembersOld () {
237+ List <BungieUser > temp = new ArrayList <>();
238+
239+ if (members != null ) {
240+ return members ;
257241 }
258- return arr ;
242+
243+ if (jj == null ) {
244+ jj = hu .urlRequestGET ("https://www.bungie.net/Platform/GroupV2/" + clanId + "/Members/" ).get ("Response" ).getAsJsonObject ();
245+ }
246+
247+ for (JsonElement je : jj .getAsJsonArray ("results" )) {
248+ CompletableFuture <BungieUser > cf = new CompletableFuture <>();
249+
250+ cf .completeAsync (() -> new BungieUser (je .getAsJsonObject ().getAsJsonObject ("destinyUserInfo" ).get ("membershipId" ).getAsString ()));
251+ try {
252+ temp .add (cf .get ());
253+ } catch (InterruptedException | ExecutionException e ) {
254+ System .out .println ("Returned a null list of users for the clan in Clan.getMembers()" );
255+ return null ;
256+ }
257+ }
258+ members = temp ;
259+ return temp ;
259260 }
260261
261262 /**
@@ -276,7 +277,7 @@ public boolean isMember(String bungieID) {
276277 }
277278
278279 /**
279- * Retrieve a JsonObject depicting the top stats of the clna
280+ * Retrieve a JsonObject depicting the top stats of the clan
280281 * Unfortunately does not say who has those top stats
281282 */
282283 public JsonObject getClanStats (ActivityMode ... filter ) {
@@ -297,7 +298,6 @@ public Date getJoinDate(BungieUser member) {
297298 jj = hu .urlRequestGET ("https://www.bungie.net/Platform/GroupV2/" + clanId + "/Members/" ).get ("Response" ).getAsJsonObject ();
298299 }
299300
300- List <String > ids = new ArrayList <>();
301301 for (JsonElement je : jj .getAsJsonArray ("results" )) {
302302 if (member .getBungieMembershipID ().equals (je .getAsJsonObject ().getAsJsonObject ("destinyUserInfo" ).get ("membershipId" ).getAsString ())) {
303303 return StringUtils .valueOfZTime (je .getAsJsonObject ().get ("joinDate" ).getAsString ());
@@ -316,7 +316,9 @@ public ClanManagement getClanManagement() {
316316 return clanManagement ;
317317 }
318318
319- class MemberThread extends Thread {
319+ // Util Methods / Classes used elsewhere
320+
321+ private class MemberThread extends Thread {
320322
321323 public MemberThread (List <BungieUser > source , List <String > list ) {
322324 for (String string : list ) {
@@ -328,4 +330,21 @@ public void run() {
328330
329331 }
330332 }
333+
334+ private int [] splitIntoParts (int whole , int parts ) {
335+ int [] arr = new int [parts ];
336+ int remain = whole ;
337+ int partsLeft = parts ;
338+ for (int i = 0 ; partsLeft > 0 ; i ++) {
339+ int size = (remain + partsLeft - 1 ) / partsLeft ; // rounded up, aka ceiling
340+ arr [i ] = size ;
341+ remain -= size ;
342+ partsLeft --;
343+ }
344+ return arr ;
345+ }
346+
347+ private JsonObject getDetail () {
348+ return getJO ().getAsJsonObject ("detail" );
349+ }
331350}
0 commit comments