Skip to content

Change switch statements to switch expressions. #2706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -435,20 +435,11 @@ public void clusterSetSlot(RedisClusterNode node, int slot, AddSlots mode) {
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);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,12 @@ public static SortingParams toSortingParams(@Nullable SortParameters params) {
}

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;
};
}

/**
Expand Down Expand Up @@ -462,14 +456,11 @@ public static SetParams toSetCommandNxXxArgument(SetOption option, SetParams par

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,
Expand Down Expand Up @@ -663,24 +654,16 @@ public static GeoRadiusParam toGeoRadiusParam(GeoRadiusCommandArgs source) {
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();
}
}

Expand All @@ -702,12 +685,12 @@ public static GeoRadiusParam toGeoRadiusParam(GeoRadiusCommandArgs source) {
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);
}
}
}

Expand Down Expand Up @@ -753,14 +736,10 @@ static FlushMode toFlushMode(@Nullable RedisServerCommands.FlushOption option) {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,11 @@ public void clusterSetSlot(RedisClusterNode node, int slot, AddSlots mode) {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,15 @@ public static String toString(@Nullable byte[] source) {

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) {
Expand Down Expand Up @@ -514,31 +509,14 @@ private static Set<Flag> parseFlags(@Nullable Set<NodeFlag> source) {
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;
Expand All @@ -562,35 +540,29 @@ public static SetArgs toSetArgs(@Nullable Expiration expiration, @Nullable SetOp
} 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;
}
}
}
}

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;
Expand Down Expand Up @@ -686,12 +658,8 @@ public static GeoArgs toGeoArgs(GeoCommandArgs args) {

if (args.hasSortDirection()) {
switch (args.getSortDirection()) {
case ASC:
geoArgs.asc();
break;
case DESC:
geoArgs.desc();
break;
case ASC -> geoArgs.asc();
case DESC -> geoArgs.desc();
}
}

Expand Down Expand Up @@ -735,23 +703,12 @@ public static BitFieldArgs toBitFieldArgs(BitFieldSubCommands subCommands) {
BitFieldIncrBy.Overflow overflow = ((BitFieldIncrBy) subCommand).getOverflow();
if (overflow != null) {

BitFieldArgs.OverflowType type;

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));
}
BitFieldArgs.OverflowType type = switch (overflow) {
case SAT -> BitFieldArgs.OverflowType.SAT;
case FAIL -> BitFieldArgs.OverflowType.FAIL;
case WRAP -> BitFieldArgs.OverflowType.WRAP;
};

args = args.overflow(type);
}

Expand Down Expand Up @@ -937,14 +894,10 @@ static FlushMode toFlushMode(@Nullable RedisServerCommands.FlushOption option) {
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;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,12 @@ public Mono<Void> clusterSetSlot(RedisClusterNode node, int slot, AddSlots mode)
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();
}
Expand Down Expand Up @@ -359,12 +353,11 @@ protected Mono<RedisClusterReactiveCommands<ByteBuffer, ByteBuffer>> getCommands
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()))
Expand Down
Loading