Polishing.

Simplify code to use well-known Spring patterns.

Original pull request: #2752
See #2751
This commit is contained in:
Mark Paluch
2024-09-10 14:01:02 +02:00
parent 44aa79e2b2
commit 9c5f21b840
9 changed files with 58 additions and 167 deletions

View File

@@ -37,7 +37,6 @@ import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -82,8 +81,7 @@ public class RedisCache extends AbstractValueAdaptingCache {
*/
protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) {
super(RedisAssertions.requireNonNull(cacheConfiguration, "CacheConfiguration must not be null")
.getAllowCacheNullValues());
super(cacheConfiguration.getAllowCacheNullValues());
Assert.notNull(name, "Name must not be null");
Assert.notNull(cacheWriter, "CacheWriter must not be null");

View File

@@ -27,7 +27,6 @@ import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -103,10 +102,11 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
private RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
boolean allowRuntimeCacheCreation) {
this.defaultCacheConfiguration = RedisAssertions.requireNonNull(defaultCacheConfiguration,
"DefaultCacheConfiguration must not be null");
Assert.notNull(defaultCacheConfiguration, "DefaultCacheConfiguration must not be null");
Assert.notNull(cacheWriter, "CacheWriter must not be null");
this.cacheWriter = RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null");
this.defaultCacheConfiguration = defaultCacheConfiguration;
this.cacheWriter = cacheWriter;
this.initialCacheConfiguration = new LinkedHashMap<>();
this.allowRuntimeCacheCreation = allowRuntimeCacheCreation;
}
@@ -423,7 +423,10 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
* @see org.springframework.data.redis.cache.RedisCacheWriter
*/
public static RedisCacheManagerBuilder fromCacheWriter(RedisCacheWriter cacheWriter) {
return new RedisCacheManagerBuilder(RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null"));
Assert.notNull(cacheWriter, "CacheWriter must not be null");
return new RedisCacheManagerBuilder(cacheWriter);
}
/**
@@ -534,7 +537,10 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
* @since 2.3
*/
public RedisCacheManagerBuilder cacheWriter(RedisCacheWriter cacheWriter) {
this.cacheWriter = RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null");
Assert.notNull(cacheWriter, "CacheWriter must not be null");
this.cacheWriter = cacheWriter;
return this;
}
@@ -558,8 +564,10 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
*/
public RedisCacheManagerBuilder initialCacheNames(Set<String> cacheNames) {
RedisAssertions.requireNonNull(cacheNames, "CacheNames must not be null")
.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration));
Assert.notNull(cacheNames, "CacheNames must not be null");
Assert.noNullElements(cacheNames, "CacheNames must not be null");
cacheNames.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration));
return this;
}
@@ -603,9 +611,9 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
public RedisCacheManagerBuilder withInitialCacheConfigurations(
Map<String, RedisCacheConfiguration> cacheConfigurations) {
RedisAssertions.requireNonNull(cacheConfigurations, "CacheConfigurations must not be null")
.forEach((cacheName, cacheConfiguration) -> RedisAssertions.requireNonNull(cacheConfiguration,
"RedisCacheConfiguration for cache [%s] must not be null", cacheName));
Assert.notNull(cacheConfigurations, "CacheConfigurations must not be null!");
cacheConfigurations.forEach((cacheName, configuration) -> Assert.notNull(configuration,
String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName)));
this.initialCaches.putAll(cacheConfigurations);

View File

@@ -25,7 +25,6 @@ import java.util.Set;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.NumberUtils;
@@ -161,7 +160,10 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
* @param node must not be {@literal null}.
*/
public void addClusterNode(RedisNode node) {
this.clusterNodes.add(RedisAssertions.requireNonNull(node, "ClusterNode must not be null"));
Assert.notNull(node, "ClusterNode must not be null");
this.clusterNodes.add(node);
}
/**
@@ -211,7 +213,10 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
@Override
public void setPassword(RedisPassword password) {
this.password = RedisAssertions.requireNonNull(password, "RedisPassword must not be null");
Assert.notNull(password, "RedisPassword must not be null");
this.password = password;
}
@Override

View File

@@ -22,7 +22,6 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -76,7 +75,9 @@ public class RedisClusterNode extends RedisNode {
this(SlotRange.empty());
this.id = RedisAssertions.requireNonNull(id, "Id must not be null");
Assert.notNull(id, "Id must not be null");
this.id = id;
}
/**
@@ -86,8 +87,10 @@ public class RedisClusterNode extends RedisNode {
*/
public RedisClusterNode(SlotRange slotRange) {
Assert.notNull(slotRange, "SlotRange must not be null");
this.flags = Collections.emptySet();
this.slotRange = RedisAssertions.requireNonNull(slotRange,"SlotRange must not be null");
this.slotRange = slotRange;
}
/**
@@ -101,8 +104,10 @@ public class RedisClusterNode extends RedisNode {
super(host, port);
Assert.notNull(slotRange, "SlotRange must not be null");
this.flags = Collections.emptySet();
this.slotRange = RedisAssertions.requireNonNull(slotRange,"SlotRange must not be null");
this.slotRange = slotRange;
}
/**

View File

@@ -64,7 +64,6 @@ import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration;
import org.springframework.data.redis.connection.RedisConfiguration.WithDatabaseIndex;
import org.springframework.data.redis.connection.RedisConfiguration.WithPassword;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.data.util.Optionals;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -670,8 +669,11 @@ public class LettuceConnectionFactory implements RedisConnectionFactory, Reactiv
*/
public AbstractRedisClient getRequiredNativeClient() {
return RedisAssertions.requireState(getNativeClient(),
"Client not yet initialized; Did you forget to call initialize the bean");
AbstractRedisClient client = getNativeClient();
Assert.state(client != null, "Client not yet initialized; Did you forget to call initialize the bean");
return client;
}
@Nullable

View File

@@ -27,6 +27,7 @@ import java.util.List;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.Limit;
@@ -38,7 +39,6 @@ import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -745,8 +745,13 @@ class DefaultReactiveZSetOperations<K, V> implements ReactiveZSetOperations<K, V
private V readRequiredValue(ByteBuffer buffer) {
return RedisAssertions.requireNonNull(readValue(buffer),
() -> new InvalidDataAccessApiUsageException("Deserialized sorted set value is null"));
V value = readValue(buffer);
if (value == null) {
throw new InvalidDataAccessApiUsageException("Deserialized sorted set value is null");
}
return value;
}
private TypedTuple<V> readTypedTuple(Tuple raw) {

View File

@@ -20,8 +20,8 @@ import org.springframework.core.serializer.DefaultDeserializer;
import org.springframework.core.serializer.DefaultSerializer;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Java Serialization {@link RedisSerializer}.
@@ -77,8 +77,11 @@ public class JdkSerializationRedisSerializer implements RedisSerializer<Object>
public JdkSerializationRedisSerializer(Converter<Object, byte[]> serializer,
Converter<byte[], Object> deserializer) {
this.serializer = RedisAssertions.requireNonNull(serializer, "Serializer must not be null");
this.deserializer = RedisAssertions.requireNonNull(deserializer, "Deserializer must not be null");
Assert.notNull(serializer, "Serializer must not be null");
Assert.notNull(deserializer, "Deserializer must not be null");
this.serializer = serializer;
this.deserializer = deserializer;
}
@Override

View File

@@ -25,7 +25,9 @@ import org.springframework.util.Assert;
*
* @author John Blum
* @since 3.1.0
* @deprecated since 3.3, will be removed in a future revision in favor of Spring's {@link Assert} utility.
*/
@Deprecated(since = "3.3", forRemoval = true)
public abstract class RedisAssertions {
/**