Skip to content

Commit c7ab023

Browse files
jxblummp911de
authored andcommitted
Replace use of String.format(…) with formatted Strings.
Original pull request: #2752 Closes #2751
1 parent fcbe0fd commit c7ab023

File tree

82 files changed

+285
-328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+285
-328
lines changed

src/main/java/org/springframework/data/redis/ClusterRedirectException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class ClusterRedirectException extends DataRetrievalFailureException {
4646
*/
4747
public ClusterRedirectException(int slot, String targetHost, int targetPort, Throwable e) {
4848

49-
super(String.format("Redirect: slot %s to %s:%s.", slot, targetHost, targetPort), e);
49+
super("Redirect: slot %s to %s:%s.".formatted(slot, targetHost, targetPort), e);
5050

5151
this.slot = slot;
5252
this.host = targetHost;

src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,7 @@ private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection c
391391
// Re-interrupt current Thread to allow other participants to react.
392392
Thread.currentThread().interrupt();
393393

394-
String message = String.format("Interrupted while waiting to unlock cache %s", name);
395-
396-
throw new PessimisticLockingFailureException(message, ex);
394+
throw new PessimisticLockingFailureException("Interrupted while waiting to unlock cache %s".formatted(name), ex);
397395
} finally {
398396
this.statistics.incLockTime(name, System.nanoTime() - lockWaitTimeNs);
399397
}

src/main/java/org/springframework/data/redis/cache/RedisCache.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,9 @@ private Object processAndCheckValue(@Nullable Object value) {
318318
Object cacheValue = preProcessCacheValue(value);
319319

320320
if (nullCacheValueIsNotAllowed(cacheValue)) {
321-
322-
String message = String.format("Cache '%s' does not allow 'null' values; Avoid storing null"
321+
throw new IllegalArgumentException(("Cache '%s' does not allow 'null' values; Avoid storing null"
323322
+ " via '@Cacheable(unless=\"#result == null\")' or configure RedisCache to allow 'null'"
324-
+ " via RedisCacheConfiguration", getName());
325-
326-
throw new IllegalArgumentException(message);
323+
+ " via RedisCacheConfiguration").formatted(getName()));
327324
}
328325

329326
return cacheValue;
@@ -434,12 +431,9 @@ protected String convertKey(Object key) {
434431
return key.toString();
435432
}
436433

437-
String message = String.format(
438-
"Cannot convert cache key %s to String; Please register a suitable Converter"
439-
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'",
440-
source, key.getClass().getName());
441-
442-
throw new IllegalStateException(message);
434+
throw new IllegalStateException(("Cannot convert cache key %s to String; Please register a suitable Converter"
435+
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'")
436+
.formatted(source, key.getClass().getName()));
443437
}
444438

445439
private CompletableFuture<ValueWrapper> retrieveValue(Object key) {
@@ -499,7 +493,7 @@ private String convertCollectionLikeOrMapKey(Object key, TypeDescriptor source)
499493
return "[" + stringJoiner + "]";
500494
}
501495

502-
throw new IllegalArgumentException(String.format("Cannot convert cache key [%s] to String", key));
496+
throw new IllegalArgumentException("Cannot convert cache key [%s] to String".formatted(key));
503497
}
504498

505499
private byte[] createAndConvertCacheKey(Object key) {

src/main/java/org/springframework/data/redis/cache/RedisCacheConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,9 @@ public void addCacheKeyConverter(Converter<?, String> cacheKeyConverter) {
424424
public void configureKeyConverters(Consumer<ConverterRegistry> registryConsumer) {
425425

426426
if (!(getConversionService() instanceof ConverterRegistry)) {
427-
428-
String message = "'%s' returned by getConversionService() does not allow Converter registration;"
429-
+ " Please make sure to provide a ConversionService that implements ConverterRegistry";
430-
431-
throw new IllegalStateException(String.format(message, getConversionService().getClass().getName()));
427+
throw new IllegalStateException(("'%s' returned by getConversionService() does not allow Converter registration;"
428+
+ " Please make sure to provide a ConversionService that implements ConverterRegistry")
429+
.formatted(getConversionService().getClass().getName()));
432430
}
433431

434432
registryConsumer.accept((ConverterRegistry) getConversionService());

src/main/java/org/springframework/data/redis/connection/ClusterCommandExecutor.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,9 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
130130
Assert.notNull(node, "RedisClusterNode must not be null");
131131

132132
if (redirectCount > this.maxRedirects) {
133-
134-
String message = String.format("Cannot follow Cluster Redirects over more than %s legs; "
135-
+ "Consider increasing the number of redirects to follow; Current value is: %s.",
136-
redirectCount, this.maxRedirects);
137-
138-
throw new TooManyClusterRedirectionsException(message);
133+
throw new TooManyClusterRedirectionsException(("Cannot follow Cluster Redirects over more than %s legs;"
134+
+ " Consider increasing the number of redirects to follow; Current value is: %s")
135+
.formatted(redirectCount, this.maxRedirects));
139136
}
140137

141138
RedisClusterNode nodeToUse = lookupNode(node);
@@ -178,7 +175,7 @@ private RedisClusterNode lookupNode(RedisClusterNode node) {
178175
try {
179176
return topologyProvider.getTopology().lookup(node);
180177
} catch (ClusterStateFailureException ex) {
181-
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), ex);
178+
throw new IllegalArgumentException("Node %s is unknown to cluster".formatted(node), ex);
182179
}
183180
}
184181

@@ -215,7 +212,7 @@ public <S, T> MultiNodeResult<T> executeCommandAsyncOnNodes(ClusterCommandCallba
215212
try {
216213
resolvedRedisClusterNodes.add(topology.lookup(node));
217214
} catch (ClusterStateFailureException ex) {
218-
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), ex);
215+
throw new IllegalArgumentException("Node %s is unknown to cluster".formatted(node), ex);
219216
}
220217
}
221218

src/main/java/org/springframework/data/redis/connection/ClusterTopology.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public RedisClusterNode getKeyServingMasterNode(byte[] key) {
141141
}
142142

143143
throw new ClusterStateFailureException(
144-
String.format("Could not find master node serving slot %s for key '%s',", slot, Arrays.toString(key)));
144+
"Could not find master node serving slot %s for key '%s',".formatted(slot, Arrays.toString(key)));
145145
}
146146

147147
/**
@@ -161,7 +161,7 @@ public RedisClusterNode lookup(String host, int port) {
161161
}
162162

163163
throw new ClusterStateFailureException(
164-
String.format("Could not find node at %s:%s; Is your cluster info up to date", host, port));
164+
"Could not find node at %s:%d; Is your cluster info up to date".formatted(host, port));
165165
}
166166

167167
/**
@@ -182,7 +182,7 @@ public RedisClusterNode lookup(String nodeId) {
182182
}
183183

184184
throw new ClusterStateFailureException(
185-
String.format("Could not find node at %s; Is your cluster info up to date", nodeId));
185+
"Could not find node at %s; Is your cluster info up to date".formatted(nodeId));
186186
}
187187

188188
/**
@@ -210,7 +210,7 @@ public RedisClusterNode lookup(RedisClusterNode node) {
210210
}
211211

212212
throw new ClusterStateFailureException(
213-
String.format("Could not find node at %s; Have you provided either host and port or the nodeId", node));
213+
("Could not find node at %s;" + " Have you provided either host and port or the nodeId").formatted(node));
214214
}
215215

216216
/**

src/main/java/org/springframework/data/redis/connection/RedisNode.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public static RedisNode fromString(String hostPortString) {
102102
try {
103103
port = Integer.parseInt(portString);
104104
} catch (RuntimeException ignore) {
105-
throw new IllegalArgumentException(String.format("Unparseable port number: %s", hostPortString));
105+
throw new IllegalArgumentException("Unparseable port number: %s".formatted(hostPortString));
106106
}
107107

108108
if (!isValidPort(port)) {
109-
throw new IllegalArgumentException(String.format("Port number out of range: %s", hostPortString));
109+
throw new IllegalArgumentException("Port number out of range: %s".formatted(hostPortString));
110110
}
111111

112112
return new RedisNode(host, port);
@@ -123,27 +123,26 @@ private static String[] getHostAndPortFromBracketedHost(String hostPortString) {
123123

124124
if (hostPortString.charAt(0) != '[') {
125125
throw new IllegalArgumentException(
126-
String.format("Bracketed host-port string must start with a bracket: %s", hostPortString));
126+
"Bracketed host-port string must start with a bracket: %s".formatted(hostPortString));
127127
}
128128

129129
int colonIndex = hostPortString.indexOf(':');
130130
int closeBracketIndex = hostPortString.lastIndexOf(']');
131131

132132
if (!(colonIndex > -1 && closeBracketIndex > colonIndex)) {
133-
throw new IllegalArgumentException(String.format("Invalid bracketed host/port: %s", hostPortString));
133+
throw new IllegalArgumentException("Invalid bracketed host/port: %s".formatted(hostPortString));
134134
}
135135

136136
String host = hostPortString.substring(1, closeBracketIndex);
137137
if (closeBracketIndex + 1 == hostPortString.length()) {
138138
return new String[] { host, "" };
139139
} else {
140140
if (!(hostPortString.charAt(closeBracketIndex + 1) == ':')) {
141-
throw new IllegalArgumentException(
142-
String.format("Only a colon may follow a close bracket: %s", hostPortString));
141+
throw new IllegalArgumentException("Only a colon may follow a close bracket: %s".formatted(hostPortString));
143142
}
144143
for (int i = closeBracketIndex + 2; i < hostPortString.length(); ++i) {
145144
if (!Character.isDigit(hostPortString.charAt(i))) {
146-
throw new IllegalArgumentException(String.format("Port must be numeric: %s", hostPortString));
145+
throw new IllegalArgumentException("Port must be numeric: %s".formatted(hostPortString));
147146
}
148147
}
149148
return new String[] { host, hostPortString.substring(closeBracketIndex + 2) };

src/main/java/org/springframework/data/redis/connection/RedisPassword.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public Optional<char[]> toOptional() {
140140

141141
@Override
142142
public String toString() {
143-
return String.format("%s[%s]", getClass().getSimpleName(), isPresent() ? "*****" : "<none>");
143+
return "%s[%s]".formatted(getClass().getSimpleName(), isPresent() ? "*****" : "<none>");
144144
}
145145

146146
@Override

src/main/java/org/springframework/data/redis/connection/RedisSentinelConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public RedisSentinelConfiguration(PropertySource<?> propertySource) {
150150
try {
151151
database = Integer.parseInt(databaseSource);
152152
} catch (NumberFormatException ex) {
153-
throw new IllegalArgumentException(String.format("Invalid DB index '%s'; integer required", databaseSource));
153+
throw new IllegalArgumentException("Invalid DB index '%s'; integer required".formatted(databaseSource));
154154
}
155155
this.setDatabase(database);
156156
}
@@ -266,7 +266,7 @@ public int getDatabase() {
266266
@Override
267267
public void setDatabase(int index) {
268268

269-
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%d'; non-negative index required", index));
269+
Assert.isTrue(index >= 0, "Invalid DB index '%d'; non-negative index required".formatted(index));
270270

271271
this.database = index;
272272
}

src/main/java/org/springframework/data/redis/connection/RedisSocketConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public int getDatabase() {
7474
@Override
7575
public void setDatabase(int index) {
7676

77-
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index));
77+
Assert.isTrue(index >= 0, () -> "Invalid DB index '%s'; non-negative index required".formatted(index));
7878

7979
this.database = index;
8080
}

src/main/java/org/springframework/data/redis/connection/RedisStandaloneConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public RedisStandaloneConfiguration(String hostName, int port) {
7070

7171
Assert.hasText(hostName, "Host name must not be null or empty");
7272
Assert.isTrue(port >= 1 && port <= 65535,
73-
() -> String.format("Port %d must be a valid TCP port in the range between 1-65535", port));
73+
"Port %d must be a valid TCP port in the range between 1-65535".formatted(port));
7474

7575
this.hostName = hostName;
7676
this.port = port;
@@ -103,7 +103,7 @@ public int getDatabase() {
103103
@Override
104104
public void setDatabase(int index) {
105105

106-
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index));
106+
Assert.isTrue(index >= 0, "Invalid DB index '%d'; non-negative index required".formatted(index));
107107

108108
this.database = index;
109109
}

src/main/java/org/springframework/data/redis/connection/RedisStaticMasterReplicaConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public int getDatabase() {
118118
@Override
119119
public void setDatabase(int index) {
120120

121-
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index));
121+
Assert.isTrue(index >= 0, "Invalid DB index '%d'; non-negative index required".formatted(index));
122122

123123
this.database = index;
124124
this.nodes.forEach(it -> it.setDatabase(database));

src/main/java/org/springframework/data/redis/connection/convert/Converters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public static Object parse(Object source, String sourcePath, Map<String, Class<?
410410
}
411411

412412
if (LOGGER.isDebugEnabled()) {
413-
LOGGER.debug(String.format("parsing %s (%s) as %s", sourcePath, path, targetType));
413+
LOGGER.debug("parsing %s (%s) as %s".formatted(sourcePath, path, targetType));
414414
}
415415

416416
if (targetType == Object.class) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ public Jedis getResourceForSpecificNode(RedisClusterNode node) {
761761
return new Jedis(connection);
762762
}
763763

764-
throw new DataAccessResourceFailureException(String.format("Node %s is unknown to cluster", node));
764+
throw new DataAccessResourceFailureException("Node %s is unknown to cluster".formatted(node));
765765
}
766766

767767
private ConnectionPool getResourcePoolForSpecificNode(RedisClusterNode node) {
@@ -779,8 +779,8 @@ private Connection getConnectionForSpecificNode(RedisClusterNode node) {
779779
RedisClusterNode member = topologyProvider.getTopology().lookup(node);
780780

781781
if (!member.hasValidHost()) {
782-
throw new DataAccessResourceFailureException(String
783-
.format("Cannot obtain connection to node %ss as it is not associated with a hostname", node.getId()));
782+
throw new DataAccessResourceFailureException(
783+
"Cannot obtain connection to node %ss; " + "it is not associated with a hostname".formatted(node.getId()));
784784
}
785785

786786
if (member != null && connectionHandler != null) {
@@ -872,7 +872,7 @@ public ClusterTopology getTopology() {
872872
StringBuilder stringBuilder = new StringBuilder();
873873

874874
for (Entry<String, Exception> entry : errors.entrySet()) {
875-
stringBuilder.append(String.format("\r\n\t- %s failed: %s", entry.getKey(), entry.getValue().getMessage()));
875+
stringBuilder.append("\r\n\t- %s failed: %s".formatted(entry.getKey(), entry.getValue().getMessage()));
876876
}
877877

878878
throw new ClusterStateFailureException(

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,11 @@ public Long time(RedisClusterNode node, TimeUnit timeUnit) {
347347
public void killClient(String host, int port) {
348348

349349
Assert.hasText(host, "Host for 'CLIENT KILL' must not be 'null' or 'empty'");
350-
String hostAndPort = String.format("%s:%s", host, port);
350+
String hostAndPort = "%s:%d".formatted(host, port);
351351

352-
connection.getClusterCommandExecutor()
353-
.executeCommandOnAllNodes((JedisClusterCommandCallback<String>) client -> client.clientKill(hostAndPort));
352+
JedisClusterCommandCallback<String> command = client -> client.clientKill(hostAndPort);
353+
354+
connection.getClusterCommandExecutor().executeCommandOnAllNodes(command);
354355
}
355356

356357
@Override

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,9 @@ public Set<Tuple> zInterWithScores(Aggregate aggregate, Weights weights, byte[].
908908

909909
Assert.notNull(sets, "Sets must not be null");
910910
Assert.noNullElements(sets, "Source sets must not contain null elements");
911-
Assert.isTrue(weights.size() == sets.length, () -> String
912-
.format("The number of weights (%d) must match the number of source sets (%d)", weights.size(), sets.length));
911+
Assert.isTrue(weights.size() == sets.length,
912+
() -> "The number of weights %d must match the number of source sets %d".formatted(weights.size(),
913+
sets.length));
913914

914915
if (ClusterSlotHashUtil.isSameSlotForAllKeys(sets)) {
915916

@@ -951,8 +952,8 @@ public Long zInterStore(byte[] destKey, Aggregate aggregate, Weights weights, by
951952
Assert.notNull(destKey, "Destination key must not be null");
952953
Assert.notNull(sets, "Source sets must not be null");
953954
Assert.noNullElements(sets, "Source sets must not contain null elements");
954-
Assert.isTrue(weights.size() == sets.length, () -> String
955-
.format("The number of weights (%d) must match the number of source sets (%d)", weights.size(), sets.length));
955+
Assert.isTrue(weights.size() == sets.length,
956+
"The number of weights %d must match the number of source sets %d".formatted(weights.size(), sets.length));
956957

957958
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
958959

@@ -1008,8 +1009,9 @@ public Set<Tuple> zUnionWithScores(Aggregate aggregate, Weights weights, byte[].
10081009

10091010
Assert.notNull(sets, "Sets must not be null");
10101011
Assert.noNullElements(sets, "Source sets must not contain null elements");
1011-
Assert.isTrue(weights.size() == sets.length, () -> String
1012-
.format("The number of weights (%d) must match the number of source sets (%d)", weights.size(), sets.length));
1012+
Assert.isTrue(weights.size() == sets.length,
1013+
() -> "The number of weights %d must match the number of source sets %d".formatted(weights.size(),
1014+
sets.length));
10131015

10141016
if (ClusterSlotHashUtil.isSameSlotForAllKeys(sets)) {
10151017

@@ -1052,8 +1054,8 @@ public Long zUnionStore(byte[] destKey, Aggregate aggregate, Weights weights, by
10521054
Assert.notNull(destKey, "Destination key must not be null");
10531055
Assert.notNull(sets, "Source sets must not be null");
10541056
Assert.noNullElements(sets, "Source sets must not contain null elements");
1055-
Assert.isTrue(weights.size() == sets.length, () -> String
1056-
.format("The number of weights (%d) must match the number of source sets (%d)", weights.size(), sets.length));
1057+
Assert.isTrue(weights.size() == sets.length,
1058+
"The number of weights %d must match the number of source sets %d".formatted(weights.size(), sets.length));
10571059

10581060
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
10591061

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ private Jedis getActiveSentinel() {
10031003
return jedis;
10041004
}
10051005
} catch (Exception ex) {
1006-
log.warn(String.format("Ping failed for sentinel host: %s", node.getHost()), ex);
1006+
log.warn("Ping failed for sentinel host: %s".formatted(node.getHost()), ex);
10071007
} finally {
10081008
if (!success && jedis != null) {
10091009
jedis.close();
@@ -1040,8 +1040,8 @@ private int getConnectTimeout() {
10401040
private MutableJedisClientConfiguration getMutableConfiguration() {
10411041

10421042
Assert.state(clientConfiguration instanceof MutableJedisClientConfiguration,
1043-
() -> String.format("Client configuration must be instance of MutableJedisClientConfiguration but is %s",
1044-
ClassUtils.getShortName(clientConfiguration.getClass())));
1043+
() -> "Client configuration must be instance of MutableJedisClientConfiguration but is %s"
1044+
.formatted(ClassUtils.getShortName(clientConfiguration.getClass())));
10451045

10461046
return (MutableJedisClientConfiguration) clientConfiguration;
10471047
}
@@ -1056,10 +1056,10 @@ private void assertInitialized() {
10561056

10571057
switch (current) {
10581058
case CREATED, STOPPED -> throw new IllegalStateException(
1059-
String.format("JedisConnectionFactory has been %s. Use start() to initialize it", current));
1059+
"JedisConnectionFactory has been %s. Use start() to initialize it".formatted(current));
10601060
case DESTROYED ->
10611061
throw new IllegalStateException("JedisConnectionFactory was destroyed and cannot be used anymore");
1062-
default -> throw new IllegalStateException(String.format("JedisConnectionFactory is %s", current));
1062+
default -> throw new IllegalStateException("JedisConnectionFactory is %s".formatted(current));
10631063
}
10641064
}
10651065

0 commit comments

Comments
 (0)