66import java .util .Map ;
77import java .util .Optional ;
88import java .util .concurrent .CompletableFuture ;
9- import java .util .concurrent .CompletionStage ;
109import java .util .concurrent .ConcurrentHashMap ;
1110import java .util .stream .Collectors ;
1211import java .util .stream .StreamSupport ;
2120import okhttp3 .OkHttpClient ;
2221import org .jetbrains .annotations .Nullable ;
2322
23+ /**
24+ * API Class. Create an instance of this using {@link #builder()} to use the API.
25+ */
2426@ Builder
2527public final class CodeBottle {
26- public final CompletionStage <Void > lazyLoading = CompletableFuture .allOf (requestLanguages (), requestCategories ());
27-
2828 private final Map <String , Language > languageCache = new ConcurrentHashMap <>();
2929 private final Map <String , Category > categoryCache = new ConcurrentHashMap <>();
3030 private final Map <String , Snippet > snippetCache = new ConcurrentHashMap <>();
31-
3231 @ Builder .Default
3332 private final @ Nullable String token = null ;
3433 @ Builder .Default
3534 private @ Getter final OkHttpClient httpClient = new OkHttpClient .Builder ().build ();
35+ /**
36+ * A {@link CompletableFuture} that completes once lazy loading was finished.
37+ * <p>
38+ * Lazy loading is calling {@link #requestLanguages()} and {@link #requestCategories()} on API construction.
39+ */
40+ public final CompletableFuture <Void > lazyLoading = CompletableFuture .allOf (requestLanguages (), requestCategories ());
41+
42+ /**
43+ * Waits for {@linkplain #lazyLoading lazy loading} to finish using {@link CompletableFuture#join()}.
44+ * This method is intended to be used for API chaining, otherwise it is recommended to wait for {@link #lazyLoading} yourself.
45+ *
46+ * @return this {@link CodeBottle} instance.
47+ */
48+ public CodeBottle waitForLazyLoading () {
49+ lazyLoading .join ();
50+
51+ return this ;
52+ }
3653
3754 @ SuppressWarnings ("ConstantConditions" )
3855 public Optional <String > getToken () {
3956 return Optional .ofNullable (token );
4057 }
4158
59+ /**
60+ * Returns the {@link Language} matching the given {@code id} if found.
61+ * Before {@link #lazyLoading} was completed, this will always return {@link Optional#empty()}.
62+ *
63+ * @param id is the ID of the desired language.
64+ *
65+ * @return the language matching the given {@code id}.
66+ */
4267 public Optional <Language > getLanguageByID (String id ) {
4368 return Optional .ofNullable (languageCache .get (id ));
4469 }
4570
71+ /**
72+ * Returns all cached {@link Language}s.
73+ * Before {@link #lazyLoading} was completed, the returned {@link Collection} will always be empty.
74+ *
75+ * @return a {@link Collection} of all cached languages.
76+ */
4677 public Collection <Language > getLanguages () {
4778 return languageCache .values ();
4879 }
4980
81+ /**
82+ * Requests the language by the given {@code id} and synchronizes the cache when deserializing the result.
83+ *
84+ * @param id is the {@code id} of the {@link Language} to request.
85+ *
86+ * @return a future that will complete with the requested {@link Language} after updating it in the cache.
87+ */
5088 public CompletableFuture <Language > requestLanguageByID (String id ) {
5189 return new CodeBottleRequest <Language >(this )
5290 .to (Endpoint .LANGUAGE_SPECIFIC , id )
@@ -62,6 +100,11 @@ public CompletableFuture<Language> requestLanguageByID(String id) {
62100 });
63101 }
64102
103+ /**
104+ * Requests all {@link Language}s and refreshes them in the cache.
105+ *
106+ * @return a future that will complete with all cached {@link Language}s.
107+ */
65108 public CompletableFuture <Collection <Language >> requestLanguages () {
66109 return new CodeBottleRequest <Collection <Language >>(this )
67110 .to (Endpoint .LANGUAGES )
@@ -83,14 +126,35 @@ public CompletableFuture<Collection<Language>> requestLanguages() {
83126 });
84127 }
85128
129+ /**
130+ * Returns the {@link Category} matching the given {@code id} if found in cache.
131+ * Before {@link #lazyLoading} was completed, this will always return {@link Optional#empty()}.
132+ *
133+ * @param id is the ID of the desired category.
134+ *
135+ * @return the category matching the given {@code id}.
136+ */
86137 public Optional <Category > getCategoryByID (String id ) {
87138 return Optional .ofNullable (categoryCache .get (id ));
88139 }
89140
141+ /**
142+ * Returns all cached {@linkplain Category categories}.
143+ * Before {@link #lazyLoading} was completed, the returned {@link Collection} will always be empty.
144+ *
145+ * @return a {@link Collection} of all cached categories.
146+ */
90147 public Collection <Category > getCategories () {
91148 return categoryCache .values ();
92149 }
93150
151+ /**
152+ * Requests the category by the given {@code id} and synchronizes the cache when deserializing the result.
153+ *
154+ * @param id is the {@code id} of the {@link Category} to request.
155+ *
156+ * @return a future that will complete with the requested {@link Category} after updating it in the cache.
157+ */
94158 public CompletableFuture <Category > requestCategoryByID (String id ) {
95159 return new CodeBottleRequest <Category >(this )
96160 .to (Endpoint .CATEGORY_SPECIFIC , id )
@@ -106,6 +170,11 @@ public CompletableFuture<Category> requestCategoryByID(String id) {
106170 });
107171 }
108172
173+ /**
174+ * Requests all {@link Category}s and refreshes them in the cache.
175+ *
176+ * @return a future that will complete with all cached {@linkplain Category categories}.
177+ */
109178 public CompletableFuture <Collection <Category >> requestCategories () {
110179 return new CodeBottleRequest <Collection <Category >>(this )
111180 .to (Endpoint .CATEGORIES )
@@ -127,14 +196,33 @@ public CompletableFuture<Collection<Category>> requestCategories() {
127196 });
128197 }
129198
199+ /**
200+ * Returns the {@link Snippet} matching the given {@code id} if found.
201+ *
202+ * @param id is the ID of the desired snippet.
203+ *
204+ * @return the snippet matching the given {@code id}.
205+ */
130206 public Optional <Snippet > getSnippetByID (String id ) {
131207 return Optional .ofNullable (snippetCache .get (id ));
132208 }
133209
210+ /**
211+ * Returns all cached {@link Snippet}s.
212+ *
213+ * @return a {@link Collection} of all cached snippets.
214+ */
134215 public Collection <Snippet > getSnippets () {
135216 return snippetCache .values ();
136217 }
137218
219+ /**
220+ * Requests the snippet by the given {@code id} and synchronizes the cache when deserializing the result.
221+ *
222+ * @param id is the {@code id} of the {@link Snippet} to request.
223+ *
224+ * @return a future that will complete with the requested {@link Snippet} after updating it in the cache.
225+ */
138226 public CompletableFuture <Snippet > requestSnippetByID (String id ) {
139227 return new CodeBottleRequest <Snippet >(this )
140228 .to (Endpoint .SNIPPET_SPECIFIC , id )
@@ -150,6 +238,11 @@ public CompletableFuture<Snippet> requestSnippetByID(String id) {
150238 });
151239 }
152240
241+ /**
242+ * Requests all {@link Snippet}s and refreshes them in the cache.
243+ *
244+ * @return a future that will complete with all cached {@link Snippet}s.
245+ */
153246 public CompletableFuture <Collection <Snippet >> requestSnippets () {
154247 return new CodeBottleRequest <Collection <Snippet >>(this )
155248 .to (Endpoint .SNIPPETS )
@@ -171,40 +264,70 @@ public CompletableFuture<Collection<Snippet>> requestSnippets() {
171264 });
172265 }
173266
267+ /**
268+ * Returns the {@link Snippet.Revision} matching the given {@code id pair} if found in cache.
269+ * Before this method will ever not return {@link Optional#empty()}, you must {@linkplain #requestAllRevisions() request all revisions}.
270+ *
271+ * @param id is the ID of the desired language.
272+ *
273+ * @return the language matching the given {@code id}.
274+ */
174275 public Optional <Snippet .Revision > getSnippetRevisionByID (String snippetId , int id ) throws IndexOutOfBoundsException {
175276 return Optional .ofNullable (snippetCache .get (snippetId ))
176277 .flatMap (snippet -> snippet .getRevisionByID (id ));
177278 }
178279
280+ /**
281+ * Returns all cached {@link Language}s.
282+ *
283+ * @return a {@link Collection} of all cached languages.
284+ */
179285 public Collection <Snippet .Revision > getSnippetRevisions () {
180286 return snippetCache .values ()
181287 .stream ()
182288 .flatMap (snippet -> snippet .getRevisions ().stream ())
183289 .collect (Collectors .toList ());
184290 }
185291
292+ /**
293+ * Requests the snippet revision by the given {@code id} and synchronizes the cache when deserializing the result.
294+ *
295+ * @param id is the {@code id} of the {@link Snippet.Revision} to request.
296+ *
297+ * @return a future that will complete with the requested {@link Snippet.Revision} after updating it in the cache.
298+ */
186299 public CompletableFuture <Snippet .Revision > requestSnippetRevisionByID (String snippetId , int id ) {
187300 return getSnippetByID (snippetId )
188301 .orElseGet (() -> requestSnippetByID (snippetId ).join ())
189302 .requestRevision (id );
190303 }
191304
192- public CompletableFuture <List <Snippet .Revision >> requestSnippetRevisions (String snippetId , int id ) {
305+ /**
306+ * Requests all {@link Snippet.Revision}s of the defined {@linkplain Snippet snippet id} and refreshes them in the cache.
307+ *
308+ * @return a future that will complete with all cached {@link Snippet.Revision}s.
309+ */
310+ public CompletableFuture <List <Snippet .Revision >> requestSnippetRevisions (String snippetId ) {
193311 return getSnippetByID (snippetId )
194312 .orElseGet (() -> requestSnippetByID (snippetId ).join ())
195313 .requestRevisions ();
196314 }
197-
315+
316+ /**
317+ * Requests all {@link Snippet.Revision}s and refreshes them in the cache.
318+ *
319+ * @return a future that will complete with all cached {@link Snippet.Revision}s.
320+ */
198321 public CompletableFuture <Collection <Snippet .Revision >> requestAllRevisions () {
199322 return requestSnippets ()
200323 .thenApply (snippets -> {
201324 Collection <Snippet .Revision > yields = new ArrayList <>();
202-
325+
203326 (snippets .size () > 200 ? snippets .parallelStream () : snippets .stream ())
204327 .map (Snippet ::requestRevisions )
205328 .map (CompletableFuture ::join )
206329 .forEach (yields ::addAll );
207-
330+
208331 return yields ;
209332 });
210333 }
0 commit comments