Skip to content

Commit 3946f93

Browse files
committed
refactor: format code for improved readability and consistency
1 parent 903fe65 commit 3946f93

File tree

7 files changed

+92
-82
lines changed

7 files changed

+92
-82
lines changed

src/main/java/de/joshicodes/javashock/JavaShock.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public JavaShock(final String token, final String apiHost) {
4848
/**
4949
* Retrieves all shockers and their hubs from the API.
5050
* They will automatically be cached.
51+
*
5152
* @return The RestAction to queue or execute
5253
*/
5354
public RestAction<HashMap<DeviceHub, List<Shocker>>> retrieveAllShockers() {
@@ -58,7 +59,8 @@ public RestAction<HashMap<DeviceHub, List<Shocker>>> retrieveAllShockers() {
5859
* Caches a hub with its shockers.
5960
* <b>Does NOT create a new hub or shocker for the user.</b>
6061
* This Method is designed to be used internally, use with caution.
61-
* @param hub The hub to cache
62+
*
63+
* @param hub The hub to cache
6264
* @param shockers The shockers to cache
6365
*/
6466
public void registerHub(final DeviceHub hub, final List<Shocker> shockers) {
@@ -70,7 +72,8 @@ public void registerHub(final DeviceHub hub, final List<Shocker> shockers) {
7072
* Caches a shocker in a hub.
7173
* <b>Does NOT create a new hub or shocker for the user.</b>
7274
* This Method is designed to be used internally, use with caution.
73-
* @param hub The hub to cache the shocker in
75+
*
76+
* @param hub The hub to cache the shocker in
7477
* @param shocker The shocker to cache
7578
*/
7679
public void registerShocker(final DeviceHub hub, final Shocker shocker) {
@@ -84,14 +87,14 @@ public void registerShocker(final DeviceHub hub, final Shocker shocker) {
8487
/**
8588
* Retrieves a shocker by its ID.
8689
* If the shocker is not cached, it will be fetched from the API.
90+
*
8791
* @param shockerId The ID of the shocker
8892
* @return The shocker or null if it does not exist
89-
*
9093
* @see #getCachedShocker(String) #getCachedShocker(String) - to fetch the shocker from the cache
9194
* @see #retrieveShocker(String) #retrieveShocker(String) - to always fetch the shocker from the API
9295
*/
9396
public RestAction<Shocker> getShocker(final String shockerId) {
94-
if(cachedHubs
97+
if (cachedHubs
9598
.stream()
9699
.anyMatch(hub -> hub.getShockers().stream().anyMatch(shock -> shock.getId().equals(shockerId)))
97100
) {
@@ -112,9 +115,9 @@ public RestAction<Shocker> getShocker(final String shockerId) {
112115
/**
113116
* Retrieves a shocker by its ID.
114117
* If the shocker is not cached, null will be returned.
118+
*
115119
* @param shockerId The ID of the shocker
116120
* @return The shocker or null if it does not exist
117-
*
118121
* @see #getShocker(String) - to fetch the shocker from cache or alternatively from the API
119122
* @see #retrieveShocker(String) - to always fetch the shocker from the API
120123
*/
@@ -126,9 +129,9 @@ public Shocker getCachedShocker(final String shockerId) {
126129
* Retrieves a shocker by its ID.
127130
* This method will always fetch the shocker from the API.
128131
* Try to use {@link #getShocker(String)} instead.
132+
*
129133
* @param shockerId The ID of the shocker
130134
* @return The RestAction to queue or execute
131-
*
132135
* @see #getShocker(String) - to fetch the shocker from cache or alternatively from the API
133136
* @see #getCachedShocker(String) - to fetch the shocker from the cache
134137
*/
@@ -139,10 +142,10 @@ public RestAction<Shocker> retrieveShocker(final String shockerId) {
139142
"GET",
140143
resp -> {
141144
final JsonElement element = resp.getAsJsonElement();
142-
if(element == null) return null;
143-
if(!element.isJsonObject()) return null;
145+
if (element == null) return null;
146+
if (!element.isJsonObject()) return null;
144147
final JsonObject object = element.getAsJsonObject();
145-
if(!object.has("data")) return null;
148+
if (!object.has("data")) return null;
146149
final JsonObject data = object.getAsJsonObject("data");
147150
return Shocker.fromJson(this, data);
148151
}
@@ -154,11 +157,12 @@ public RestAction<Shocker> retrieveShocker(final String shockerId) {
154157
* If hubs are fetched from the API, they do not contain their shockers.
155158
* Use {@link #retrieveAllShockers()} to retrieve all shockers and hubs instead.
156159
* <b>This method does not fetch the hub from the api.</b>
160+
*
157161
* @param hubId The ID of the hub
158162
* @return The hub or null if it does not exist
159163
*/
160164
public DeviceHub getHub(String hubId) {
161-
if(
165+
if (
162166
cachedHubs
163167
.stream()
164168
.anyMatch(hub -> hub.getId().equals(hubId))

src/main/java/de/joshicodes/javashock/action/GetShockerAction.java

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,47 @@ public GetShockerAction(final JavaShock instance) {
2020
"/1/shockers/own",
2121
"GET",
2222
(resp) -> {
23-
final JsonElement json = resp.getAsJsonElement();
24-
final HashMap<DeviceHub, List<Shocker>> list = new HashMap<>();
25-
if(json == null) return list;
26-
if(!json.isJsonObject()) return list;
27-
final JsonObject object = json.getAsJsonObject();
28-
if(!object.has("data")) return list;
29-
final JsonElement dataElement = object.get("data");
30-
if(!dataElement.isJsonArray()) return list;
31-
final JsonArray data = dataElement.getAsJsonArray();
32-
for(final JsonElement hubElement : data) {
33-
if(!hubElement.isJsonObject()) continue;
34-
final JsonObject hubObject = hubElement.getAsJsonObject();
35-
final String hubId = JsonUtil.getString(hubObject, "id");
36-
final String hubName = JsonUtil.getString(hubObject, "name");
37-
if(hubId == null || hubName == null) continue;
38-
final DeviceHub hub = new DeviceHub(instance, hubId, hubName);
39-
if(!hubObject.has("shockers")) continue;
40-
final JsonElement shockersElement = hubObject.get("shockers");
41-
if(!shockersElement.isJsonArray()) continue;
42-
final JsonArray shockersArray = shockersElement.getAsJsonArray();
43-
final List<Shocker> shockers = new ArrayList<>();
44-
for(final JsonElement shockerElement : shockersArray) {
45-
if(!shockerElement.isJsonObject()) continue;
46-
final JsonObject shockerObject = shockerElement.getAsJsonObject();
47-
final String name = JsonUtil.getString(shockerObject, "name");
48-
final String id = JsonUtil.getString(shockerObject, "id");
49-
final boolean isPaused = JsonUtil.getBoolean(shockerObject, "isPaused");
50-
final long rfId = JsonUtil.getLong(shockerObject, "rfId");
51-
final String model = JsonUtil.getString(shockerObject, "model");
52-
if(name == null || id == null || model == null) continue;
53-
final Shocker shocker = new Shocker(instance, id, name, rfId, model, isPaused, hub.getId());
54-
shockers.add(shocker);
55-
}
56-
instance.registerHub(
57-
hub,
58-
shockers
59-
);
60-
list.put(hub, shockers);
61-
}
62-
return list;
63-
});
23+
final JsonElement json = resp.getAsJsonElement();
24+
final HashMap<DeviceHub, List<Shocker>> list = new HashMap<>();
25+
if (json == null) return list;
26+
if (!json.isJsonObject()) return list;
27+
final JsonObject object = json.getAsJsonObject();
28+
if (!object.has("data")) return list;
29+
final JsonElement dataElement = object.get("data");
30+
if (!dataElement.isJsonArray()) return list;
31+
final JsonArray data = dataElement.getAsJsonArray();
32+
for (final JsonElement hubElement : data) {
33+
if (!hubElement.isJsonObject()) continue;
34+
final JsonObject hubObject = hubElement.getAsJsonObject();
35+
final String hubId = JsonUtil.getString(hubObject, "id");
36+
final String hubName = JsonUtil.getString(hubObject, "name");
37+
if (hubId == null || hubName == null) continue;
38+
final DeviceHub hub = new DeviceHub(instance, hubId, hubName);
39+
if (!hubObject.has("shockers")) continue;
40+
final JsonElement shockersElement = hubObject.get("shockers");
41+
if (!shockersElement.isJsonArray()) continue;
42+
final JsonArray shockersArray = shockersElement.getAsJsonArray();
43+
final List<Shocker> shockers = new ArrayList<>();
44+
for (final JsonElement shockerElement : shockersArray) {
45+
if (!shockerElement.isJsonObject()) continue;
46+
final JsonObject shockerObject = shockerElement.getAsJsonObject();
47+
final String name = JsonUtil.getString(shockerObject, "name");
48+
final String id = JsonUtil.getString(shockerObject, "id");
49+
final boolean isPaused = JsonUtil.getBoolean(shockerObject, "isPaused");
50+
final long rfId = JsonUtil.getLong(shockerObject, "rfId");
51+
final String model = JsonUtil.getString(shockerObject, "model");
52+
if (name == null || id == null || model == null) continue;
53+
final Shocker shocker = new Shocker(instance, id, name, rfId, model, isPaused, hub.getId());
54+
shockers.add(shocker);
55+
}
56+
instance.registerHub(
57+
hub,
58+
shockers
59+
);
60+
list.put(hub, shockers);
61+
}
62+
return list;
63+
});
6464
}
6565

6666
}

src/main/java/de/joshicodes/javashock/action/RestAction.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public RestAction(final JavaShock instance, final String endpoint, final String
4343

4444
public RestAction(final JavaShock instance, final String endpoint, final String method) {
4545
this(instance, endpoint, method, Function.identity(), (response) -> {
46-
if(response.httpResponse.statusCode() != 200)
46+
if (response.httpResponse.statusCode() != 200)
4747
throw new RuntimeException("Request failed with status code " + response.httpResponse.statusCode());
4848
return null;
4949
});
@@ -71,8 +71,8 @@ public void queue() {
7171
/**
7272
* Executes the request asynchronously. <br>
7373
* If you want to execute the request synchronously, use {@link #execute()} instead.
74-
* @param success The consumer that will be called when the request was successful. Can be null.
7574
*
75+
* @param success The consumer that will be called when the request was successful. Can be null.
7676
* @see #execute()
7777
* @see #queue()
7878
* @see #queue(Consumer, Consumer)
@@ -86,9 +86,9 @@ public void queue(final Consumer<T> success) {
8686
/**
8787
* Executes the request asynchronously. <br>
8888
* If you want to execute the request synchronously, use {@link #execute()} instead.
89+
*
8990
* @param success The consumer that will be called when the request was successful. Can be null.
9091
* @param failure The consumer that will be called when the request failed. Can be null.
91-
*
9292
* @see #execute()
9393
* @see #queue()
9494
* @see #queue(Consumer)
@@ -97,16 +97,17 @@ public void queue(final Consumer<T> success, final Consumer<Throwable> failure)
9797
CompletableFuture.runAsync(() -> {
9898
try {
9999
final T result = execute();
100-
if(success != null) success.accept(result);
100+
if (success != null) success.accept(result);
101101
} catch (Throwable e) {
102-
if(failure != null) failure.accept(e);
102+
if (failure != null) failure.accept(e);
103103
}
104104
}).join();
105105
}
106106

107107
/**
108108
* Executes the request and returns the result. This method is blocking. <br>
109109
* If you want to execute the request asynchronously, use {@link #queue()} instead.
110+
*
110111
* @return The result of the request
111112
*/
112113
public T execute() {
@@ -129,7 +130,7 @@ protected <C> HttpResponse<C> sendRequest(HttpClient client, HttpRequest request
129130
try {
130131
return client.send(request, handler);
131132
} catch (Exception e) {
132-
if(retries > 0) {
133+
if (retries > 0) {
133134
return sendRequest(client, request, handler, retries - 1);
134135
}
135136
throw new RuntimeException(e);
@@ -154,7 +155,7 @@ protected HttpRequest.Builder buildRequest() {
154155
.uri(URI.create(url))
155156
.method(method, body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(body));
156157
// Add body
157-
if(body != null && !method.equals("GET")) {
158+
if (body != null && !method.equals("GET")) {
158159
request = request.header("Content-Type", contentType == null ? "application/json" : contentType);
159160
// send content length
160161
//request = request.header("Content-Length", String.valueOf(body.length()));
@@ -163,7 +164,7 @@ protected HttpRequest.Builder buildRequest() {
163164
}
164165
// Apply headers
165166
getHeaders().forEach(request::header);
166-
if(clientModifier != null) {
167+
if (clientModifier != null) {
167168
request = clientModifier.apply(request);
168169
}
169170
return request;
@@ -172,25 +173,25 @@ protected HttpRequest.Builder buildRequest() {
172173
public record RestResponse<A>(HttpResponse<A> httpResponse, Class<A> aClass) {
173174

174175
public String rawBody() {
175-
if(httpResponse.body() == null)
176+
if (httpResponse.body() == null)
176177
return null;
177178
return String.valueOf(httpResponse.body());
178179
}
179180

180181
public JsonElement getAsJsonElement() {
181-
if(httpResponse.statusCode() != 200)
182+
if (httpResponse.statusCode() != 200)
182183
return null;
183-
if(httpResponse.body() == null)
184+
if (httpResponse.body() == null)
184185
return null;
185186
String body = String.valueOf(httpResponse.body());
186-
if(body.isEmpty())
187+
if (body.isEmpty())
187188
return null;
188189
return JsonParser.parseString(body);
189190
}
190191

191192
public JsonObject getAsJsonObject() {
192193
JsonElement element = getAsJsonElement();
193-
if(element == null || !element.isJsonObject())
194+
if (element == null || !element.isJsonObject())
194195
return new JsonObject();
195196
return element.getAsJsonObject();
196197
}

src/main/java/de/joshicodes/javashock/action/SimpleAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* Just returns the object that was passed to the constructor.
9+
*
910
* @param <T>
1011
*/
1112
public class SimpleAction<T> extends RestAction<T> {

src/main/java/de/joshicodes/javashock/action/control/ControlData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public record ControlData(ControlType type, int intensity, long duration) {
44

55
public enum ControlType {
6+
67
SHOCK,
78
VIBRATE,
89
SOUND,

src/main/java/de/joshicodes/javashock/action/control/ControlRequestAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ControlRequestAction extends RestAction<Boolean> {
1414

1515
public ControlRequestAction(JavaShock instance) {
1616
super(instance, "/2/shockers/control", "POST", resp -> {
17-
if(resp.httpResponse().statusCode() != 200)
17+
if (resp.httpResponse().statusCode() != 200)
1818
throw new RuntimeException("Request failed with status code " + resp.httpResponse().statusCode());
1919
return true;
2020
});
@@ -24,10 +24,10 @@ public ControlRequestAction(JavaShock instance) {
2424

2525
public RestAction<Boolean> addShockControl(Shocker shocker, ControlData data) {
2626
final int intensity = data.intensity();
27-
if(intensity < 0 || intensity > 100)
27+
if (intensity < 0 || intensity > 100)
2828
throw new IllegalArgumentException("Intensity must be between 0 and 100");
2929
final long duration = data.duration();
30-
if(duration < 0 || duration > JavaShock.MAX_SHOCK_DURATION)
30+
if (duration < 0 || duration > JavaShock.MAX_SHOCK_DURATION)
3131
throw new IllegalArgumentException("Duration must be between 0 and " + JavaShock.MAX_SHOCK_DURATION);
3232
this.data.put(shocker, data);
3333
return this;

0 commit comments

Comments
 (0)