11package io .codebottle .api ;
22
33import java .util .Collection ;
4+ import java .util .Collections ;
45import java .util .List ;
56import java .util .Map ;
67import java .util .Optional ;
2021public final class CodeBottle {
2122 public final CompletionStage <Void > lazyLoading ;
2223
23- private final Map <Integer , Language > languageCache = new ConcurrentHashMap <>();
24- private final Map <Integer , Category > categoryCache = new ConcurrentHashMap <>();
25- private final Map <Integer , Snippet > snippetCache = new ConcurrentHashMap <>();
24+ private final Map <String , Language > languageCache = new ConcurrentHashMap <>();
25+ private final Map <String , Category > categoryCache = new ConcurrentHashMap <>();
26+ private final Map <String , Snippet > snippetCache = new ConcurrentHashMap <>();
2627 private final String token ;
2728
2829 public CodeBottle () {
@@ -38,16 +39,20 @@ public CodeBottle(@Nullable String token) {
3839 public Optional <String > getToken () {
3940 return Optional .ofNullable (token );
4041 }
41-
42- public Optional <Language > getLanguageByID (int id ) {
43- if (id == -1 ) return Optional .empty ();
44-
42+
43+ public Optional <Language > getLanguageByID (String id ) {
4544 synchronized (languageCache ) {
4645 return Optional .ofNullable (languageCache .get (id ));
4746 }
4847 }
4948
50- public CompletableFuture <Language > requestLanguageByID (int id ) {
49+ public Collection <Language > getLanguages () {
50+ synchronized (languageCache ) {
51+ return languageCache .values ();
52+ }
53+ }
54+
55+ public CompletableFuture <Language > requestLanguageByID (String id ) {
5156 return new CodeBottleRequest <Language >(this )
5257 .to (Endpoint .LANGUAGE_SPECIFIC , id )
5358 .makeGET ()
@@ -69,7 +74,7 @@ public CompletableFuture<Collection<Language>> requestLanguages() {
6974 .then (data -> {
7075 synchronized (languageCache ) {
7176 return StreamSupport .stream (data .spliterator (), false )
72- .map (node -> getLanguageByID (node .path ("id" ).asInt ())
77+ .map (node -> getLanguageByID (node .path ("id" ).asText ())
7378 .map (entity -> entity .update (node ))
7479 .orElseGet (() -> {
7580 final Language language = new Language (this , node );
@@ -83,15 +88,19 @@ public CompletableFuture<Collection<Language>> requestLanguages() {
8388 });
8489 }
8590
86- public Optional <Category > getCategoryByID (int id ) {
87- if (id == -1 ) return Optional .empty ();
88-
91+ public Optional <Category > getCategoryByID (String id ) {
8992 synchronized (categoryCache ) {
9093 return Optional .ofNullable (categoryCache .get (id ));
9194 }
9295 }
9396
94- public CompletableFuture <Category > requestCategoryByID (int id ) {
97+ public Collection <Category > getCategories () {
98+ synchronized (categoryCache ) {
99+ return categoryCache .values ();
100+ }
101+ }
102+
103+ public CompletableFuture <Category > requestCategoryByID (String id ) {
95104 return new CodeBottleRequest <Category >(this )
96105 .to (Endpoint .CATEGORY_SPECIFIC , id )
97106 .makeGET ()
@@ -113,7 +122,7 @@ public CompletableFuture<Collection<Category>> requestCategories() {
113122 .then (data -> {
114123 synchronized (categoryCache ) {
115124 return StreamSupport .stream (data .spliterator (), false )
116- .map (node -> getCategoryByID (node .path ("id" ).asInt ())
125+ .map (node -> getCategoryByID (node .path ("id" ).asText ())
117126 .map (entity -> entity .update (node ))
118127 .orElseGet (() -> {
119128 final Category category = new Category (this , node );
@@ -127,15 +136,19 @@ public CompletableFuture<Collection<Category>> requestCategories() {
127136 });
128137 }
129138
130- public Optional <Snippet > getSnippetByID (int id ) {
131- if (id == -1 ) return Optional .empty ();
132-
139+ public Optional <Snippet > getSnippetByID (String id ) {
133140 synchronized (snippetCache ) {
134141 return Optional .ofNullable (snippetCache .get (id ));
135142 }
136143 }
137144
138- public CompletableFuture <Snippet > requestSnippetByID (int id ) {
145+ public Collection <Snippet > getSnippets () {
146+ synchronized (snippetCache ) {
147+ return snippetCache .values ();
148+ }
149+ }
150+
151+ public CompletableFuture <Snippet > requestSnippetByID (String id ) {
139152 return new CodeBottleRequest <Snippet >(this )
140153 .to (Endpoint .SNIPPET_SPECIFIC , id )
141154 .makeGET ()
@@ -157,7 +170,7 @@ public CompletableFuture<Collection<Snippet>> requestSnippets() {
157170 .then (data -> {
158171 synchronized (snippetCache ) {
159172 return StreamSupport .stream (data .spliterator (), false )
160- .map (node -> getSnippetByID (node .path ("id" ).asInt ())
173+ .map (node -> getSnippetByID (node .path ("id" ).asText ())
161174 .map (entity -> entity .update (node ))
162175 .orElseGet (() -> {
163176 final Snippet snippet = new Snippet (this , node );
@@ -171,22 +184,29 @@ public CompletableFuture<Collection<Snippet>> requestSnippets() {
171184 });
172185 }
173186
174- public Optional <Snippet .Revision > getSnippetRevisionByID (int snippetId , int id ) throws IndexOutOfBoundsException {
175- if (id == -1 ) return Optional .empty ();
176-
187+ public Optional <Snippet .Revision > getSnippetRevisionByID (String snippetId , int id ) throws IndexOutOfBoundsException {
177188 synchronized (snippetCache ) {
178- return Optional .ofNullable (snippetCache .get (id ))
189+ return Optional .ofNullable (snippetCache .get (snippetId ))
179190 .flatMap (snippet -> snippet .getRevisionByID (id ));
180191 }
181192 }
193+
194+ public Collection <Snippet .Revision > getSnippetRevisions () {
195+ synchronized (snippetCache ) {
196+ return snippetCache .values ()
197+ .stream ()
198+ .flatMap (snippet -> snippet .getRevisions ().stream ())
199+ .collect (Collectors .toList ());
200+ }
201+ }
182202
183- public CompletableFuture <Snippet .Revision > requestSnippetRevision (int snippetId , int id ) {
203+ public CompletableFuture <Snippet .Revision > requestSnippetRevision (String snippetId , int id ) {
184204 return getSnippetByID (snippetId )
185205 .orElseGet (() -> requestSnippetByID (snippetId ).join ())
186206 .requestRevision (id );
187207 }
188208
189- public CompletableFuture <List <Snippet .Revision >> requestSnippetRevisions (int snippetId , int id ) {
209+ public CompletableFuture <List <Snippet .Revision >> requestSnippetRevisions (String snippetId , int id ) {
190210 return getSnippetByID (snippetId )
191211 .orElseGet (() -> requestSnippetByID (snippetId ).join ())
192212 .requestRevisions ();
0 commit comments