Skip to content

Commit af1fae4

Browse files
committed
Enhance switch expressions.
1 parent e423851 commit af1fae4

14 files changed

+140
-280
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -435,20 +435,12 @@ 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);
443+
default -> throw new IllegalArgumentException(String.format("Unknown AddSlots mode '%s'", mode));
452444
}, node);
453445

454446
}

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

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,13 @@ 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+
default -> throw new IllegalArgumentException();
297+
};
303298
}
304299

305300
/**
@@ -462,14 +457,11 @@ public static SetParams toSetCommandNxXxArgument(SetOption option, SetParams par
462457

463458
SetParams paramsToUse = params == null ? SetParams.setParams() : params;
464459

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-
}
460+
return switch (option) {
461+
case SET_IF_PRESENT -> paramsToUse.xx();
462+
case SET_IF_ABSENT -> paramsToUse.nx();
463+
default -> paramsToUse;
464+
};
473465
}
474466

475467
private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Bound<?> boundary, byte[] inclPrefix,
@@ -663,24 +655,16 @@ public static GeoRadiusParam toGeoRadiusParam(GeoRadiusCommandArgs source) {
663655
if (source.hasFlags()) {
664656
for (Flag flag : source.getFlags()) {
665657
switch (flag) {
666-
case WITHCOORD:
667-
param.withCoord();
668-
break;
669-
case WITHDIST:
670-
param.withDist();
671-
break;
658+
case WITHCOORD -> param.withCoord();
659+
case WITHDIST -> param.withDist();
672660
}
673661
}
674662
}
675663

676664
if (source.hasSortDirection()) {
677665
switch (source.getSortDirection()) {
678-
case ASC:
679-
param.sortAscending();
680-
break;
681-
case DESC:
682-
param.sortDescending();
683-
break;
666+
case ASC -> param.sortAscending();
667+
case DESC -> param.sortDescending();
684668
}
685669
}
686670

@@ -702,12 +686,12 @@ public static GeoRadiusParam toGeoRadiusParam(GeoRadiusCommandArgs source) {
702686
static double toSeconds(long timeout, TimeUnit unit) {
703687

704688
switch (unit) {
705-
case MILLISECONDS:
706-
case MICROSECONDS:
707-
case NANOSECONDS:
689+
case MILLISECONDS, MICROSECONDS, NANOSECONDS -> {
708690
return unit.toMillis(timeout) / 1000d;
709-
default:
691+
}
692+
default -> {
710693
return unit.toSeconds(timeout);
694+
}
711695
}
712696
}
713697

@@ -753,14 +737,11 @@ static FlushMode toFlushMode(@Nullable RedisServerCommands.FlushOption option) {
753737
return FlushMode.SYNC;
754738
}
755739

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-
}
740+
return switch (option) {
741+
case ASYNC -> FlushMode.ASYNC;
742+
case SYNC -> FlushMode.SYNC;
743+
default -> throw new IllegalArgumentException("Flush option " + option + " is not supported");
744+
};
764745
}
765746

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

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,12 @@ 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);
427+
default -> throw new InvalidDataAccessApiUsageException("Invalid import mode for cluster slot: " + slot);
435428
}, node);
436429
}
437430

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

Lines changed: 38 additions & 81 deletions
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,15 @@ 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+
default -> throw new IllegalArgumentException(
711+
String.format("Invalid OVERFLOW; Expected one the following %s but got %s",
712+
Arrays.toString(Overflow.values()), overflow));
713+
};
714+
755715
args = args.overflow(type);
756716
}
757717

@@ -937,14 +897,11 @@ static FlushMode toFlushMode(@Nullable RedisServerCommands.FlushOption option) {
937897
return FlushMode.SYNC;
938898
}
939899

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-
}
900+
return switch (option) {
901+
case ASYNC -> FlushMode.ASYNC;
902+
case SYNC -> FlushMode.SYNC;
903+
default -> throw new IllegalArgumentException("Flush option " + option + " is not supported");
904+
};
948905
}
949906

950907
/**

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,13 @@ 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+
default -> throw new InvalidDataAccessApiUsageException("Invalid import mode for cluster slot: " + slot);
292+
};
298293

299294
}).then();
300295
}
@@ -359,12 +354,11 @@ protected Mono<RedisClusterReactiveCommands<ByteBuffer, ByteBuffer>> getCommands
359354
protected Mono<RedisReactiveCommands<ByteBuffer, ByteBuffer>> getCommands(RedisNode node) {
360355

361356
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-
});
357+
return getConnection().cast(StatefulRedisClusterConnection.class).flatMap(it -> {
358+
StatefulRedisClusterConnection<ByteBuffer, ByteBuffer> connection = it;
359+
return Mono.fromCompletionStage(connection.getConnectionAsync(node.getId()))
360+
.map(StatefulRedisConnection::reactive);
361+
});
368362
}
369363

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

0 commit comments

Comments
 (0)