Skip to content

Commit 2eddc22

Browse files
committed
Use StreamName enum in (un)subscribe methods
1 parent cc12811 commit 2eddc22

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This library no longer depends directly on any SLF4J implementation. If you want
1313
from this library, you must include an SLF4J implementation in your project.
1414

1515
### Other
16+
- `Server#subscribe` and `Server#unsubscribe` now accept the `StreamName` enum instead of any string
1617
- Arrays have been Replaced by Collection's in almost all places
1718
- Config options now return a generic type instead of `Object`
1819
- Removed `ExarotonClient#getGson()` and `WebsocketClient#getGson()`

src/main/java/com/exaroton/api/server/Server.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.exaroton.api.ExarotonClient;
55
import com.exaroton.api.request.server.*;
66
import com.exaroton.api.ws.WebSocketManager;
7+
import com.exaroton.api.ws.stream.StreamName;
78
import com.exaroton.api.ws.subscriber.*;
89
import com.google.gson.Gson;
910
import org.jetbrains.annotations.ApiStatus;
@@ -451,8 +452,8 @@ public void subscribe() {
451452
*
452453
* @param streams stream names
453454
*/
454-
public void subscribe(String... streams) {
455-
for (String stream : streams) {
455+
public void subscribe(StreamName... streams) {
456+
for (StreamName stream : streams) {
456457
if (this.webSocket == null) {
457458
this.subscribe();
458459
}
@@ -475,9 +476,9 @@ public void unsubscribe() {
475476
*
476477
* @param streams stream names
477478
*/
478-
public void unsubscribe(String... streams) {
479+
public void unsubscribe(StreamName... streams) {
479480
if (this.webSocket == null) throw new RuntimeException("No websocket connection active.");
480-
for (String stream : streams) {
481+
for (StreamName stream : streams) {
481482
this.webSocket.unsubscribe(stream);
482483
}
483484
}

src/main/java/com/exaroton/api/ws/WebSocketManager.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import com.exaroton.api.ws.stream.*;
77
import com.exaroton.api.ws.subscriber.*;
88
import com.google.gson.Gson;
9+
import org.jetbrains.annotations.ApiStatus;
910
import org.jetbrains.annotations.NotNull;
1011

1112
import java.net.URI;
1213
import java.net.URISyntaxException;
1314
import java.util.*;
1415

16+
@ApiStatus.Internal
1517
public final class WebSocketManager {
1618
private final Gson gson;
1719

@@ -166,35 +168,37 @@ public void handleOpen() {
166168
/**
167169
* subscribe to a stream if it is not already active
168170
*
169-
* @param stream stream name
171+
* @param name stream name
170172
*/
171-
public void subscribe(String stream) {
172-
if (stream == null) throw new IllegalArgumentException("No stream specified");
173+
public void subscribe(@NotNull StreamName name) {
174+
Objects.requireNonNull(name);
173175

174-
Stream s;
175-
final StreamName name = StreamName.get(stream);
176+
if (streams.containsKey(name)) {
177+
return;
178+
}
179+
180+
Stream stream;
176181
if (name == StreamName.CONSOLE) {
177-
s = new ConsoleStream(this, this.gson);
182+
stream = new ConsoleStream(this, this.gson);
178183
} else {
179-
s = new Stream(this, this.gson, name);
184+
stream = new Stream(this, this.gson, name);
180185
}
181186

182-
this.streams.put(name, s);
183-
s.start();
187+
this.streams.put(name, stream);
188+
stream.start();
184189
}
185190

186191
/**
187192
* unsubscribe from a stream
188193
*
189-
* @param stream stream name
194+
* @param name stream name
190195
*/
191-
public void unsubscribe(String stream) {
192-
if (stream == null) throw new IllegalArgumentException("No stream specified");
196+
public void unsubscribe(@NotNull StreamName name) {
197+
Objects.requireNonNull(name);
193198

194-
final StreamName name = StreamName.get(stream);
195-
Stream s = this.streams.get(name);
196-
if (s != null) {
197-
s.stop();
199+
Stream stream = this.streams.get(name);
200+
if (stream != null) {
201+
stream.stop();
198202
this.streams.remove(name);
199203
}
200204
}

0 commit comments

Comments
 (0)