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
}

0 commit comments

Comments
 (0)