Change switch statements to switch expressions.

Remove unused default branches.

Closes #2705
Original pull request: #2706
This commit is contained in:
Junghoon Ban
2023-09-11 13:48:32 +09:00
committed by John Blum
parent 68c77b9a69
commit 5e23156f78
14 changed files with 129 additions and 279 deletions

View File

@@ -435,20 +435,11 @@ public class JedisClusterConnection implements RedisClusterConnection {
RedisClusterNode nodeToUse = topologyProvider.getTopology().lookup(node);
String nodeId = nodeToUse.getId();
clusterCommandExecutor.executeCommandOnSingleNode((JedisClusterCommandCallback<String>) client -> {
switch (mode) {
case IMPORTING:
return client.clusterSetSlotImporting(slot, nodeId);
case MIGRATING:
return client.clusterSetSlotMigrating(slot, nodeId);
case STABLE:
return client.clusterSetSlotStable(slot);
case NODE:
return client.clusterSetSlotNode(slot, nodeId);
}
throw new IllegalArgumentException(String.format("Unknown AddSlots mode '%s'", mode));
clusterCommandExecutor.executeCommandOnSingleNode((JedisClusterCommandCallback<String>) client -> switch (mode) {
case IMPORTING -> client.clusterSetSlotImporting(slot, nodeId);
case MIGRATING -> client.clusterSetSlotMigrating(slot, nodeId);
case STABLE -> client.clusterSetSlotStable(slot);
case NODE -> client.clusterSetSlotNode(slot, nodeId);
}, node);
}

View File

@@ -288,18 +288,12 @@ abstract class JedisConverters extends Converters {
}
public static BitOP toBitOp(BitOperation bitOp) {
switch (bitOp) {
case AND:
return BitOP.AND;
case OR:
return BitOP.OR;
case NOT:
return BitOP.NOT;
case XOR:
return BitOP.XOR;
default:
throw new IllegalArgumentException();
}
return switch (bitOp) {
case AND -> BitOP.AND;
case OR -> BitOP.OR;
case NOT -> BitOP.NOT;
case XOR -> BitOP.XOR;
};
}
/**
@@ -462,14 +456,11 @@ abstract class JedisConverters extends Converters {
SetParams paramsToUse = params == null ? SetParams.setParams() : params;
switch (option) {
case SET_IF_PRESENT:
return paramsToUse.xx();
case SET_IF_ABSENT:
return paramsToUse.nx();
default:
return paramsToUse;
}
return switch (option) {
case SET_IF_PRESENT -> paramsToUse.xx();
case SET_IF_ABSENT -> paramsToUse.nx();
default -> paramsToUse;
};
}
private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Bound<?> boundary, byte[] inclPrefix,
@@ -663,24 +654,16 @@ abstract class JedisConverters extends Converters {
if (source.hasFlags()) {
for (Flag flag : source.getFlags()) {
switch (flag) {
case WITHCOORD:
param.withCoord();
break;
case WITHDIST:
param.withDist();
break;
case WITHCOORD -> param.withCoord();
case WITHDIST -> param.withDist();
}
}
}
if (source.hasSortDirection()) {
switch (source.getSortDirection()) {
case ASC:
param.sortAscending();
break;
case DESC:
param.sortDescending();
break;
case ASC -> param.sortAscending();
case DESC -> param.sortDescending();
}
}
@@ -702,12 +685,12 @@ abstract class JedisConverters extends Converters {
static double toSeconds(long timeout, TimeUnit unit) {
switch (unit) {
case MILLISECONDS:
case MICROSECONDS:
case NANOSECONDS:
case MILLISECONDS, MICROSECONDS, NANOSECONDS -> {
return unit.toMillis(timeout) / 1000d;
default:
}
default -> {
return unit.toSeconds(timeout);
}
}
}
@@ -753,14 +736,10 @@ abstract class JedisConverters extends Converters {
return FlushMode.SYNC;
}
switch (option) {
case ASYNC:
return FlushMode.ASYNC;
case SYNC:
return FlushMode.SYNC;
default:
throw new IllegalArgumentException("Flush option " + option + " is not supported");
}
return switch (option) {
case ASYNC -> FlushMode.ASYNC;
case SYNC -> FlushMode.SYNC;
};
}
static GeoSearchParam toGeoSearchParams(GeoReference<byte[]> reference, GeoShape predicate,

View File

@@ -419,19 +419,11 @@ public class LettuceClusterConnection extends LettuceConnection
RedisClusterNode nodeToUse = topologyProvider.getTopology().lookup(node);
String nodeId = nodeToUse.getId();
clusterCommandExecutor.executeCommandOnSingleNode((LettuceClusterCommandCallback<String>) client -> {
switch (mode) {
case MIGRATING:
return client.clusterSetSlotMigrating(slot, nodeId);
case IMPORTING:
return client.clusterSetSlotImporting(slot, nodeId);
case NODE:
return client.clusterSetSlotNode(slot, nodeId);
case STABLE:
return client.clusterSetSlotStable(slot);
default:
throw new InvalidDataAccessApiUsageException("Invalid import mode for cluster slot: " + slot);
}
clusterCommandExecutor.executeCommandOnSingleNode((LettuceClusterCommandCallback<String>) client -> switch (mode) {
case MIGRATING -> client.clusterSetSlotMigrating(slot, nodeId);
case IMPORTING -> client.clusterSetSlotImporting(slot, nodeId);
case NODE -> client.clusterSetSlotNode(slot, nodeId);
case STABLE -> client.clusterSetSlotStable(slot);
}, node);
}

View File

@@ -175,20 +175,15 @@ public abstract class LettuceConverters extends Converters {
public static ScriptOutputType toScriptOutputType(ReturnType returnType) {
switch (returnType) {
case BOOLEAN:
return ScriptOutputType.BOOLEAN;
case MULTI:
return ScriptOutputType.MULTI;
case VALUE:
return ScriptOutputType.VALUE;
case INTEGER:
return ScriptOutputType.INTEGER;
case STATUS:
return ScriptOutputType.STATUS;
default:
return switch (returnType) {
case BOOLEAN -> ScriptOutputType.BOOLEAN;
case MULTI -> ScriptOutputType.MULTI;
case VALUE -> ScriptOutputType.VALUE;
case INTEGER -> ScriptOutputType.INTEGER;
case STATUS -> ScriptOutputType.STATUS;
default ->
throw new IllegalArgumentException("Return type " + returnType + " is not a supported script output type");
}
};
}
public static boolean toBoolean(Position where) {
@@ -514,31 +509,14 @@ public abstract class LettuceConverters extends Converters {
Set<Flag> flags = new LinkedHashSet<>(source != null ? source.size() : 8, 1);
for (NodeFlag flag : source) {
switch (flag) {
case NOFLAGS:
flags.add(Flag.NOFLAGS);
break;
case EVENTUAL_FAIL:
flags.add(Flag.PFAIL);
break;
case FAIL:
flags.add(Flag.FAIL);
break;
case HANDSHAKE:
flags.add(Flag.HANDSHAKE);
break;
case MASTER:
flags.add(Flag.MASTER);
break;
case MYSELF:
flags.add(Flag.MYSELF);
break;
case NOADDR:
flags.add(Flag.NOADDR);
break;
case SLAVE:
case REPLICA:
flags.add(Flag.REPLICA);
break;
case NOFLAGS -> flags.add(Flag.NOFLAGS);
case EVENTUAL_FAIL -> flags.add(Flag.PFAIL);
case FAIL -> flags.add(Flag.FAIL);
case HANDSHAKE -> flags.add(Flag.HANDSHAKE);
case MASTER -> flags.add(Flag.MASTER);
case MYSELF -> flags.add(Flag.MYSELF);
case NOADDR -> flags.add(Flag.NOADDR);
case SLAVE, REPLICA -> flags.add(Flag.REPLICA);
}
}
return flags;
@@ -562,20 +540,20 @@ public abstract class LettuceConverters extends Converters {
} else if (!expiration.isPersistent()) {
switch (expiration.getTimeUnit()) {
case MILLISECONDS:
case MILLISECONDS -> {
if (expiration.isUnixTimestamp()) {
args.pxAt(expiration.getConverted(TimeUnit.MILLISECONDS));
} else {
args.px(expiration.getConverted(TimeUnit.MILLISECONDS));
}
break;
default:
}
default -> {
if (expiration.isUnixTimestamp()) {
args.exAt(expiration.getConverted(TimeUnit.SECONDS));
} else {
args.ex(expiration.getConverted(TimeUnit.SECONDS));
}
break;
}
}
}
}
@@ -583,14 +561,8 @@ public abstract class LettuceConverters extends Converters {
if (option != null) {
switch (option) {
case SET_IF_ABSENT:
args.nx();
break;
case SET_IF_PRESENT:
args.xx();
break;
default:
break;
case SET_IF_ABSENT -> args.nx();
case SET_IF_PRESENT -> args.xx();
}
}
return args;
@@ -686,12 +658,8 @@ public abstract class LettuceConverters extends Converters {
if (args.hasSortDirection()) {
switch (args.getSortDirection()) {
case ASC:
geoArgs.asc();
break;
case DESC:
geoArgs.desc();
break;
case ASC -> geoArgs.asc();
case DESC -> geoArgs.desc();
}
}
@@ -735,23 +703,12 @@ public abstract class LettuceConverters extends Converters {
BitFieldIncrBy.Overflow overflow = ((BitFieldIncrBy) subCommand).getOverflow();
if (overflow != null) {
BitFieldArgs.OverflowType type;
BitFieldArgs.OverflowType type = switch (overflow) {
case SAT -> BitFieldArgs.OverflowType.SAT;
case FAIL -> BitFieldArgs.OverflowType.FAIL;
case WRAP -> BitFieldArgs.OverflowType.WRAP;
};
switch (overflow) {
case SAT:
type = BitFieldArgs.OverflowType.SAT;
break;
case FAIL:
type = BitFieldArgs.OverflowType.FAIL;
break;
case WRAP:
type = BitFieldArgs.OverflowType.WRAP;
break;
default:
throw new IllegalArgumentException(
String.format("Invalid OVERFLOW; Expected one the following %s but got %s",
Arrays.toString(Overflow.values()), overflow));
}
args = args.overflow(type);
}
@@ -937,14 +894,10 @@ public abstract class LettuceConverters extends Converters {
return FlushMode.SYNC;
}
switch (option) {
case ASYNC:
return FlushMode.ASYNC;
case SYNC:
return FlushMode.SYNC;
default:
throw new IllegalArgumentException("Flush option " + option + " is not supported");
}
return switch (option) {
case ASYNC -> FlushMode.ASYNC;
case SYNC -> FlushMode.SYNC;
};
}
/**

View File

@@ -283,18 +283,12 @@ class LettuceReactiveRedisClusterConnection extends LettuceReactiveRedisConnecti
RedisClusterNode nodeToUse = lookup(node);
String nodeId = nodeToUse.getId();
switch (mode) {
case MIGRATING:
return cmd.clusterSetSlotMigrating(slot, nodeId);
case IMPORTING:
return cmd.clusterSetSlotImporting(slot, nodeId);
case NODE:
return cmd.clusterSetSlotNode(slot, nodeId);
case STABLE:
return cmd.clusterSetSlotStable(slot);
default:
throw new InvalidDataAccessApiUsageException("Invalid import mode for cluster slot: " + slot);
}
return switch (mode) {
case MIGRATING -> cmd.clusterSetSlotMigrating(slot, nodeId);
case IMPORTING -> cmd.clusterSetSlotImporting(slot, nodeId);
case NODE -> cmd.clusterSetSlotNode(slot, nodeId);
case STABLE -> cmd.clusterSetSlotStable(slot);
};
}).then();
}
@@ -359,12 +353,11 @@ class LettuceReactiveRedisClusterConnection extends LettuceReactiveRedisConnecti
protected Mono<RedisReactiveCommands<ByteBuffer, ByteBuffer>> getCommands(RedisNode node) {
if (StringUtils.hasText(node.getId())) {
return getConnection().cast(StatefulRedisClusterConnection.class)
.flatMap(it -> {
StatefulRedisClusterConnection<ByteBuffer, ByteBuffer> connection = it;
return Mono.fromCompletionStage(connection.getConnectionAsync(node.getId()))
.map(StatefulRedisConnection::reactive);
});
return getConnection().cast(StatefulRedisClusterConnection.class).flatMap(it -> {
StatefulRedisClusterConnection<ByteBuffer, ByteBuffer> connection = it;
return Mono.fromCompletionStage(connection.getConnectionAsync(node.getId()))
.map(StatefulRedisConnection::reactive);
});
}
return getConnection().flatMap(it -> Mono.fromCompletionStage(it.getConnectionAsync(node.getHost(), node.getPort()))

View File

@@ -329,25 +329,16 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands {
ByteBuffer destinationKey = command.getDestinationKey();
ByteBuffer[] sourceKeys = command.getKeys().toArray(new ByteBuffer[0]);
switch (command.getBitOp()) {
case AND:
result = cmd.bitopAnd(destinationKey, sourceKeys);
break;
case OR:
result = cmd.bitopOr(destinationKey, sourceKeys);
break;
case XOR:
result = cmd.bitopXor(destinationKey, sourceKeys);
break;
case NOT:
result = switch (command.getBitOp()) {
case AND -> cmd.bitopAnd(destinationKey, sourceKeys);
case OR -> cmd.bitopOr(destinationKey, sourceKeys);
case XOR -> cmd.bitopXor(destinationKey, sourceKeys);
case NOT -> {
Assert.isTrue(sourceKeys.length == 1, "BITOP NOT does not allow more than 1 source key.");
result = cmd.bitopNot(destinationKey, sourceKeys[0]);
break;
default:
throw new IllegalArgumentException(String.format("Unknown BITOP '%s'.", command.getBitOp()));
}
yield cmd.bitopNot(destinationKey, sourceKeys[0]);
}
default -> throw new IllegalArgumentException(String.format("Unknown BITOP '%s'.", command.getBitOp()));
};
return result.map(value -> new NumericResponse<>(command, value));
}));

View File

@@ -703,15 +703,9 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
ZStoreArgs args = new ZStoreArgs();
if (aggregate != null) {
switch (aggregate) {
case MIN:
args.min();
break;
case MAX:
args.max();
break;
default:
args.sum();
break;
case MIN -> args.min();
case MAX -> args.max();
default -> args.sum();
}
}

View File

@@ -281,21 +281,17 @@ class LettuceStringCommands implements RedisStringCommands {
return connection.invoke().just(it -> {
switch (op) {
case AND:
return it.bitopAnd(destination, keys);
case OR:
return it.bitopOr(destination, keys);
case XOR:
return it.bitopXor(destination, keys);
case NOT:
return switch (op) {
case AND -> it.bitopAnd(destination, keys);
case OR -> it.bitopOr(destination, keys);
case XOR -> it.bitopXor(destination, keys);
case NOT -> {
if (keys.length != 1) {
throw new IllegalArgumentException("Bitop NOT should only be performed against one key");
}
return it.bitopNot(destination, keys[0]);
default:
throw new UnsupportedOperationException("Bit operation " + op + " is not supported");
}
yield it.bitopNot(destination, keys[0]);
}
};
});
}

View File

@@ -703,15 +703,9 @@ class LettuceZSetCommands implements RedisZSetCommands {
if (aggregate != null) {
switch (aggregate) {
case MIN:
args.min();
break;
case MAX:
args.max();
break;
default:
args.sum();
break;
case MIN -> args.min();
case MAX -> args.max();
default -> args.sum();
}
}
@@ -726,15 +720,9 @@ class LettuceZSetCommands implements RedisZSetCommands {
if (aggregate != null) {
switch (aggregate) {
case MIN:
args.min();
break;
case MAX:
args.max();
break;
default:
args.sum();
break;
case MIN -> args.min();
case MAX -> args.max();
default -> args.sum();
}
}

View File

@@ -137,17 +137,14 @@ class BoundOperationsProxyFactory {
Method method = invocation.getMethod();
switch (method.getName()) {
case "getKey":
return delegate.getKey();
case "rename":
return switch (method.getName()) {
case "getKey" -> delegate.getKey();
case "rename" -> {
delegate.rename(invocation.getArguments()[0]);
return null;
case "getOperations":
return delegate.getOps();
}
yield null;
}
case "getOperations" -> delegate.getOps();
};
if (method.getDeclaringClass() == boundOperationsInterface) {
return doInvoke(invocation, method, operationsTarget, true);

View File

@@ -412,14 +412,8 @@ public abstract class RedisConnectionUtils {
try {
if (!readOnly) {
switch (status) {
case TransactionSynchronization.STATUS_COMMITTED:
connection.exec();
break;
case TransactionSynchronization.STATUS_ROLLED_BACK:
case TransactionSynchronization.STATUS_UNKNOWN:
default:
case TransactionSynchronization.STATUS_COMMITTED -> connection.exec();
case TransactionSynchronization.STATUS_ROLLED_BACK, TransactionSynchronization.STATUS_UNKNOWN ->
connection.discard();
}
}
@@ -445,8 +439,7 @@ public abstract class RedisConnectionUtils {
* @author Mark Paluch
* @since 1.3
*/
static class ConnectionSplittingInterceptor
implements MethodInterceptor {
static class ConnectionSplittingInterceptor implements MethodInterceptor {
private final RedisConnectionFactory factory;
@@ -502,7 +495,6 @@ public abstract class RedisConnectionUtils {
}
}
private boolean isPotentiallyThreadBoundCommand(RedisCommand command) {
return RedisCommand.UNKNOWN.equals(command) || !command.isReadonly();
}
@@ -607,8 +599,8 @@ public abstract class RedisConnectionUtils {
* Subinterface of {@link RedisConnection} to be implemented by {@link RedisConnection} proxies. Allows access to the
* underlying target {@link RedisConnection}.
*
* @since 2.4.2
* @see RedisConnectionUtils#getTargetConnection(RedisConnection)
* @since 2.4.2
*/
public interface RedisConnectionProxy extends RedisConnection, RawTargetAccess {

View File

@@ -826,14 +826,11 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
private boolean keepShadowCopy() {
switch (shadowCopy) {
case OFF:
return false;
case ON:
return true;
default:
return this.expirationListener.get() != null;
}
return switch (shadowCopy) {
case OFF -> false;
case ON -> true;
default -> this.expirationListener.get() != null;
};
}
/**

View File

@@ -76,14 +76,10 @@ public abstract class TimeoutUtils {
*/
public static double toDoubleSeconds(long timeout, TimeUnit unit) {
switch (unit) {
case MILLISECONDS:
case MICROSECONDS:
case NANOSECONDS:
return unit.toMillis(timeout) / 1000d;
default:
return unit.toSeconds(timeout);
}
return switch (unit) {
case MILLISECONDS, MICROSECONDS, NANOSECONDS -> unit.toMillis(timeout) / 1000d;
default -> unit.toSeconds(timeout);
};
}
/**

View File

@@ -52,21 +52,12 @@ public class RedisQueryCreator extends AbstractQueryCreator<KeyValueQuery<RedisO
private RedisOperationChain from(Part part, Iterator<Object> iterator, RedisOperationChain sink) {
switch (part.getType()) {
case SIMPLE_PROPERTY:
sink.sismember(part.getProperty().toDotPath(), iterator.next());
break;
case TRUE:
sink.sismember(part.getProperty().toDotPath(), true);
break;
case FALSE:
sink.sismember(part.getProperty().toDotPath(), false);
break;
case WITHIN:
case NEAR:
sink.near(getNearPath(part, iterator));
break;
default:
throw new IllegalArgumentException(String.format("%s is not supported for Redis query derivation", part.getType()));
case SIMPLE_PROPERTY -> sink.sismember(part.getProperty().toDotPath(), iterator.next());
case TRUE -> sink.sismember(part.getProperty().toDotPath(), true);
case FALSE -> sink.sismember(part.getProperty().toDotPath(), false);
case WITHIN, NEAR -> sink.near(getNearPath(part, iterator));
default -> throw new IllegalArgumentException(
String.format("%s is not supported for Redis query derivation", part.getType()));
}
return sink;