Skip to content

Commit 82e8849

Browse files
author
Tobias Burdow [Kaleidox]
committed
Code Cleanup
1 parent 24b0f91 commit 82e8849

File tree

6 files changed

+104
-104
lines changed

6 files changed

+104
-104
lines changed

src/main/java/io/codebottle/api/CodeBottleAPI.java

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/io/codebottle/api/model/AbstractEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ protected AbstractEntity(CodeBottleAPI context, JsonNode data) {
1616
update(data);
1717
}
1818

19+
public abstract AbstractEntity update(JsonNode data);
20+
1921
public AbstractEntity(CodeBottleAPI context, int id) {
2022
this.context = context;
2123

@@ -29,6 +31,4 @@ public String getID() {
2931
public CodeBottleAPI getContext() {
3032
return context;
3133
}
32-
33-
public abstract AbstractEntity update(JsonNode data);
3434
}

src/main/java/io/codebottle/api/model/Snippet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public CompletableFuture<List<Revision>> requestRevisions() {
171171

172172
public static class Revision extends AbstractEntity implements Comparable<Revision> {
173173
public static final Comparator<Revision> REVISION_COMPARATOR = Comparator.comparingInt(entity -> Integer.parseInt(entity.getID()));
174-
174+
175175
private @JsonProperty(required = true) String title;
176176
private @JsonProperty @Nullable String description;
177177
private @JsonProperty(required = true) String code;
@@ -237,7 +237,7 @@ public Revision update(JsonNode data) {
237237
return this;
238238
}
239239

240-
@Override
240+
@Override
241241
@Contract(pure = true)
242242
public int compareTo(@NotNull Snippet.Revision other) {
243243
return REVISION_COMPARATOR.compare(this, other);

src/main/java/io/codebottle/api/rest/CodeBottleRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ public CodeBottleAPI getContext() {
6666
return context;
6767
}
6868

69+
public CodeBottleRequest<T> makeGET() {
70+
//noinspection ConstantConditions
71+
return make(Method.GET, null);
72+
}
73+
6974
public CodeBottleRequest<T> make(Method method, JsonNode withData) {
7075
httpRequest.method(method.name(), method == Method.GET ? null : RequestBody.create(withData.toString(), MediaType.parse("application/json")));
7176

7277
return this;
7378
}
7479

75-
public CodeBottleRequest<T> makeGET() {
76-
//noinspection ConstantConditions
77-
return make(Method.GET, null);
78-
}
79-
8080
public CodeBottleRequest<T> to(Endpoint endpoint, Object... at) throws IllegalArgumentException {
8181
final URL url = endpoint.url(at);
8282
httpRequest.url(url);

src/main/java/io/codebottle/api/rest/Endpoint.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ public enum Endpoint {
3636
this.requiredParameterCount = requiredParameterCount;
3737
}
3838

39-
public int getRequiredParameterCount() {
40-
return requiredParameterCount;
41-
}
42-
4339
public URL url(Object... args) throws IllegalArgumentException {
4440
final int parameterCount = getRequiredParameterCount();
4541

@@ -57,4 +53,8 @@ public URL url(Object... args) throws IllegalArgumentException {
5753
throw new AssertionError("Unexpected MalformedURLException", e);
5854
}
5955
}
56+
57+
public int getRequiredParameterCount() {
58+
return requiredParameterCount;
59+
}
6060
}

src/main/java/io/codebottle/api/rest/HTTPCodes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public final class HTTPCodes {
8585

8686
private static final Field[] fields = HTTPCodes.class.getFields();
8787

88+
public static String getString(int code) {
89+
return String.valueOf(getName(code)).toUpperCase() + '[' + code + ']';
90+
}
91+
8892
public static @Nullable String getName(int code) {
8993
try {
9094
for (Field field : fields)
@@ -96,8 +100,4 @@ public final class HTTPCodes {
96100

97101
return null;
98102
}
99-
100-
public static String getString(int code) {
101-
return String.valueOf(getName(code)).toUpperCase() + '[' + code + ']';
102-
}
103103
}

0 commit comments

Comments
 (0)