Skip to content

Commit e34872d

Browse files
junghoon-vansjxblum
authored andcommitted
Change switch statements to switch expressions.
Remove unused default branches. Closes #2705 Original pull request: #2706
1 parent c64b22b commit e34872d

14 files changed

+130
-280
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -435,20 +435,11 @@ public void clusterSetSlot(RedisClusterNode node, int slot, AddSlots mode) {
435435
RedisClusterNode nodeToUse = topologyProvider.getTopology().lookup(node);
436436
String nodeId = nodeToUse.getId();
437437

438-
clusterCommandExecutor.executeCommandOnSingleNode((JedisClusterCommandCallback<String>) client -> {
439-
440-
switch (mode) {
441-
case IMPORTING:
442-
return client.clusterSetSlotImporting(slot, nodeId);
443-
case MIGRATING:
444-
return client.clusterSetSlotMigrating(slot, nodeId);
445-
case STABLE:
446-
return client.clusterSetSlotStable(slot);
447-
case NODE:
448-
return client.clusterSetSlotNode(slot, nodeId);
449-
}
450-
451-
throw new IllegalArgumentException(String.format("Unknown AddSlots mode '%s'", mode));
438+
clusterCommandExecutor.executeCommandOnSingleNode((JedisClusterCommandCallback<String>) client -> switch (mode) {
439+
case IMPORTING -> client.clusterSetSlotImporting(slot, nodeId);
440+
case MIGRATING -> client.clusterSetSlotMigrating(slot, nodeId);
441+
case STABLE -> client.clusterSetSlotStable(slot);
442+
case NODE -> client.clusterSetSlotNode(slot, nodeId);
452443
}, node);
453444

454445
}

src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java

+23-44
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,12 @@ public static SortingParams toSortingParams(@Nullable SortParameters params) {
288288
}
289289

290290
public static BitOP toBitOp(BitOperation bitOp) {
291-
switch (bitOp) {
292-
case AND:
293-
return BitOP.AND;
294-
case OR:
295-
return BitOP.OR;
296-
case NOT:
297-
return BitOP.NOT;
298-
case XOR:
299-
return BitOP.XOR;
300-
default:
301-
throw new IllegalArgumentException();
302-
}
291+
return switch (bitOp) {
292+
case AND -> BitOP.AND;
293+
case OR -> BitOP.OR;
294+
case NOT -> BitOP.NOT;
295+
case XOR -> BitOP.XOR;
296+
};
303297
}
304298

305299
/**
@@ -462,14 +456,11 @@ public static SetParams toSetCommandNxXxArgument(SetOption option, SetParams par
462456

463457
SetParams paramsToUse = params == null ? SetParams.setParams() : params;
464458

465-
switch (option) {
466-
case SET_IF_PRESENT:
467-
return paramsToUse.xx();
468-
case SET_IF_ABSENT:
469-
return paramsToUse.nx();
470-
default:
471-
return paramsToUse;
472-
}
459+
return switch (option) {
460+
case SET_IF_PRESENT -> paramsToUse.xx();
461+
case SET_IF_ABSENT -> paramsToUse.nx();
462+
default -> paramsToUse;
463+
};
473464
}
474465

475466
private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Bound<?> boundary, byte[] inclPrefix,
@@ -663,24 +654,16 @@ public static GeoRadiusParam toGeoRadiusParam(GeoRadiusCommandArgs source) {
663654
if (source.hasFlags()) {
664655
for (Flag flag : source.getFlags()) {
665656
switch (flag) {
666-
case WITHCOORD:
667-
param.withCoord();
668-
break;
669-
case WITHDIST:
670-
param.withDist();
671-
break;
657+
case WITHCOORD -> param.withCoord();
658+
case WITHDIST -> param.withDist();
672659
}
673660
}
674661
}
675662

676663
if (source.hasSortDirection()) {
677664
switch (source.getSortDirection()) {
678-
case ASC:
679-
param.sortAscending();
680-
break;
681-
case DESC:
682-
param.sortDescending();
683-
break;
665+
case ASC -> param.sortAscending();
666+
case DESC -> param.sortDescending();
684667
}
685668
}
686669

@@ -702,12 +685,12 @@ public static GeoRadiusParam toGeoRadiusParam(GeoRadiusCommandArgs source) {
702685
static double toSeconds(long timeout, TimeUnit unit) {
703686

704687
switch (unit) {
705-
case MILLISECONDS:
706-
case MICROSECONDS:
707-
case NANOSECONDS:
688+
case MILLISECONDS, MICROSECONDS, NANOSECONDS -> {
708689
return unit.toMillis(timeout) / 1000d;
709-
default:
690+
}
691+
default -> {
710692
return unit.toSeconds(timeout);
693+
}
711694
}
712695
}
713696

@@ -753,14 +736,10 @@ static FlushMode toFlushMode(@Nullable RedisServerCommands.FlushOption option) {
753736
return FlushMode.SYNC;
754737
}
755738

756-
switch (option) {
757-
case ASYNC:
758-
return FlushMode.ASYNC;
759-
case SYNC:
760-
return FlushMode.SYNC;
761-
default:
762-
throw new IllegalArgumentException("Flush option " + option + " is not supported");
763-
}
739+
return switch (option) {
740+
case ASYNC -> FlushMode.ASYNC;
741+
case SYNC -> FlushMode.SYNC;
742+
};
764743
}
765744

766745
static GeoSearchParam toGeoSearchParams(GeoReference<byte[]> reference, GeoShape predicate,

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,11 @@ public void clusterSetSlot(RedisClusterNode node, int slot, AddSlots mode) {
419419
RedisClusterNode nodeToUse = topologyProvider.getTopology().lookup(node);
420420
String nodeId = nodeToUse.getId();
421421

422-
clusterCommandExecutor.executeCommandOnSingleNode((LettuceClusterCommandCallback<String>) client -> {
423-
switch (mode) {
424-
case MIGRATING:
425-
return client.clusterSetSlotMigrating(slot, nodeId);
426-
case IMPORTING:
427-
return client.clusterSetSlotImporting(slot, nodeId);
428-
case NODE:
429-
return client.clusterSetSlotNode(slot, nodeId);
430-
case STABLE:
431-
return client.clusterSetSlotStable(slot);
432-
default:
433-
throw new InvalidDataAccessApiUsageException("Invalid import mode for cluster slot: " + slot);
434-
}
422+
clusterCommandExecutor.executeCommandOnSingleNode((LettuceClusterCommandCallback<String>) client -> switch (mode) {
423+
case MIGRATING -> client.clusterSetSlotMigrating(slot, nodeId);
424+
case IMPORTING -> client.clusterSetSlotImporting(slot, nodeId);
425+
case NODE -> client.clusterSetSlotNode(slot, nodeId);
426+
case STABLE -> client.clusterSetSlotStable(slot);
435427
}, node);
436428
}
437429

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

+34-81
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,15 @@ public static String toString(@Nullable byte[] source) {
175175

176176
public static ScriptOutputType toScriptOutputType(ReturnType returnType) {
177177

178-
switch (returnType) {
179-
case BOOLEAN:
180-
return ScriptOutputType.BOOLEAN;
181-
case MULTI:
182-
return ScriptOutputType.MULTI;
183-
case VALUE:
184-
return ScriptOutputType.VALUE;
185-
case INTEGER:
186-
return ScriptOutputType.INTEGER;
187-
case STATUS:
188-
return ScriptOutputType.STATUS;
189-
default:
178+
return switch (returnType) {
179+
case BOOLEAN -> ScriptOutputType.BOOLEAN;
180+
case MULTI -> ScriptOutputType.MULTI;
181+
case VALUE -> ScriptOutputType.VALUE;
182+
case INTEGER -> ScriptOutputType.INTEGER;
183+
case STATUS -> ScriptOutputType.STATUS;
184+
default ->
190185
throw new IllegalArgumentException("Return type " + returnType + " is not a supported script output type");
191-
}
186+
};
192187
}
193188

194189
public static boolean toBoolean(Position where) {
@@ -514,31 +509,14 @@ private static Set<Flag> parseFlags(@Nullable Set<NodeFlag> source) {
514509
Set<Flag> flags = new LinkedHashSet<>(source != null ? source.size() : 8, 1);
515510
for (NodeFlag flag : source) {
516511
switch (flag) {
517-
case NOFLAGS:
518-
flags.add(Flag.NOFLAGS);
519-
break;
520-
case EVENTUAL_FAIL:
521-
flags.add(Flag.PFAIL);
522-
break;
523-
case FAIL:
524-
flags.add(Flag.FAIL);
525-
break;
526-
case HANDSHAKE:
527-
flags.add(Flag.HANDSHAKE);
528-
break;
529-
case MASTER:
530-
flags.add(Flag.MASTER);
531-
break;
532-
case MYSELF:
533-
flags.add(Flag.MYSELF);
534-
break;
535-
case NOADDR:
536-
flags.add(Flag.NOADDR);
537-
break;
538-
case SLAVE:
539-
case REPLICA:
540-
flags.add(Flag.REPLICA);
541-
break;
512+
case NOFLAGS -> flags.add(Flag.NOFLAGS);
513+
case EVENTUAL_FAIL -> flags.add(Flag.PFAIL);
514+
case FAIL -> flags.add(Flag.FAIL);
515+
case HANDSHAKE -> flags.add(Flag.HANDSHAKE);
516+
case MASTER -> flags.add(Flag.MASTER);
517+
case MYSELF -> flags.add(Flag.MYSELF);
518+
case NOADDR -> flags.add(Flag.NOADDR);
519+
case SLAVE, REPLICA -> flags.add(Flag.REPLICA);
542520
}
543521
}
544522
return flags;
@@ -562,35 +540,29 @@ public static SetArgs toSetArgs(@Nullable Expiration expiration, @Nullable SetOp
562540
} else if (!expiration.isPersistent()) {
563541

564542
switch (expiration.getTimeUnit()) {
565-
case MILLISECONDS:
543+
case MILLISECONDS -> {
566544
if (expiration.isUnixTimestamp()) {
567545
args.pxAt(expiration.getConverted(TimeUnit.MILLISECONDS));
568546
} else {
569547
args.px(expiration.getConverted(TimeUnit.MILLISECONDS));
570548
}
571-
break;
572-
default:
549+
}
550+
default -> {
573551
if (expiration.isUnixTimestamp()) {
574552
args.exAt(expiration.getConverted(TimeUnit.SECONDS));
575553
} else {
576554
args.ex(expiration.getConverted(TimeUnit.SECONDS));
577555
}
578-
break;
556+
}
579557
}
580558
}
581559
}
582560

583561
if (option != null) {
584562

585563
switch (option) {
586-
case SET_IF_ABSENT:
587-
args.nx();
588-
break;
589-
case SET_IF_PRESENT:
590-
args.xx();
591-
break;
592-
default:
593-
break;
564+
case SET_IF_ABSENT -> args.nx();
565+
case SET_IF_PRESENT -> args.xx();
594566
}
595567
}
596568
return args;
@@ -686,12 +658,8 @@ public static GeoArgs toGeoArgs(GeoCommandArgs args) {
686658

687659
if (args.hasSortDirection()) {
688660
switch (args.getSortDirection()) {
689-
case ASC:
690-
geoArgs.asc();
691-
break;
692-
case DESC:
693-
geoArgs.desc();
694-
break;
661+
case ASC -> geoArgs.asc();
662+
case DESC -> geoArgs.desc();
695663
}
696664
}
697665

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

738-
BitFieldArgs.OverflowType type;
739-
740-
switch (overflow) {
741-
case SAT:
742-
type = BitFieldArgs.OverflowType.SAT;
743-
break;
744-
case FAIL:
745-
type = BitFieldArgs.OverflowType.FAIL;
746-
break;
747-
case WRAP:
748-
type = BitFieldArgs.OverflowType.WRAP;
749-
break;
750-
default:
751-
throw new IllegalArgumentException(
752-
String.format("Invalid OVERFLOW; Expected one the following %s but got %s",
753-
Arrays.toString(Overflow.values()), overflow));
754-
}
706+
BitFieldArgs.OverflowType type = switch (overflow) {
707+
case SAT -> BitFieldArgs.OverflowType.SAT;
708+
case FAIL -> BitFieldArgs.OverflowType.FAIL;
709+
case WRAP -> BitFieldArgs.OverflowType.WRAP;
710+
};
711+
755712
args = args.overflow(type);
756713
}
757714

@@ -937,14 +894,10 @@ static FlushMode toFlushMode(@Nullable RedisServerCommands.FlushOption option) {
937894
return FlushMode.SYNC;
938895
}
939896

940-
switch (option) {
941-
case ASYNC:
942-
return FlushMode.ASYNC;
943-
case SYNC:
944-
return FlushMode.SYNC;
945-
default:
946-
throw new IllegalArgumentException("Flush option " + option + " is not supported");
947-
}
897+
return switch (option) {
898+
case ASYNC -> FlushMode.ASYNC;
899+
case SYNC -> FlushMode.SYNC;
900+
};
948901
}
949902

950903
/**

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisClusterConnection.java

+11-18
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,12 @@ public Mono<Void> clusterSetSlot(RedisClusterNode node, int slot, AddSlots mode)
283283
RedisClusterNode nodeToUse = lookup(node);
284284
String nodeId = nodeToUse.getId();
285285

286-
switch (mode) {
287-
case MIGRATING:
288-
return cmd.clusterSetSlotMigrating(slot, nodeId);
289-
case IMPORTING:
290-
return cmd.clusterSetSlotImporting(slot, nodeId);
291-
case NODE:
292-
return cmd.clusterSetSlotNode(slot, nodeId);
293-
case STABLE:
294-
return cmd.clusterSetSlotStable(slot);
295-
default:
296-
throw new InvalidDataAccessApiUsageException("Invalid import mode for cluster slot: " + slot);
297-
}
286+
return switch (mode) {
287+
case MIGRATING -> cmd.clusterSetSlotMigrating(slot, nodeId);
288+
case IMPORTING -> cmd.clusterSetSlotImporting(slot, nodeId);
289+
case NODE -> cmd.clusterSetSlotNode(slot, nodeId);
290+
case STABLE -> cmd.clusterSetSlotStable(slot);
291+
};
298292

299293
}).then();
300294
}
@@ -359,12 +353,11 @@ protected Mono<RedisClusterReactiveCommands<ByteBuffer, ByteBuffer>> getCommands
359353
protected Mono<RedisReactiveCommands<ByteBuffer, ByteBuffer>> getCommands(RedisNode node) {
360354

361355
if (StringUtils.hasText(node.getId())) {
362-
return getConnection().cast(StatefulRedisClusterConnection.class)
363-
.flatMap(it -> {
364-
StatefulRedisClusterConnection<ByteBuffer, ByteBuffer> connection = it;
365-
return Mono.fromCompletionStage(connection.getConnectionAsync(node.getId()))
366-
.map(StatefulRedisConnection::reactive);
367-
});
356+
return getConnection().cast(StatefulRedisClusterConnection.class).flatMap(it -> {
357+
StatefulRedisClusterConnection<ByteBuffer, ByteBuffer> connection = it;
358+
return Mono.fromCompletionStage(connection.getConnectionAsync(node.getId()))
359+
.map(StatefulRedisConnection::reactive);
360+
});
368361
}
369362

370363
return getConnection().flatMap(it -> Mono.fromCompletionStage(it.getConnectionAsync(node.getHost(), node.getPort()))

0 commit comments

Comments
 (0)