Use switch expression where feasible

This commit is contained in:
Yanming Zhou
2023-11-01 10:49:43 +08:00
committed by Sam Brannen
parent 8ed04b5dd1
commit 490b5c77fc
28 changed files with 420 additions and 424 deletions

View File

@@ -251,27 +251,24 @@ public class SingleConnectionFactory extends DelegatingConnectionFactory
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
return switch (method.getName()) {
case "equals" -> proxy == args[0];
// Only consider equal when proxies are identical.
return proxy == args[0];
case "hashCode":
case "hashCode" -> System.identityHashCode(proxy);
// Use hashCode of PersistenceManager proxy.
return System.identityHashCode(proxy);
case "unwrap":
return this.target;
case "close":
case "unwrap" -> this.target;
case "close" -> Mono.empty();
// Handle close method: suppress, not valid.
return Mono.empty();
}
// Invoke method on target Connection.
try {
return method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
default -> {
// Invoke method on target Connection.
try {
yield method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
};
}
}

View File

@@ -143,28 +143,26 @@ public class TransactionAwareConnectionFactoryProxy extends DelegatingConnection
}
}
switch (method.getName()) {
case "unwrap":
return this.connection;
case "close":
// Handle close method: only close if not within a transaction.
return ConnectionFactoryUtils.doReleaseConnection(this.connection, this.targetConnectionFactory)
return switch (method.getName()) {
case "unwrap" -> this.connection;
case "close" -> ConnectionFactoryUtils.doReleaseConnection(this.connection, this.targetConnectionFactory)
.doOnSubscribe(n -> this.closed = true);
case "isClosed":
return this.closed;
}
// Handle close method: only close if not within a transaction.
case "isClosed" -> this.closed;
default -> {
if (this.closed) {
throw new IllegalStateException("Connection handle already closed");
}
if (this.closed) {
throw new IllegalStateException("Connection handle already closed");
}
// Invoke method on target Connection.
try {
return method.invoke(this.connection, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
// Invoke method on target Connection.
try {
yield method.invoke(this.connection, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
};
}
private String proxyToString(@Nullable Object proxy) {

View File

@@ -519,27 +519,24 @@ final class DefaultDatabaseClient implements DatabaseClient {
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
return switch (method.getName()) {
case "equals" -> proxy == args[0];
// Only consider equal when proxies are identical.
return proxy == args[0];
case "hashCode":
case "hashCode" -> System.identityHashCode(proxy);
// Use hashCode of PersistenceManager proxy.
return System.identityHashCode(proxy);
case "unwrap":
return this.target;
case "close":
case "unwrap" -> this.target;
case "close" -> Mono.error(new UnsupportedOperationException("Close is not supported!"));
// Handle close method: suppress, not valid.
return Mono.error(new UnsupportedOperationException("Close is not supported!"));
}
// Invoke method on target Connection.
try {
return method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
default -> {
// Invoke method on target Connection.
try {
yield method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
};
}
}