diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java index 533b16362..857f430e2 100644 --- a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java @@ -23,7 +23,6 @@ import java.util.function.IntFunction; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.core.convert.converter.Converter; import org.springframework.data.geo.Circle; import org.springframework.data.geo.Distance; @@ -35,19 +34,10 @@ import org.springframework.data.redis.connection.convert.Converters; import org.springframework.data.redis.connection.convert.ListConverter; import org.springframework.data.redis.connection.convert.MapConverter; import org.springframework.data.redis.connection.convert.SetConverter; -import org.springframework.data.redis.connection.stream.ByteRecord; -import org.springframework.data.redis.connection.stream.Consumer; -import org.springframework.data.redis.connection.stream.MapRecord; -import org.springframework.data.redis.connection.stream.PendingMessages; -import org.springframework.data.redis.connection.stream.PendingMessagesSummary; -import org.springframework.data.redis.connection.stream.ReadOffset; -import org.springframework.data.redis.connection.stream.RecordId; +import org.springframework.data.redis.connection.stream.*; import org.springframework.data.redis.connection.stream.StreamInfo.XInfoConsumers; import org.springframework.data.redis.connection.stream.StreamInfo.XInfoGroups; import org.springframework.data.redis.connection.stream.StreamInfo.XInfoStream; -import org.springframework.data.redis.connection.stream.StreamOffset; -import org.springframework.data.redis.connection.stream.StreamReadOptions; -import org.springframework.data.redis.connection.stream.StringRecord; import org.springframework.data.redis.connection.zset.Aggregate; import org.springframework.data.redis.connection.zset.DefaultTuple; import org.springframework.data.redis.connection.zset.Tuple; @@ -3046,8 +3036,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco return null; } - if (!(converter instanceof ListConverter) && value instanceof List) { - return (T) new ListConverter<>(converter).convert((List) value); + if (!(converter instanceof ListConverter) && value instanceof List list) { + return (T) new ListConverter<>(converter).convert(list); } return value == null ? null diff --git a/src/main/java/org/springframework/data/redis/connection/RedisNode.java b/src/main/java/org/springframework/data/redis/connection/RedisNode.java index 7c66f5a1e..6f2ff597f 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisNode.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisNode.java @@ -274,12 +274,10 @@ public class RedisNode implements NamedNode { if (this == obj) { return true; } - if (obj == null || !(obj instanceof RedisNode)) { + if (obj == null || !(obj instanceof RedisNode other)) { return false; } - RedisNode other = (RedisNode) obj; - if (!ObjectUtils.nullSafeEquals(this.host, other.host)) { return false; } diff --git a/src/main/java/org/springframework/data/redis/connection/convert/Converters.java b/src/main/java/org/springframework/data/redis/connection/convert/Converters.java index 94dbc9087..dd5f07810 100644 --- a/src/main/java/org/springframework/data/redis/connection/convert/Converters.java +++ b/src/main/java/org/springframework/data/redis/connection/convert/Converters.java @@ -421,17 +421,16 @@ public abstract class Converters { if (source instanceof String) { return source.toString(); } - if (source instanceof byte[]) { - return new String((byte[]) source); + if (source instanceof byte[] bytes) { + return new String(bytes); } - if (source instanceof ByteBuffer) { - return new String(ByteUtils.getBytes((ByteBuffer) source)); + if (source instanceof ByteBuffer byteBuffer) { + return new String(ByteUtils.getBytes(byteBuffer)); } } - if (ClassUtils.isAssignable(List.class, targetType) && source instanceof List) { + if (ClassUtils.isAssignable(List.class, targetType) && source instanceof List sourceCollection) { - List sourceCollection = (List) source; List targetList = new ArrayList<>(); for (int i = 0; i < sourceCollection.size(); i++) { @@ -441,9 +440,8 @@ public abstract class Converters { return targetList; } - if (ClassUtils.isAssignable(Map.class, targetType) && source instanceof List) { + if (ClassUtils.isAssignable(Map.class, targetType) && source instanceof List sourceCollection) { - List sourceCollection = ((List) source); Map targetMap = new LinkedHashMap<>(); for (int i = 0; i < sourceCollection.size(); i = i + 2) { diff --git a/src/main/java/org/springframework/data/redis/connection/convert/TransactionResultConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/TransactionResultConverter.java index 5ad11aecd..1ab6d9a30 100644 --- a/src/main/java/org/springframework/data/redis/connection/convert/TransactionResultConverter.java +++ b/src/main/java/org/springframework/data/redis/connection/convert/TransactionResultConverter.java @@ -58,9 +58,8 @@ public class TransactionResultConverter implements Converter, Li for (Object result : execResults) { FutureResult futureResult = txResults.remove(); - if (result instanceof Exception) { + if (result instanceof Exception source) { - Exception source = (Exception) result; DataAccessException convertedException = exceptionConverter.convert(source); throw convertedException != null ? convertedException : new RedisSystemException("Error reading future result", source); diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java index ac689c75c..376bb8bd2 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java @@ -88,8 +88,6 @@ import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; -import redis.clients.jedis.util.SafeEncoder; - /** * Jedis type converters. * @@ -470,16 +468,16 @@ abstract class JedisConverters extends Converters { byte[] prefix = boundary.isInclusive() ? inclPrefix : exclPrefix; byte[] value = null; Object theValue = boundary.getValue().get(); - if (theValue instanceof byte[]) { - value = (byte[]) theValue; - } else if (theValue instanceof Double) { - value = toBytes((Double) theValue); - } else if (theValue instanceof Long) { - value = toBytes((Long) theValue); - } else if (theValue instanceof Integer) { - value = toBytes((Integer) theValue); - } else if (theValue instanceof String) { - value = toBytes((String) theValue); + if (theValue instanceof byte[] bytes) { + value = bytes; + } else if (theValue instanceof Double doubleValue) { + value = toBytes(doubleValue); + } else if (theValue instanceof Long longValue) { + value = toBytes(longValue); + } else if (theValue instanceof Integer integer) { + value = toBytes(integer); + } else if (theValue instanceof String string) { + value = toBytes(string); } else { throw new IllegalArgumentException(String.format("Cannot convert %s to binary format", boundary.getValue())); } diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java index 74c094cee..9f4b037c1 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java @@ -45,8 +45,8 @@ public class JedisExceptionConverter implements Converter { @SuppressWarnings("unchecked") public Object convert(@Nullable Object result) { - if (result instanceof String) { + if (result instanceof String stringResult) { // evalsha converts byte[] to String. Convert back for consistency - return SafeEncoder.encode((String) result); + return SafeEncoder.encode(stringResult); } if (returnType == ReturnType.STATUS) { return JedisConverters.toString((byte[]) result); @@ -57,10 +57,10 @@ public class JedisScriptReturnConverter implements Converter { List resultList = (List) result; List convertedResults = new ArrayList<>(); for (Object res : resultList) { - if (res instanceof String) { + if (res instanceof String stringResult) { // evalsha converts byte[] to String. Convert back for // consistency - convertedResults.add(SafeEncoder.encode((String) res)); + convertedResults.add(SafeEncoder.encode(stringResult)); } else { convertedResults.add(res); } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java index 8c0fb00de..7a057d802 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java @@ -46,14 +46,14 @@ public class LettuceExceptionConverter implements Converter targetList = new ArrayList<>(); - for (Object it : (List) value) { + for (Object it : listValue) { targetList.add(preConvertNativeValues(it)); } return targetList; diff --git a/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java b/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java index 6b92a3977..2d7e18dc9 100644 --- a/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java +++ b/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java @@ -41,8 +41,8 @@ public class ByteArrayWrapper implements Comparable { } public boolean equals(@Nullable Object obj) { - if (obj instanceof ByteArrayWrapper) { - return Arrays.equals(array, ((ByteArrayWrapper) obj).array); + if (obj instanceof ByteArrayWrapper byteArrayWrapper) { + return Arrays.equals(array, byteArrayWrapper.array); } return false; diff --git a/src/main/java/org/springframework/data/redis/core/AbstractOperations.java b/src/main/java/org/springframework/data/redis/core/AbstractOperations.java index 5f2b77cde..24d445f51 100644 --- a/src/main/java/org/springframework/data/redis/core/AbstractOperations.java +++ b/src/main/java/org/springframework/data/redis/core/AbstractOperations.java @@ -106,8 +106,8 @@ abstract class AbstractOperations { Assert.notNull(key, "non null key required"); - if (keySerializer() == null && key instanceof byte[]) { - return (byte[]) key; + if (keySerializer() == null && key instanceof byte[] bytes) { + return bytes; } return keySerializer().serialize(key); @@ -121,8 +121,8 @@ abstract class AbstractOperations { @SuppressWarnings("unchecked") byte[] rawValue(Object value) { - if (valueSerializer() == null && value instanceof byte[]) { - return (byte[]) value; + if (valueSerializer() == null && value instanceof byte[] bytes) { + return bytes; } return valueSerializer().serialize(value); @@ -161,8 +161,8 @@ abstract class AbstractOperations { @SuppressWarnings("unchecked") byte[] rawHashKey(HK hashKey) { Assert.notNull(hashKey, "non null hash key required"); - if (hashKeySerializer() == null && hashKey instanceof byte[]) { - return (byte[]) hashKey; + if (hashKeySerializer() == null && hashKey instanceof byte[] bytes) { + return bytes; } return hashKeySerializer().serialize(hashKey); } @@ -180,8 +180,8 @@ abstract class AbstractOperations { @SuppressWarnings("unchecked") byte[] rawHashValue(HV value) { - if (hashValueSerializer() == null && value instanceof byte[]) { - return (byte[]) value; + if (hashValueSerializer() == null && value instanceof byte[] bytes) { + return bytes; } return hashValueSerializer().serialize(value); } @@ -268,8 +268,8 @@ abstract class AbstractOperations { Set rawTuples = new LinkedHashSet<>(values.size()); for (TypedTuple value : values) { byte[] rawValue; - if (valueSerializer() == null && value.getValue() instanceof byte[]) { - rawValue = (byte[]) value.getValue(); + if (valueSerializer() == null && value.getValue() instanceof byte[] bytes) { + rawValue = bytes; } else { rawValue = valueSerializer().serialize(value.getValue()); } diff --git a/src/main/java/org/springframework/data/redis/core/DefaultTypedTuple.java b/src/main/java/org/springframework/data/redis/core/DefaultTypedTuple.java index 4ed23420f..ea24a13b9 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultTypedTuple.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultTypedTuple.java @@ -64,9 +64,8 @@ public class DefaultTypedTuple implements TypedTuple { return true; if (obj == null) return false; - if (!(obj instanceof DefaultTypedTuple)) + if (!(obj instanceof DefaultTypedTuple other)) return false; - DefaultTypedTuple other = (DefaultTypedTuple) obj; if (score == null) { if (other.score != null) return false; @@ -75,11 +74,11 @@ public class DefaultTypedTuple implements TypedTuple { if (value == null) { if (other.value != null) return false; - } else if (value instanceof byte[]) { - if (!(other.value instanceof byte[])) { + } else if (value instanceof byte[] bytes) { + if (!(other.value instanceof byte[] otherBytes)) { return false; } - return Arrays.equals((byte[]) value, (byte[]) other.value); + return Arrays.equals(bytes, otherBytes); } else if (!value.equals(other.value)) return false; return true; diff --git a/src/main/java/org/springframework/data/redis/core/GeoOperationsEditor.java b/src/main/java/org/springframework/data/redis/core/GeoOperationsEditor.java index ce479796a..27b085eee 100644 --- a/src/main/java/org/springframework/data/redis/core/GeoOperationsEditor.java +++ b/src/main/java/org/springframework/data/redis/core/GeoOperationsEditor.java @@ -28,8 +28,8 @@ class GeoOperationsEditor extends PropertyEditorSupport { public void setValue(Object value) { - if (value instanceof RedisOperations) { - super.setValue(((RedisOperations) value).opsForGeo()); + if (value instanceof RedisOperations redisOperations) { + super.setValue(redisOperations.opsForGeo()); } else { throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class); } diff --git a/src/main/java/org/springframework/data/redis/core/HashOperationsEditor.java b/src/main/java/org/springframework/data/redis/core/HashOperationsEditor.java index 46326e4f5..17eaa7a4c 100644 --- a/src/main/java/org/springframework/data/redis/core/HashOperationsEditor.java +++ b/src/main/java/org/springframework/data/redis/core/HashOperationsEditor.java @@ -25,8 +25,8 @@ import java.beans.PropertyEditorSupport; class HashOperationsEditor extends PropertyEditorSupport { public void setValue(Object value) { - if (value instanceof RedisOperations) { - super.setValue(((RedisOperations) value).opsForHash()); + if (value instanceof RedisOperations redisOperations) { + super.setValue(redisOperations.opsForHash()); } else { throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class); } diff --git a/src/main/java/org/springframework/data/redis/core/IndexWriter.java b/src/main/java/org/springframework/data/redis/core/IndexWriter.java index f933112b6..67f02bad2 100644 --- a/src/main/java/org/springframework/data/redis/core/IndexWriter.java +++ b/src/main/java/org/springframework/data/redis/core/IndexWriter.java @@ -208,9 +208,9 @@ class IndexWriter { return; } - if (indexedData instanceof SimpleIndexedPropertyValue) { + if (indexedData instanceof SimpleIndexedPropertyValue simpleIndexedData) { - Object value = ((SimpleIndexedPropertyValue) indexedData).getValue(); + Object value = simpleIndexedData.getValue(); if (value == null) { return; @@ -222,9 +222,7 @@ class IndexWriter { // keep track of indexes used for the object connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey); - } else if (indexedData instanceof GeoIndexedPropertyValue) { - - GeoIndexedPropertyValue geoIndexedData = ((GeoIndexedPropertyValue) indexedData); + } else if (indexedData instanceof GeoIndexedPropertyValue geoIndexedData) { Object value = geoIndexedData.getValue(); if (value == null) { @@ -248,8 +246,8 @@ class IndexWriter { return new byte[] {}; } - if (source instanceof byte[]) { - return (byte[]) source; + if (source instanceof byte[] bytes) { + return bytes; } if (converter.getConversionService().canConvert(source.getClass(), byte[].class)) { diff --git a/src/main/java/org/springframework/data/redis/core/ListOperationsEditor.java b/src/main/java/org/springframework/data/redis/core/ListOperationsEditor.java index ecd483c47..f556f7c2d 100644 --- a/src/main/java/org/springframework/data/redis/core/ListOperationsEditor.java +++ b/src/main/java/org/springframework/data/redis/core/ListOperationsEditor.java @@ -25,8 +25,8 @@ import java.beans.PropertyEditorSupport; class ListOperationsEditor extends PropertyEditorSupport { public void setValue(Object value) { - if (value instanceof RedisOperations) { - super.setValue(((RedisOperations) value).opsForList()); + if (value instanceof RedisOperations redisOperations) { + super.setValue(redisOperations.opsForList()); } else { throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class); } diff --git a/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java b/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java index 2956c3d88..9e80e83b7 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java +++ b/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java @@ -305,8 +305,8 @@ public abstract class RedisConnectionUtils { RedisConnection connectionToUse = connection; - while (connectionToUse instanceof RedisConnectionProxy) { - connectionToUse = ((RedisConnectionProxy) connectionToUse).getTargetConnection(); + while (connectionToUse instanceof RedisConnectionProxy redisConnectionProxy) { + connectionToUse = redisConnectionProxy.getTargetConnection(); } return connectionToUse; diff --git a/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java b/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java index 34d309918..4fd7064b8 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java +++ b/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java @@ -571,8 +571,8 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter */ public byte[] toBytes(Object source) { - if (source instanceof byte[]) { - return (byte[]) source; + if (source instanceof byte[] bytes) { + return bytes; } return converter.getConversionService().convert(source, byte[].class); diff --git a/src/main/java/org/springframework/data/redis/core/RedisKeyValueTemplate.java b/src/main/java/org/springframework/data/redis/core/RedisKeyValueTemplate.java index bdd8acf81..acda71c7c 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisKeyValueTemplate.java +++ b/src/main/java/org/springframework/data/redis/core/RedisKeyValueTemplate.java @@ -152,8 +152,8 @@ public class RedisKeyValueTemplate extends KeyValueTemplate { @Override public T update(T objectToUpdate) { - if (objectToUpdate instanceof PartialUpdate) { - doPartialUpdate((PartialUpdate) objectToUpdate); + if (objectToUpdate instanceof PartialUpdate partialUpdate) { + doPartialUpdate(partialUpdate); return objectToUpdate; } diff --git a/src/main/java/org/springframework/data/redis/core/RedisTemplate.java b/src/main/java/org/springframework/data/redis/core/RedisTemplate.java index 246f87687..e0be526eb 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisTemplate.java +++ b/src/main/java/org/springframework/data/redis/core/RedisTemplate.java @@ -1056,8 +1056,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation @SuppressWarnings("unchecked") private byte[] rawKey(Object key) { Assert.notNull(key, "non null key required"); - if (keySerializer == null && key instanceof byte[]) { - return (byte[]) key; + if (keySerializer == null && key instanceof byte[] bytes) { + return bytes; } return keySerializer.serialize(key); } @@ -1068,8 +1068,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation @SuppressWarnings("unchecked") private byte[] rawValue(Object value) { - if (valueSerializer == null && value instanceof byte[]) { - return (byte[]) value; + if (valueSerializer == null && value instanceof byte[] bytes) { + return bytes; } return valueSerializer.serialize(value); } @@ -1103,16 +1103,15 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation List values = new ArrayList<>(); for (Object rawValue : rawValues) { - if (rawValue instanceof byte[] && valueSerializer != null) { - values.add(valueSerializer.deserialize((byte[]) rawValue)); - } else if (rawValue instanceof List) { + if (rawValue instanceof byte[] bytes && valueSerializer != null) { + values.add(valueSerializer.deserialize(bytes)); + } else if (rawValue instanceof List list) { // Lists are the only potential Collections of mixed values.... - values.add(deserializeMixedResults((List) rawValue, valueSerializer, hashKeySerializer, hashValueSerializer)); - } else if (rawValue instanceof Set && !(((Set) rawValue).isEmpty())) { - values.add(deserializeSet((Set) rawValue, valueSerializer)); - } else if (rawValue instanceof Map && !(((Map) rawValue).isEmpty()) - && ((Map) rawValue).values().iterator().next() instanceof byte[]) { - values.add(SerializationUtils.deserialize((Map) rawValue, hashKeySerializer, hashValueSerializer)); + values.add(deserializeMixedResults(list, valueSerializer, hashKeySerializer, hashValueSerializer)); + } else if (rawValue instanceof Set set && !(set.isEmpty())) { + values.add(deserializeSet(set, valueSerializer)); + } else if (rawValue instanceof Map map && !(map.isEmpty()) && map.values().iterator().next() instanceof byte[]) { + values.add(SerializationUtils.deserialize(map, hashKeySerializer, hashValueSerializer)); } else { values.add(rawValue); } diff --git a/src/main/java/org/springframework/data/redis/core/SetOperationsEditor.java b/src/main/java/org/springframework/data/redis/core/SetOperationsEditor.java index d17e9dd9c..ab087affa 100644 --- a/src/main/java/org/springframework/data/redis/core/SetOperationsEditor.java +++ b/src/main/java/org/springframework/data/redis/core/SetOperationsEditor.java @@ -25,8 +25,8 @@ import java.beans.PropertyEditorSupport; class SetOperationsEditor extends PropertyEditorSupport { public void setValue(Object value) { - if (value instanceof RedisOperations) { - super.setValue(((RedisOperations) value).opsForSet()); + if (value instanceof RedisOperations redisOperations) { + super.setValue(redisOperations.opsForSet()); } else { throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class); } diff --git a/src/main/java/org/springframework/data/redis/core/ValueOperationsEditor.java b/src/main/java/org/springframework/data/redis/core/ValueOperationsEditor.java index cc5f354f7..d10ff0801 100644 --- a/src/main/java/org/springframework/data/redis/core/ValueOperationsEditor.java +++ b/src/main/java/org/springframework/data/redis/core/ValueOperationsEditor.java @@ -25,8 +25,8 @@ import java.beans.PropertyEditorSupport; class ValueOperationsEditor extends PropertyEditorSupport { public void setValue(Object value) { - if (value instanceof RedisOperations) { - super.setValue(((RedisOperations) value).opsForValue()); + if (value instanceof RedisOperations redisOperations) { + super.setValue(redisOperations.opsForValue()); } else { throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class); } diff --git a/src/main/java/org/springframework/data/redis/core/ZSetOperationsEditor.java b/src/main/java/org/springframework/data/redis/core/ZSetOperationsEditor.java index af2226eaa..50e9f0b55 100644 --- a/src/main/java/org/springframework/data/redis/core/ZSetOperationsEditor.java +++ b/src/main/java/org/springframework/data/redis/core/ZSetOperationsEditor.java @@ -25,8 +25,8 @@ import java.beans.PropertyEditorSupport; class ZSetOperationsEditor extends PropertyEditorSupport { public void setValue(Object value) { - if (value instanceof RedisOperations) { - super.setValue(((RedisOperations) value).opsForZSet()); + if (value instanceof RedisOperations redisOperations) { + super.setValue(redisOperations.opsForZSet()); } else { throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class); } diff --git a/src/main/java/org/springframework/data/redis/core/convert/DefaultRedisTypeMapper.java b/src/main/java/org/springframework/data/redis/core/convert/DefaultRedisTypeMapper.java index e9aa234e3..69770adbc 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/DefaultRedisTypeMapper.java +++ b/src/main/java/org/springframework/data/redis/core/convert/DefaultRedisTypeMapper.java @@ -148,8 +148,8 @@ public class DefaultRedisTypeMapper extends DefaultTypeMapper expressionCache; + private @Nullable BeanResolver beanResolver; /** * Creates a new instance using a default {@link SpelExpressionParser}. @@ -95,9 +93,9 @@ public class SpelIndexResolver implements IndexResolver { for (IndexDefinition setting : settings.getIndexDefinitionsFor(keyspace)) { - if (setting instanceof SpelIndexDefinition) { + if (setting instanceof SpelIndexDefinition spelIndexDefinition) { - Expression expression = getAndCacheIfAbsent((SpelIndexDefinition) setting); + Expression expression = getAndCacheIfAbsent(spelIndexDefinition); StandardEvaluationContext context = new StandardEvaluationContext(); context.setRootObject(value); diff --git a/src/main/java/org/springframework/data/redis/core/index/RedisIndexDefinition.java b/src/main/java/org/springframework/data/redis/core/index/RedisIndexDefinition.java index f5357a137..0606f788c 100644 --- a/src/main/java/org/springframework/data/redis/core/index/RedisIndexDefinition.java +++ b/src/main/java/org/springframework/data/redis/core/index/RedisIndexDefinition.java @@ -140,11 +140,11 @@ public abstract class RedisIndexDefinition implements IndexDefinition { @Override public Object convert(Object source) { - if (!(source instanceof String)) { + if (!(source instanceof String string)) { return source; } - return ((String) source).toLowerCase(); + return string.toLowerCase(); } } diff --git a/src/main/java/org/springframework/data/redis/core/script/DefaultScriptExecutor.java b/src/main/java/org/springframework/data/redis/core/script/DefaultScriptExecutor.java index 23032af69..b38c91391 100644 --- a/src/main/java/org/springframework/data/redis/core/script/DefaultScriptExecutor.java +++ b/src/main/java/org/springframework/data/redis/core/script/DefaultScriptExecutor.java @@ -99,16 +99,16 @@ public class DefaultScriptExecutor implements ScriptExecutor { int i = 0; if (keys != null) { for (K key : keys) { - if (keySerializer() == null && key instanceof byte[]) { - keysAndArgs[i++] = (byte[]) key; + if (keySerializer() == null && key instanceof byte[] keyBytes) { + keysAndArgs[i++] = keyBytes; } else { keysAndArgs[i++] = keySerializer().serialize(key); } } } for (Object arg : args) { - if (argsSerializer == null && arg instanceof byte[]) { - keysAndArgs[i++] = (byte[]) arg; + if (argsSerializer == null && arg instanceof byte[] argBytes) { + keysAndArgs[i++] = argBytes; } else { keysAndArgs[i++] = argsSerializer.serialize(arg); } diff --git a/src/main/java/org/springframework/data/redis/core/script/ScriptUtils.java b/src/main/java/org/springframework/data/redis/core/script/ScriptUtils.java index 8e89cc919..3d112b7da 100644 --- a/src/main/java/org/springframework/data/redis/core/script/ScriptUtils.java +++ b/src/main/java/org/springframework/data/redis/core/script/ScriptUtils.java @@ -46,15 +46,15 @@ class ScriptUtils { @SuppressWarnings({ "unchecked" }) static T deserializeResult(RedisSerializer resultSerializer, Object result) { - if (result instanceof byte[]) { - return resultSerializer.deserialize((byte[]) result); + if (result instanceof byte[] resultBytes) { + return resultSerializer.deserialize(resultBytes); } - if (result instanceof List) { + if (result instanceof List listResult) { - List results = new ArrayList<>(((List) result).size()); + List results = new ArrayList<>(listResult.size()); - for (Object obj : (List) result) { + for (Object obj : listResult) { results.add(deserializeResult(resultSerializer, obj)); } @@ -76,15 +76,15 @@ class ScriptUtils { @SuppressWarnings({ "unchecked" }) static T deserializeResult(RedisElementReader reader, Object result) { - if (result instanceof ByteBuffer) { - return reader.read((ByteBuffer) result); + if (result instanceof ByteBuffer byteBuffer) { + return reader.read(byteBuffer); } - if (result instanceof List) { + if (result instanceof List listResult) { - List results = new ArrayList<>(((List) result).size()); + List results = new ArrayList<>(listResult.size()); - for (Object obj : (List) result) { + for (Object obj : listResult) { results.add(deserializeResult(reader, obj)); } diff --git a/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java b/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java index 5108adbec..cc083f14d 100644 --- a/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java +++ b/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java @@ -918,9 +918,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab try { - if (subscriptionExecutor instanceof ScheduledExecutorService) { - ((ScheduledExecutorService) subscriptionExecutor).schedule(retryRunnable, recoveryInterval, - TimeUnit.MILLISECONDS); + if (subscriptionExecutor instanceof ScheduledExecutorService scheduledExecutorService) { + scheduledExecutorService.schedule(retryRunnable, recoveryInterval, TimeUnit.MILLISECONDS); } else { Thread.sleep(recoveryInterval); retryRunnable.run(); @@ -957,8 +956,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab Executor executor = getRequiredTaskExecutor(); for (MessageListener messageListener : listeners) { - if (messageListener instanceof SubscriptionListener) { - executor.execute(() -> listenerConsumer.accept((SubscriptionListener) messageListener, source, count)); + if (messageListener instanceof SubscriptionListener subscriptionListener) { + executor.execute(() -> listenerConsumer.accept(subscriptionListener, source, count)); } } } diff --git a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java index 11a4b72fc..5baefb58d 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java +++ b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java @@ -144,8 +144,8 @@ public abstract class AbstractRedisCollection extends AbstractCollection i if (o == this) return true; - if (o instanceof RedisStore) { - return key.equals(((RedisStore) o).getKey()); + if (o instanceof RedisStore redisStore) { + return key.equals(redisStore.getKey()); } if (o instanceof AbstractRedisCollection) { diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java index 401ffd99d..e42fa818f 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java @@ -158,8 +158,8 @@ public class DefaultRedisList extends AbstractRedisCollection implements R @SuppressWarnings("unchecked") private void potentiallyCap(RedisList destination) { - if (destination instanceof DefaultRedisList) { - ((DefaultRedisList) destination).cap(); + if (destination instanceof DefaultRedisList defaultRedisList) { + defaultRedisList.cap(); } } @@ -619,8 +619,7 @@ public class DefaultRedisList extends AbstractRedisCollection implements R */ int lastReturnedElementIndex = -1; - @Nullable - E lastReturnedElement; + @Nullable E lastReturnedElement; @Override public boolean hasNext() { diff --git a/src/test/java/org/springframework/data/redis/Address.java b/src/test/java/org/springframework/data/redis/Address.java index 508534a31..1314843f6 100644 --- a/src/test/java/org/springframework/data/redis/Address.java +++ b/src/test/java/org/springframework/data/redis/Address.java @@ -91,18 +91,17 @@ public class Address implements Serializable { return true; if (obj == null) return false; - if (!(obj instanceof Address)) + if (!(obj instanceof Address that)) return false; - Address other = (Address) obj; if (number == null) { - if (other.number != null) + if (that.number != null) return false; - } else if (!number.equals(other.number)) + } else if (!number.equals(that.number)) return false; if (street == null) { - if (other.street != null) + if (that.street != null) return false; - } else if (!street.equals(other.street)) + } else if (!street.equals(that.street)) return false; return true; } diff --git a/src/test/java/org/springframework/data/redis/Person.java b/src/test/java/org/springframework/data/redis/Person.java index 3865c99ca..4e3b8d481 100644 --- a/src/test/java/org/springframework/data/redis/Person.java +++ b/src/test/java/org/springframework/data/redis/Person.java @@ -96,28 +96,27 @@ public class Person implements Serializable { return true; if (obj == null) return false; - if (!(obj instanceof Person)) + if (!(obj instanceof Person that)) return false; - Person other = (Person) obj; if (address == null) { - if (other.address != null) + if (that.address != null) return false; - } else if (!address.equals(other.address)) + } else if (!address.equals(that.address)) return false; if (age == null) { - if (other.age != null) + if (that.age != null) return false; - } else if (!age.equals(other.age)) + } else if (!age.equals(that.age)) return false; if (firstName == null) { - if (other.firstName != null) + if (that.firstName != null) return false; - } else if (!firstName.equals(other.firstName)) + } else if (!firstName.equals(that.firstName)) return false; if (lastName == null) { - if (other.lastName != null) + if (that.lastName != null) return false; - } else if (!lastName.equals(other.lastName)) + } else if (!lastName.equals(that.lastName)) return false; return true; }