Skip to content

Commit d271182

Browse files
committed
Replace use of String.format(..) with formatted Strings.
Closes #2751
1 parent 3d7cb3d commit d271182

File tree

83 files changed

+316
-351
lines changed

Some content is hidden

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

83 files changed

+316
-351
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
@@ -43,7 +43,7 @@ public class ClusterRedirectException extends DataRetrievalFailureException {
4343
*/
4444
public ClusterRedirectException(int slot, String targetHost, int targetPort, Throwable e) {
4545

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

4848
this.slot = slot;
4949
this.host = targetHost;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,8 @@ private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection c
382382
// Re-interrupt current Thread to allow other participants to react.
383383
Thread.currentThread().interrupt();
384384

385-
String message = String.format("Interrupted while waiting to unlock cache %s", name);
386-
387-
throw new PessimisticLockingFailureException(message, ex);
385+
throw new PessimisticLockingFailureException("Interrupted while waiting to unlock cache %s"
386+
.formatted(name), ex);
388387
} finally {
389388
this.statistics.incLockTime(name, System.nanoTime() - lockWaitTimeNs);
390389
}

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

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

326326
if (nullCacheValueIsNotAllowed(cacheValue)) {
327-
328-
String message = String.format("Cache '%s' does not allow 'null' values; Avoid storing null"
327+
throw new IllegalArgumentException(("Cache '%s' does not allow 'null' values; Avoid storing null"
329328
+ " via '@Cacheable(unless=\"#result == null\")' or configure RedisCache to allow 'null'"
330-
+ " via RedisCacheConfiguration", getName());
331-
332-
throw new IllegalArgumentException(message);
329+
+ " via RedisCacheConfiguration").formatted(getName()));
333330
}
334331

335332
return cacheValue;
@@ -440,11 +437,9 @@ protected String convertKey(Object key) {
440437
return key.toString();
441438
}
442439

443-
String message = String.format("Cannot convert cache key %s to String; Please register a suitable Converter"
444-
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'",
445-
source, key.getClass().getName());
446-
447-
throw new IllegalStateException(message);
440+
throw new IllegalStateException(("Cannot convert cache key %s to String; Please register a suitable Converter"
441+
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'")
442+
.formatted(source, key.getClass().getName()));
448443
}
449444

450445
private CompletableFuture<byte[]> retrieveValue(Object key) {
@@ -502,7 +497,7 @@ private String convertCollectionLikeOrMapKey(Object key, TypeDescriptor source)
502497
return "[" + stringJoiner + "]";
503498
}
504499

505-
throw new IllegalArgumentException(String.format("Cannot convert cache key [%s] to String", key));
500+
throw new IllegalArgumentException("Cannot convert cache key [%s] to String".formatted(key));
506501
}
507502

508503
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ public RedisClusterNode getKeyServingMasterNode(byte[] key) {
140140
}
141141
}
142142

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

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

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

167167
/**
@@ -181,8 +181,8 @@ public RedisClusterNode lookup(String nodeId) {
181181
}
182182
}
183183

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

188188
/**
@@ -209,8 +209,8 @@ public RedisClusterNode lookup(RedisClusterNode node) {
209209
return lookup(node.getId());
210210
}
211211

212-
throw new ClusterStateFailureException(
213-
String.format("Could not find node at %s; Have you provided either host and port or the nodeId", node));
212+
throw new ClusterStateFailureException(("Could not find node at %s;"
213+
+ " 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: 8 additions & 8 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);
@@ -122,28 +122,28 @@ public static RedisNode fromString(String hostPortString) {
122122
private static String[] getHostAndPortFromBracketedHost(String hostPortString) {
123123

124124
if (hostPortString.charAt(0) != '[') {
125-
throw new IllegalArgumentException(
126-
String.format("Bracketed host-port string must start with a bracket: %s", hostPortString));
125+
throw new IllegalArgumentException("Bracketed host-port string must start with a bracket: %s"
126+
.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"
142+
.formatted(hostPortString));
143143
}
144144
for (int i = closeBracketIndex + 2; i < hostPortString.length(); ++i) {
145145
if (!Character.isDigit(hostPortString.charAt(i))) {
146-
throw new IllegalArgumentException(String.format("Port must be numeric: %s", hostPortString));
146+
throw new IllegalArgumentException("Port must be numeric: %s".formatted(hostPortString));
147147
}
148148
}
149149
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public int getDatabase() {
223223
@Override
224224
public void setDatabase(int index) {
225225

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

228228
this.database = index;
229229
}

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public RedisStandaloneConfiguration(String hostName) {
6969
public RedisStandaloneConfiguration(String hostName, int port) {
7070

7171
Assert.hasText(hostName, "Host name must not be null or empty");
72-
Assert.isTrue(port >= 1 && port <= 65535,
73-
() -> String.format("Port %d must be a valid TCP port in the range between 1-65535", port));
72+
Assert.isTrue(port >= 1 && port <= 65535, "Port %d must be a valid TCP port in the range between 1-65535"
73+
.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("Cannot obtain connection to node %ss; "
783+
+ "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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,8 @@ 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(), sets.length));
913913

914914
if (ClusterSlotHashUtil.isSameSlotForAllKeys(sets)) {
915915

@@ -951,8 +951,8 @@ public Long zInterStore(byte[] destKey, Aggregate aggregate, Weights weights, by
951951
Assert.notNull(destKey, "Destination key must not be null");
952952
Assert.notNull(sets, "Source sets must not be null");
953953
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));
954+
Assert.isTrue(weights.size() == sets.length,
955+
"The number of weights %d must match the number of source sets %d".formatted(weights.size(), sets.length));
956956

957957
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
958958

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

10091009
Assert.notNull(sets, "Sets must not be null");
10101010
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));
1011+
Assert.isTrue(weights.size() == sets.length, () ->
1012+
"The number of weights %d must match the number of source sets %d".formatted(weights.size(), sets.length));
10131013

10141014
if (ClusterSlotHashUtil.isSameSlotForAllKeys(sets)) {
10151015

@@ -1052,8 +1052,8 @@ public Long zUnionStore(byte[] destKey, Aggregate aggregate, Weights weights, by
10521052
Assert.notNull(destKey, "Destination key must not be null");
10531053
Assert.notNull(sets, "Source sets must not be null");
10541054
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));
1055+
Assert.isTrue(weights.size() == sets.length,
1056+
"The number of weights %d must match the number of source sets %d".formatted(weights.size(), sets.length));
10571057

10581058
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
10591059

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
@@ -952,7 +952,7 @@ private Jedis getActiveSentinel() {
952952
return jedis;
953953
}
954954
} catch (Exception ex) {
955-
log.warn(String.format("Ping failed for sentinel host: %s", node.getHost()), ex);
955+
log.warn("Ping failed for sentinel host: %s".formatted(node.getHost()), ex);
956956
} finally {
957957
if (!success && jedis != null) {
958958
jedis.close();
@@ -989,8 +989,8 @@ private int getConnectTimeout() {
989989
private MutableJedisClientConfiguration getMutableConfiguration() {
990990

991991
Assert.state(clientConfiguration instanceof MutableJedisClientConfiguration,
992-
() -> String.format("Client configuration must be instance of MutableJedisClientConfiguration but is %s",
993-
ClassUtils.getShortName(clientConfiguration.getClass())));
992+
() -> "Client configuration must be instance of MutableJedisClientConfiguration but is %s"
993+
.formatted(ClassUtils.getShortName(clientConfiguration.getClass())));
994994

995995
return (MutableJedisClientConfiguration) clientConfiguration;
996996
}
@@ -1005,10 +1005,10 @@ private void assertInitialized() {
10051005

10061006
switch (current) {
10071007
case CREATED, STOPPED -> throw new IllegalStateException(
1008-
String.format("JedisConnectionFactory has been %s. Use start() to initialize it", current));
1008+
"JedisConnectionFactory has been %s. Use start() to initialize it".formatted(current));
10091009
case DESTROYED -> throw new IllegalStateException(
10101010
"JedisConnectionFactory was destroyed and cannot be used anymore");
1011-
default -> throw new IllegalStateException(String.format("JedisConnectionFactory is %s", current));
1011+
default -> throw new IllegalStateException("JedisConnectionFactory is %s".formatted(current));
10121012
}
10131013
}
10141014

0 commit comments

Comments
 (0)