Skip to content

Commit 85e9ae5

Browse files
junghoon-vansmp911de
authored andcommitted
Use pattern matching instead of type casting.
Closes spring-projects#2754
1 parent f28bf61 commit 85e9ae5

36 files changed

+140
-176
lines changed

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26-
2726
import org.springframework.core.convert.converter.Converter;
2827
import org.springframework.data.geo.Circle;
2928
import org.springframework.data.geo.Distance;
@@ -35,19 +34,10 @@
3534
import org.springframework.data.redis.connection.convert.ListConverter;
3635
import org.springframework.data.redis.connection.convert.MapConverter;
3736
import org.springframework.data.redis.connection.convert.SetConverter;
38-
import org.springframework.data.redis.connection.stream.ByteRecord;
39-
import org.springframework.data.redis.connection.stream.Consumer;
40-
import org.springframework.data.redis.connection.stream.MapRecord;
41-
import org.springframework.data.redis.connection.stream.PendingMessages;
42-
import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
43-
import org.springframework.data.redis.connection.stream.ReadOffset;
44-
import org.springframework.data.redis.connection.stream.RecordId;
37+
import org.springframework.data.redis.connection.stream.*;
4538
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoConsumers;
4639
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoGroups;
4740
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoStream;
48-
import org.springframework.data.redis.connection.stream.StreamOffset;
49-
import org.springframework.data.redis.connection.stream.StreamReadOptions;
50-
import org.springframework.data.redis.connection.stream.StringRecord;
5141
import org.springframework.data.redis.connection.zset.Aggregate;
5242
import org.springframework.data.redis.connection.zset.DefaultTuple;
5343
import org.springframework.data.redis.connection.zset.Tuple;
@@ -3046,8 +3036,8 @@ private <T> T convertAndReturn(@Nullable Object value, Converter converter) {
30463036
return null;
30473037
}
30483038

3049-
if (!(converter instanceof ListConverter) && value instanceof List) {
3050-
return (T) new ListConverter<>(converter).convert((List) value);
3039+
if (!(converter instanceof ListConverter) && value instanceof List list) {
3040+
return (T) new ListConverter<>(converter).convert(list);
30513041
}
30523042

30533043
return value == null ? null

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,10 @@ public boolean equals(@Nullable Object obj) {
274274
if (this == obj) {
275275
return true;
276276
}
277-
if (obj == null || !(obj instanceof RedisNode)) {
277+
if (obj == null || !(obj instanceof RedisNode other)) {
278278
return false;
279279
}
280280

281-
RedisNode other = (RedisNode) obj;
282-
283281
if (!ObjectUtils.nullSafeEquals(this.host, other.host)) {
284282
return false;
285283
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,16 @@ public static Object parse(Object source, String sourcePath, Map<String, Class<?
421421
if (source instanceof String) {
422422
return source.toString();
423423
}
424-
if (source instanceof byte[]) {
425-
return new String((byte[]) source);
424+
if (source instanceof byte[] bytes) {
425+
return new String(bytes);
426426
}
427-
if (source instanceof ByteBuffer) {
428-
return new String(ByteUtils.getBytes((ByteBuffer) source));
427+
if (source instanceof ByteBuffer byteBuffer) {
428+
return new String(ByteUtils.getBytes(byteBuffer));
429429
}
430430
}
431431

432-
if (ClassUtils.isAssignable(List.class, targetType) && source instanceof List) {
432+
if (ClassUtils.isAssignable(List.class, targetType) && source instanceof List<?> sourceCollection) {
433433

434-
List<Object> sourceCollection = (List<Object>) source;
435434
List<Object> targetList = new ArrayList<>();
436435

437436
for (int i = 0; i < sourceCollection.size(); i++) {
@@ -441,9 +440,8 @@ public static Object parse(Object source, String sourcePath, Map<String, Class<?
441440
return targetList;
442441
}
443442

444-
if (ClassUtils.isAssignable(Map.class, targetType) && source instanceof List) {
443+
if (ClassUtils.isAssignable(Map.class, targetType) && source instanceof List<?> sourceCollection) {
445444

446-
List<Object> sourceCollection = ((List<Object>) source);
447445
Map<String, Object> targetMap = new LinkedHashMap<>();
448446

449447
for (int i = 0; i < sourceCollection.size(); i = i + 2) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ public List<Object> convert(List<Object> execResults) {
5858

5959
for (Object result : execResults) {
6060
FutureResult<T> futureResult = txResults.remove();
61-
if (result instanceof Exception) {
61+
if (result instanceof Exception source) {
6262

63-
Exception source = (Exception) result;
6463
DataAccessException convertedException = exceptionConverter.convert(source);
6564
throw convertedException != null ? convertedException
6665
: new RedisSystemException("Error reading future result", source);

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
import org.springframework.util.ObjectUtils;
8989
import org.springframework.util.StringUtils;
9090

91-
import redis.clients.jedis.util.SafeEncoder;
92-
9391
/**
9492
* Jedis type converters.
9593
*
@@ -470,16 +468,16 @@ private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Boun
470468
byte[] prefix = boundary.isInclusive() ? inclPrefix : exclPrefix;
471469
byte[] value = null;
472470
Object theValue = boundary.getValue().get();
473-
if (theValue instanceof byte[]) {
474-
value = (byte[]) theValue;
475-
} else if (theValue instanceof Double) {
476-
value = toBytes((Double) theValue);
477-
} else if (theValue instanceof Long) {
478-
value = toBytes((Long) theValue);
479-
} else if (theValue instanceof Integer) {
480-
value = toBytes((Integer) theValue);
481-
} else if (theValue instanceof String) {
482-
value = toBytes((String) theValue);
471+
if (theValue instanceof byte[] bytes) {
472+
value = bytes;
473+
} else if (theValue instanceof Double doubleValue) {
474+
value = toBytes(doubleValue);
475+
} else if (theValue instanceof Long longValue) {
476+
value = toBytes(longValue);
477+
} else if (theValue instanceof Integer integer) {
478+
value = toBytes(integer);
479+
} else if (theValue instanceof String string) {
480+
value = toBytes(string);
483481
} else {
484482
throw new IllegalArgumentException(String.format("Cannot convert %s to binary format", boundary.getValue()));
485483
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public class JedisExceptionConverter implements Converter<Exception, DataAccessE
4545

4646
public DataAccessException convert(Exception ex) {
4747

48-
if (ex instanceof DataAccessException) {
49-
return (DataAccessException) ex;
48+
if (ex instanceof DataAccessException dataAccessException) {
49+
return dataAccessException;
5050
}
5151

5252
if (ex instanceof UnsupportedOperationException) {
@@ -57,10 +57,10 @@ public DataAccessException convert(Exception ex) {
5757
return new TooManyClusterRedirectionsException(ex.getMessage(), ex);
5858
}
5959

60-
if (ex instanceof JedisRedirectionException) {
60+
if (ex instanceof JedisRedirectionException redirectionException) {
6161

62-
JedisRedirectionException re = (JedisRedirectionException) ex;
63-
return new ClusterRedirectException(re.getSlot(), re.getTargetNode().getHost(), re.getTargetNode().getPort(), ex);
62+
return new ClusterRedirectException(redirectionException.getSlot(),
63+
redirectionException.getTargetNode().getHost(), redirectionException.getTargetNode().getPort(), ex);
6464
}
6565

6666
if (ex instanceof JedisConnectionException) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public JedisScriptReturnConverter(ReturnType returnType) {
3939

4040
@SuppressWarnings("unchecked")
4141
public Object convert(@Nullable Object result) {
42-
if (result instanceof String) {
42+
if (result instanceof String stringResult) {
4343
// evalsha converts byte[] to String. Convert back for consistency
44-
return SafeEncoder.encode((String) result);
44+
return SafeEncoder.encode(stringResult);
4545
}
4646
if (returnType == ReturnType.STATUS) {
4747
return JedisConverters.toString((byte[]) result);
@@ -57,10 +57,10 @@ public Object convert(@Nullable Object result) {
5757
List<Object> resultList = (List<Object>) result;
5858
List<Object> convertedResults = new ArrayList<>();
5959
for (Object res : resultList) {
60-
if (res instanceof String) {
60+
if (res instanceof String stringResult) {
6161
// evalsha converts byte[] to String. Convert back for
6262
// consistency
63-
convertedResults.add(SafeEncoder.encode((String) res));
63+
convertedResults.add(SafeEncoder.encode(stringResult));
6464
} else {
6565
convertedResults.add(res);
6666
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ public DataAccessException convert(Exception ex) {
4646

4747
if (ex instanceof ExecutionException || ex instanceof RedisCommandExecutionException) {
4848

49-
if (ex.getCause() != ex && ex.getCause() instanceof Exception) {
50-
return convert((Exception) ex.getCause());
49+
if (ex.getCause() != ex && ex.getCause() instanceof Exception cause) {
50+
return convert(cause);
5151
}
5252
return new RedisSystemException("Error in execution", ex);
5353
}
5454

55-
if (ex instanceof DataAccessException) {
56-
return (DataAccessException) ex;
55+
if (ex instanceof DataAccessException dataAccessException) {
56+
return dataAccessException;
5757
}
5858

5959
if (ex instanceof RedisCommandInterruptedException) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ public T convert(Object source) {
134134
if (returnType == ReturnType.MULTI) {
135135
List resultList = (List) source;
136136
for (Object obj : resultList) {
137-
if (obj instanceof Exception) {
138-
throw connection.convertLettuceAccessException((Exception) obj);
137+
if (obj instanceof Exception ex) {
138+
throw connection.convertLettuceAccessException(ex);
139139
}
140140
}
141141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected LettuceSubscription(MessageListener listener,
6060

6161
this.connection = pubsubConnection;
6262
this.listener = new LettuceMessageListener(listener,
63-
listener instanceof SubscriptionListener ? (SubscriptionListener) listener
63+
listener instanceof SubscriptionListener subscriptionListener ? subscriptionListener
6464
: SubscriptionListener.NO_OP_SUBSCRIPTION_LISTENER);
6565
this.connectionProvider = connectionProvider;
6666
this.pubsub = connection.sync();

0 commit comments

Comments
 (0)