@@ -59,18 +59,6 @@ public Optional<String> getToken() {
5959 return Optional .ofNullable (token );
6060 }
6161
62- /**
63- * Returns the {@link Language} matching the given {@code id} if found.
64- * Before {@link #lazyLoading} was completed, this will always return {@link Optional#empty()}.
65- *
66- * @param id is the ID of the desired language.
67- *
68- * @return the language matching the given {@code id}.
69- */
70- public Optional <Language > getLanguageByID (String id ) {
71- return Optional .ofNullable (languageCache .get (id ));
72- }
73-
7462 /**
7563 * Returns all cached {@link Language}s.
7664 * Before {@link #lazyLoading} was completed, the returned {@link Collection} will always be empty.
@@ -103,6 +91,18 @@ public CompletableFuture<Language> requestLanguageByID(String id) {
10391 });
10492 }
10593
94+ /**
95+ * Returns the {@link Language} matching the given {@code id} if found.
96+ * Before {@link #lazyLoading} was completed, this will always return {@link Optional#empty()}.
97+ *
98+ * @param id is the ID of the desired language.
99+ *
100+ * @return the language matching the given {@code id}.
101+ */
102+ public Optional <Language > getLanguageByID (String id ) {
103+ return Optional .ofNullable (languageCache .get (id ));
104+ }
105+
106106 /**
107107 * Requests all {@link Language}s and refreshes them in the cache.
108108 *
@@ -129,18 +129,6 @@ public CompletableFuture<Collection<Language>> requestLanguages() {
129129 });
130130 }
131131
132- /**
133- * Returns the {@link Category} matching the given {@code id} if found in cache.
134- * Before {@link #lazyLoading} was completed, this will always return {@link Optional#empty()}.
135- *
136- * @param id is the ID of the desired category.
137- *
138- * @return the category matching the given {@code id}.
139- */
140- public Optional <Category > getCategoryByID (String id ) {
141- return Optional .ofNullable (categoryCache .get (id ));
142- }
143-
144132 /**
145133 * Returns all cached {@linkplain Category categories}.
146134 * Before {@link #lazyLoading} was completed, the returned {@link Collection} will always be empty.
@@ -173,6 +161,18 @@ public CompletableFuture<Category> requestCategoryByID(String id) {
173161 });
174162 }
175163
164+ /**
165+ * Returns the {@link Category} matching the given {@code id} if found in cache.
166+ * Before {@link #lazyLoading} was completed, this will always return {@link Optional#empty()}.
167+ *
168+ * @param id is the ID of the desired category.
169+ *
170+ * @return the category matching the given {@code id}.
171+ */
172+ public Optional <Category > getCategoryByID (String id ) {
173+ return Optional .ofNullable (categoryCache .get (id ));
174+ }
175+
176176 /**
177177 * Requests all {@link Category}s and refreshes them in the cache.
178178 *
@@ -199,17 +199,6 @@ public CompletableFuture<Collection<Category>> requestCategories() {
199199 });
200200 }
201201
202- /**
203- * Returns the {@link Snippet} matching the given {@code id} if found.
204- *
205- * @param id is the ID of the desired snippet.
206- *
207- * @return the snippet matching the given {@code id}.
208- */
209- public Optional <Snippet > getSnippetByID (String id ) {
210- return Optional .ofNullable (snippetCache .get (id ));
211- }
212-
213202 /**
214203 * Returns all cached {@link Snippet}s.
215204 *
@@ -219,54 +208,6 @@ public Collection<Snippet> getSnippets() {
219208 return snippetCache .values ();
220209 }
221210
222- /**
223- * Requests the snippet by the given {@code id} and synchronizes the cache when deserializing the result.
224- *
225- * @param id is the {@code id} of the {@link Snippet} to request.
226- *
227- * @return a future that will complete with the requested {@link Snippet} after updating it in the cache.
228- */
229- public CompletableFuture <Snippet > requestSnippetByID (String id ) {
230- return new CodeBottleRequest <Snippet >(this )
231- .to (Endpoint .SNIPPET_SPECIFIC , id )
232- .makeGET ()
233- .then (data -> {
234- synchronized (snippetCache ) {
235- return getSnippetByID (id )
236- .map (entity -> entity .update (data ))
237- .orElseGet (() -> snippetCache .compute (id ,
238- // existing values don't matter here, as the cache access failed before
239- (k , v ) -> new Snippet (this , data )));
240- }
241- });
242- }
243-
244- /**
245- * Requests all {@link Snippet}s and refreshes them in the cache.
246- *
247- * @return a future that will complete with all cached {@link Snippet}s.
248- */
249- public CompletableFuture <Collection <Snippet >> requestSnippets () {
250- return new CodeBottleRequest <Collection <Snippet >>(this )
251- .to (Endpoint .SNIPPETS )
252- .makeGET ()
253- .then (data -> {
254- synchronized (snippetCache ) {
255- return StreamSupport .stream (data .spliterator (), false )
256- .map (node -> getSnippetByID (node .path ("id" ).asText ())
257- .map (entity -> entity .update (node ))
258- .orElseGet (() -> {
259- final Snippet snippet = new Snippet (this , node );
260-
261- snippetCache .compute (snippet .getID (), (k , v ) -> snippet );
262-
263- return snippet ;
264- }))
265- .collect (Collectors .toList ());
266- }
267- });
268- }
269-
270211 /**
271212 * Returns the {@link Snippet.Revision} matching the given {@code id pair} if found in cache.
272213 * Before this method will ever not return {@link Optional#empty()}, you must {@linkplain #requestAllRevisions() request all revisions}.
@@ -305,6 +246,39 @@ public CompletableFuture<Snippet.Revision> requestSnippetRevisionByID(String sni
305246 .requestRevision (id );
306247 }
307248
249+ /**
250+ * Returns the {@link Snippet} matching the given {@code id} if found.
251+ *
252+ * @param id is the ID of the desired snippet.
253+ *
254+ * @return the snippet matching the given {@code id}.
255+ */
256+ public Optional <Snippet > getSnippetByID (String id ) {
257+ return Optional .ofNullable (snippetCache .get (id ));
258+ }
259+
260+ /**
261+ * Requests the snippet by the given {@code id} and synchronizes the cache when deserializing the result.
262+ *
263+ * @param id is the {@code id} of the {@link Snippet} to request.
264+ *
265+ * @return a future that will complete with the requested {@link Snippet} after updating it in the cache.
266+ */
267+ public CompletableFuture <Snippet > requestSnippetByID (String id ) {
268+ return new CodeBottleRequest <Snippet >(this )
269+ .to (Endpoint .SNIPPET_SPECIFIC , id )
270+ .makeGET ()
271+ .then (data -> {
272+ synchronized (snippetCache ) {
273+ return getSnippetByID (id )
274+ .map (entity -> entity .update (data ))
275+ .orElseGet (() -> snippetCache .compute (id ,
276+ // existing values don't matter here, as the cache access failed before
277+ (k , v ) -> new Snippet (this , data )));
278+ }
279+ });
280+ }
281+
308282 /**
309283 * Requests all {@link Snippet.Revision}s of the defined {@linkplain Snippet snippet id} and refreshes them in the cache.
310284 *
@@ -335,6 +309,32 @@ public CompletableFuture<Collection<Snippet.Revision>> requestAllRevisions() {
335309 });
336310 }
337311
312+ /**
313+ * Requests all {@link Snippet}s and refreshes them in the cache.
314+ *
315+ * @return a future that will complete with all cached {@link Snippet}s.
316+ */
317+ public CompletableFuture <Collection <Snippet >> requestSnippets () {
318+ return new CodeBottleRequest <Collection <Snippet >>(this )
319+ .to (Endpoint .SNIPPETS )
320+ .makeGET ()
321+ .then (data -> {
322+ synchronized (snippetCache ) {
323+ return StreamSupport .stream (data .spliterator (), false )
324+ .map (node -> getSnippetByID (node .path ("id" ).asText ())
325+ .map (entity -> entity .update (node ))
326+ .orElseGet (() -> {
327+ final Snippet snippet = new Snippet (this , node );
328+
329+ snippetCache .compute (snippet .getID (), (k , v ) -> snippet );
330+
331+ return snippet ;
332+ }))
333+ .collect (Collectors .toList ());
334+ }
335+ });
336+ }
337+
338338 public OkHttpClient getHttpClient () {
339339 return httpClient ;
340340 }
@@ -349,15 +349,15 @@ public Optional<String> getToken() {
349349 return Optional .ofNullable (token );
350350 }
351351
352+ public void removeToken () {
353+ setToken (null );
354+ }
355+
352356 @ Deprecated
353357 public void setToken (@ Nullable String token ) {
354358 this .token = token ;
355359 }
356360
357- public void removeToken () {
358- setToken (null );
359- }
360-
361361 public Optional <OkHttpClient > getHttpClient () {
362362 return Optional .ofNullable (httpClient );
363363 }
0 commit comments