11package io .codebottle .api ;
22
3+ import java .util .Collection ;
34import java .util .List ;
45import java .util .Map ;
56import java .util .Optional ;
67import java .util .concurrent .CompletableFuture ;
8+ import java .util .concurrent .CompletionStage ;
79import java .util .concurrent .ConcurrentHashMap ;
10+ import java .util .stream .Collectors ;
11+ import java .util .stream .StreamSupport ;
812
9- import com .fasterxml .jackson .databind .ObjectMapper ;
1013import io .codebottle .api .model .Category ;
1114import io .codebottle .api .model .Language ;
1215import io .codebottle .api .model .Snippet ;
1518import org .jetbrains .annotations .Nullable ;
1619
1720public final class CodeBottle {
21+ public final CompletionStage <Void > lazyLoading ;
22+
1823 private final Map <Integer , Language > languageCache = new ConcurrentHashMap <>();
1924 private final Map <Integer , Category > categoryCache = new ConcurrentHashMap <>();
2025 private final Map <Integer , Snippet > snippetCache = new ConcurrentHashMap <>();
21-
2226 private final String token ;
2327
2428 public CodeBottle () {
@@ -27,6 +31,8 @@ public CodeBottle() {
2731
2832 public CodeBottle (@ Nullable String token ) {
2933 this .token = token ;
34+
35+ this .lazyLoading = CompletableFuture .allOf (requestLanguages (), requestCategories ());
3036 }
3137
3238 public Optional <String > getToken () {
@@ -56,6 +62,27 @@ public CompletableFuture<Language> requestLanguageByID(int id) {
5662 });
5763 }
5864
65+ public CompletableFuture <Collection <Language >> requestLanguages () {
66+ return new CodeBottleRequest <Collection <Language >>(this )
67+ .to (Endpoint .LANGUAGES )
68+ .makeGET ()
69+ .then (data -> {
70+ synchronized (languageCache ) {
71+ return StreamSupport .stream (data .spliterator (), false )
72+ .map (node -> getLanguageByID (node .path ("id" ).asInt ())
73+ .map (entity -> entity .update (node ))
74+ .orElseGet (() -> {
75+ final Language language = new Language (this , node );
76+
77+ languageCache .compute (language .getID (), (k , v ) -> language );
78+
79+ return language ;
80+ }))
81+ .collect (Collectors .toList ());
82+ }
83+ });
84+ }
85+
5986 public Optional <Category > getCategoryByID (int id ) {
6087 if (id == -1 ) return Optional .empty ();
6188
@@ -79,6 +106,27 @@ public CompletableFuture<Category> requestCategoryByID(int id) {
79106 });
80107 }
81108
109+ public CompletableFuture <Collection <Category >> requestCategories () {
110+ return new CodeBottleRequest <Collection <Category >>(this )
111+ .to (Endpoint .CATEGORIES )
112+ .makeGET ()
113+ .then (data -> {
114+ synchronized (categoryCache ) {
115+ return StreamSupport .stream (data .spliterator (), false )
116+ .map (node -> getCategoryByID (node .path ("id" ).asInt ())
117+ .map (entity -> entity .update (node ))
118+ .orElseGet (() -> {
119+ final Category category = new Category (this , node );
120+
121+ categoryCache .compute (category .getID (), (k , v ) -> category );
122+
123+ return category ;
124+ }))
125+ .collect (Collectors .toList ());
126+ }
127+ });
128+ }
129+
82130 public Optional <Snippet > getSnippetByID (int id ) {
83131 if (id == -1 ) return Optional .empty ();
84132
@@ -102,6 +150,27 @@ public CompletableFuture<Snippet> requestSnippetByID(int id) {
102150 });
103151 }
104152
153+ public CompletableFuture <Collection <Snippet >> requestSnippets () {
154+ return new CodeBottleRequest <Collection <Snippet >>(this )
155+ .to (Endpoint .SNIPPETS )
156+ .makeGET ()
157+ .then (data -> {
158+ synchronized (snippetCache ) {
159+ return StreamSupport .stream (data .spliterator (), false )
160+ .map (node -> getSnippetByID (node .path ("id" ).asInt ())
161+ .map (entity -> entity .update (node ))
162+ .orElseGet (() -> {
163+ final Snippet snippet = new Snippet (this , node );
164+
165+ snippetCache .compute (snippet .getID (), (k , v ) -> snippet );
166+
167+ return snippet ;
168+ }))
169+ .collect (Collectors .toList ());
170+ }
171+ });
172+ }
173+
105174 public Optional <Snippet .Revision > getSnippetRevisionByID (int snippetId , int id ) throws IndexOutOfBoundsException {
106175 if (id == -1 ) return Optional .empty ();
107176
0 commit comments