mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-03 04:50:37 +00:00
Use pattern matching switch cases
This commit is contained in:
parent
80c1a6d2af
commit
8d55dfb66b
27 changed files with 395 additions and 380 deletions
|
@ -82,53 +82,55 @@ public class JsonRpcReader {
|
|||
final RequestHandler requestHandler,
|
||||
final Consumer<JsonRpcResponse> responseHandler
|
||||
) {
|
||||
if (message instanceof final JsonRpcRequest jsonRpcRequest) {
|
||||
logger.debug("Received json rpc request, method: " + jsonRpcRequest.getMethod());
|
||||
final var response = handleRequest(requestHandler, jsonRpcRequest);
|
||||
if (response != null) {
|
||||
jsonRpcSender.sendResponse(response);
|
||||
switch (message) {
|
||||
case JsonRpcRequest jsonRpcRequest -> {
|
||||
logger.debug("Received json rpc request, method: " + jsonRpcRequest.getMethod());
|
||||
final var response = handleRequest(requestHandler, jsonRpcRequest);
|
||||
if (response != null) {
|
||||
jsonRpcSender.sendResponse(response);
|
||||
}
|
||||
}
|
||||
} else if (message instanceof JsonRpcResponse jsonRpcResponse) {
|
||||
responseHandler.accept(jsonRpcResponse);
|
||||
} else {
|
||||
final var messages = ((JsonRpcBatchMessage) message).getMessages();
|
||||
final var responseList = new ArrayList<JsonRpcResponse>(messages.size());
|
||||
final var executor = Executors.newFixedThreadPool(10);
|
||||
try {
|
||||
final var lock = new ReentrantLock();
|
||||
messages.forEach(jsonNode -> {
|
||||
final JsonRpcRequest request;
|
||||
try {
|
||||
request = parseJsonRpcRequest(jsonNode);
|
||||
} catch (JsonRpcException e) {
|
||||
final var response = JsonRpcResponse.forError(e.getError(), getId(jsonNode));
|
||||
lock.lock();
|
||||
case JsonRpcResponse jsonRpcResponse -> responseHandler.accept(jsonRpcResponse);
|
||||
case JsonRpcBatchMessage jsonRpcBatchMessage -> {
|
||||
final var messages = jsonRpcBatchMessage.getMessages();
|
||||
final var responseList = new ArrayList<JsonRpcResponse>(messages.size());
|
||||
final var executor = Executors.newFixedThreadPool(10);
|
||||
try {
|
||||
final var lock = new ReentrantLock();
|
||||
messages.forEach(jsonNode -> {
|
||||
final JsonRpcRequest request;
|
||||
try {
|
||||
responseList.add(response);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
executor.submit(() -> {
|
||||
final var response = handleRequest(requestHandler, request);
|
||||
if (response != null) {
|
||||
request = parseJsonRpcRequest(jsonNode);
|
||||
} catch (JsonRpcException e) {
|
||||
final var response = JsonRpcResponse.forError(e.getError(), getId(jsonNode));
|
||||
lock.lock();
|
||||
try {
|
||||
responseList.add(response);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
Util.closeExecutorService(executor);
|
||||
}
|
||||
|
||||
if (!responseList.isEmpty()) {
|
||||
jsonRpcSender.sendBatchResponses(responseList);
|
||||
executor.submit(() -> {
|
||||
final var response = handleRequest(requestHandler, request);
|
||||
if (response != null) {
|
||||
lock.lock();
|
||||
try {
|
||||
responseList.add(response);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
Util.closeExecutorService(executor);
|
||||
}
|
||||
|
||||
if (!responseList.isEmpty()) {
|
||||
jsonRpcSender.sendBatchResponses(responseList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -221,7 +223,7 @@ public class JsonRpcReader {
|
|||
|
||||
private ValueNode getId(JsonNode jsonNode) {
|
||||
final var id = jsonNode.get("id");
|
||||
return id instanceof ValueNode ? (ValueNode) id : null;
|
||||
return id instanceof ValueNode value ? value : null;
|
||||
}
|
||||
|
||||
private JsonRpcRequest parseJsonRpcRequest(final JsonNode input) throws JsonRpcException {
|
||||
|
|
|
@ -223,13 +223,11 @@ public class SignalJsonRpcDispatcherHandler {
|
|||
}
|
||||
|
||||
private Integer getSubscriptionId(final JsonNode request) {
|
||||
if (request instanceof ArrayNode req) {
|
||||
return req.get(0).asInt();
|
||||
} else if (request instanceof ObjectNode req) {
|
||||
return req.get("subscription").asInt();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return switch (request) {
|
||||
case ArrayNode req -> req.get(0).asInt();
|
||||
case ObjectNode req -> req.get("subscription").asInt();
|
||||
case null, default -> null;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue