Skip to content

Commit

Permalink
Use modern switches
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 18, 2024
1 parent 338b1dc commit 70b6716
Showing 1 changed file with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ public SocketAddress read(JsonReader in) throws IOException {
private SocketAddressHelper() {}

public static String serialize(SocketAddress address) {
if (address instanceof InetSocketAddress inetSocketAddress) {
return "inet://%s:%d".formatted(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
} else if (address instanceof UnixDomainSocketAddress unixSocketAddress) {
return "unix://%s".formatted(unixSocketAddress.getPath());
} else {
throw new IllegalArgumentException("Unsupported address type: " + address.getClass().getName());
}
return switch (address) {
case InetSocketAddress inet -> "inet://%s:%d".formatted(inet.getHostString(), inet.getPort());
case UnixDomainSocketAddress unix -> "unix://%s".formatted(unix.getPath());
default -> throw new IllegalArgumentException("Unsupported address type: " + address.getClass().getName());
};
}

public static SocketAddress deserialize(String uriString) {
var uri = URI.create(uriString);
if ("inet".equals(uri.getScheme())) {
var host = uri.getHost();
var port = uri.getPort();
return new InetSocketAddress(host, port);
} else if ("unix".equals(uri.getScheme())) {
var path = uri.getPath();
return UnixDomainSocketAddress.of(path);
} else {
throw new IllegalArgumentException("Unsupported scheme: " + uri.getScheme());
}
return switch (uri.getScheme()) {
case "inet" -> {
var host = uri.getHost();
var port = uri.getPort();
yield new InetSocketAddress(host, port);
}
case "unix" -> {
var path = uri.getPath();
yield UnixDomainSocketAddress.of(path);
}
default -> throw new IllegalArgumentException("Unsupported scheme: " + uri.getScheme());
};
}
}

0 comments on commit 70b6716

Please sign in to comment.