Skip to content

Commit 5dd43c1

Browse files
junghoon-vansmp911de
authored andcommitted
Use pattern matching instead of type casting.
Closes #2754
1 parent 8fefea7 commit 5dd43c1

36 files changed

+139
-172
lines changed

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

+3-13
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

+1-3
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

+6-8
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

+1-2
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

+10-10
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,16 @@ private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Boun
453453
byte[] prefix = boundary.isInclusive() ? inclPrefix : exclPrefix;
454454
byte[] value = null;
455455
Object theValue = boundary.getValue().get();
456-
if (theValue instanceof byte[]) {
457-
value = (byte[]) theValue;
458-
} else if (theValue instanceof Double) {
459-
value = toBytes((Double) theValue);
460-
} else if (theValue instanceof Long) {
461-
value = toBytes((Long) theValue);
462-
} else if (theValue instanceof Integer) {
463-
value = toBytes((Integer) theValue);
464-
} else if (theValue instanceof String) {
465-
value = toBytes((String) theValue);
456+
if (theValue instanceof byte[] bytes) {
457+
value = bytes;
458+
} else if (theValue instanceof Double doubleValue) {
459+
value = toBytes(doubleValue);
460+
} else if (theValue instanceof Long longValue) {
461+
value = toBytes(longValue);
462+
} else if (theValue instanceof Integer integer) {
463+
value = toBytes(integer);
464+
} else if (theValue instanceof String string) {
465+
value = toBytes(string);
466466
} else {
467467
throw new IllegalArgumentException(String.format("Cannot convert %s to binary format", boundary.getValue()));
468468
}

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

+5-5
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

+4-4
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

+4-4
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

+2-2
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

+1-1
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();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private static Object preConvertNativeValues(@Nullable Object value) {
135135

136136
if (value instanceof ByteBuffer || value instanceof byte[]) {
137137

138-
byte[] targetArray = value instanceof ByteBuffer ? ByteUtils.getBytes((ByteBuffer) value) : (byte[]) value;
138+
byte[] targetArray = value instanceof ByteBuffer byteBuffer ? ByteUtils.getBytes(byteBuffer) : (byte[]) value;
139139
String tmp = LettuceConverters.toString(targetArray);
140140

141141
try {
@@ -144,9 +144,9 @@ private static Object preConvertNativeValues(@Nullable Object value) {
144144
return tmp;
145145
}
146146
}
147-
if (value instanceof List) {
147+
if (value instanceof List listValue) {
148148
List<Object> targetList = new ArrayList<>();
149-
for (Object it : (List) value) {
149+
for (Object it : listValue) {
150150
targetList.add(preConvertNativeValues(it));
151151
}
152152
return targetList;

src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public ByteArrayWrapper(byte[] array) {
4141
}
4242

4343
public boolean equals(@Nullable Object obj) {
44-
if (obj instanceof ByteArrayWrapper) {
45-
return Arrays.equals(array, ((ByteArrayWrapper) obj).array);
44+
if (obj instanceof ByteArrayWrapper byteArrayWrapper) {
45+
return Arrays.equals(array, byteArrayWrapper.array);
4646
}
4747

4848
return false;

src/main/java/org/springframework/data/redis/core/AbstractOperations.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ byte[] rawKey(Object key) {
106106

107107
Assert.notNull(key, "non null key required");
108108

109-
if (keySerializer() == null && key instanceof byte[]) {
110-
return (byte[]) key;
109+
if (keySerializer() == null && key instanceof byte[] bytes) {
110+
return bytes;
111111
}
112112

113113
return keySerializer().serialize(key);
@@ -121,8 +121,8 @@ byte[] rawString(String key) {
121121
@SuppressWarnings("unchecked")
122122
byte[] rawValue(Object value) {
123123

124-
if (valueSerializer() == null && value instanceof byte[]) {
125-
return (byte[]) value;
124+
if (valueSerializer() == null && value instanceof byte[] bytes) {
125+
return bytes;
126126
}
127127

128128
return valueSerializer().serialize(value);
@@ -161,8 +161,8 @@ byte[][] rawValues(Collection<V> values) {
161161
@SuppressWarnings("unchecked")
162162
<HK> byte[] rawHashKey(HK hashKey) {
163163
Assert.notNull(hashKey, "non null hash key required");
164-
if (hashKeySerializer() == null && hashKey instanceof byte[]) {
165-
return (byte[]) hashKey;
164+
if (hashKeySerializer() == null && hashKey instanceof byte[] bytes) {
165+
return bytes;
166166
}
167167
return hashKeySerializer().serialize(hashKey);
168168
}
@@ -180,8 +180,8 @@ <HK> byte[][] rawHashKeys(HK... hashKeys) {
180180
@SuppressWarnings("unchecked")
181181
<HV> byte[] rawHashValue(HV value) {
182182

183-
if (hashValueSerializer() == null && value instanceof byte[]) {
184-
return (byte[]) value;
183+
if (hashValueSerializer() == null && value instanceof byte[] bytes) {
184+
return bytes;
185185
}
186186
return hashValueSerializer().serialize(value);
187187
}
@@ -268,8 +268,8 @@ Set<Tuple> rawTupleValues(Set<TypedTuple<V>> values) {
268268
Set<Tuple> rawTuples = new LinkedHashSet<>(values.size());
269269
for (TypedTuple<V> value : values) {
270270
byte[] rawValue;
271-
if (valueSerializer() == null && value.getValue() instanceof byte[]) {
272-
rawValue = (byte[]) value.getValue();
271+
if (valueSerializer() == null && value.getValue() instanceof byte[] bytes) {
272+
rawValue = bytes;
273273
} else {
274274
rawValue = valueSerializer().serialize(value.getValue());
275275
}

src/main/java/org/springframework/data/redis/core/DefaultTypedTuple.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ public boolean equals(@Nullable Object obj) {
6464
return true;
6565
if (obj == null)
6666
return false;
67-
if (!(obj instanceof DefaultTypedTuple))
67+
if (!(obj instanceof DefaultTypedTuple<?> other))
6868
return false;
69-
DefaultTypedTuple<?> other = (DefaultTypedTuple<?>) obj;
7069
if (score == null) {
7170
if (other.score != null)
7271
return false;
@@ -75,11 +74,11 @@ public boolean equals(@Nullable Object obj) {
7574
if (value == null) {
7675
if (other.value != null)
7776
return false;
78-
} else if (value instanceof byte[]) {
79-
if (!(other.value instanceof byte[])) {
77+
} else if (value instanceof byte[] bytes) {
78+
if (!(other.value instanceof byte[] otherBytes)) {
8079
return false;
8180
}
82-
return Arrays.equals((byte[]) value, (byte[]) other.value);
81+
return Arrays.equals(bytes, otherBytes);
8382
} else if (!value.equals(other.value))
8483
return false;
8584
return true;

src/main/java/org/springframework/data/redis/core/GeoOperationsEditor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class GeoOperationsEditor extends PropertyEditorSupport {
2828

2929
public void setValue(Object value) {
3030

31-
if (value instanceof RedisOperations) {
32-
super.setValue(((RedisOperations) value).opsForGeo());
31+
if (value instanceof RedisOperations<?, ?> redisOperations) {
32+
super.setValue(redisOperations.opsForGeo());
3333
} else {
3434
throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
3535
}

src/main/java/org/springframework/data/redis/core/HashOperationsEditor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
class HashOperationsEditor extends PropertyEditorSupport {
2626

2727
public void setValue(Object value) {
28-
if (value instanceof RedisOperations) {
29-
super.setValue(((RedisOperations) value).opsForHash());
28+
if (value instanceof RedisOperations<?, ?> redisOperations) {
29+
super.setValue(redisOperations.opsForHash());
3030
} else {
3131
throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
3232
}

src/main/java/org/springframework/data/redis/core/IndexWriter.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) {
208208
return;
209209
}
210210

211-
if (indexedData instanceof SimpleIndexedPropertyValue) {
211+
if (indexedData instanceof SimpleIndexedPropertyValue simpleIndexedData) {
212212

213-
Object value = ((SimpleIndexedPropertyValue) indexedData).getValue();
213+
Object value = simpleIndexedData.getValue();
214214

215215
if (value == null) {
216216
return;
@@ -222,9 +222,7 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) {
222222

223223
// keep track of indexes used for the object
224224
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
225-
} else if (indexedData instanceof GeoIndexedPropertyValue) {
226-
227-
GeoIndexedPropertyValue geoIndexedData = ((GeoIndexedPropertyValue) indexedData);
225+
} else if (indexedData instanceof GeoIndexedPropertyValue geoIndexedData) {
228226

229227
Object value = geoIndexedData.getValue();
230228
if (value == null) {
@@ -248,8 +246,8 @@ private byte[] toBytes(@Nullable Object source) {
248246
return new byte[] {};
249247
}
250248

251-
if (source instanceof byte[]) {
252-
return (byte[]) source;
249+
if (source instanceof byte[] bytes) {
250+
return bytes;
253251
}
254252

255253
if (converter.getConversionService().canConvert(source.getClass(), byte[].class)) {

src/main/java/org/springframework/data/redis/core/ListOperationsEditor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
class ListOperationsEditor extends PropertyEditorSupport {
2626

2727
public void setValue(Object value) {
28-
if (value instanceof RedisOperations) {
29-
super.setValue(((RedisOperations) value).opsForList());
28+
if (value instanceof RedisOperations<?, ?> redisOperations) {
29+
super.setValue(redisOperations.opsForList());
3030
} else {
3131
throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
3232
}

0 commit comments

Comments
 (0)