Skip to content

Commit 9c5f21b

Browse files
committed
Polishing.
Simplify code to use well-known Spring patterns. Original pull request: #2752 See #2751
1 parent 44aa79e commit 9c5f21b

File tree

9 files changed

+58
-167
lines changed

9 files changed

+58
-167
lines changed

src/main/java/org/springframework/data/redis/cache/RedisCache.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.data.redis.serializer.RedisSerializationContext;
3838
import org.springframework.data.redis.serializer.RedisSerializer;
3939
import org.springframework.data.redis.util.ByteUtils;
40-
import org.springframework.data.redis.util.RedisAssertions;
4140
import org.springframework.lang.Nullable;
4241
import org.springframework.util.Assert;
4342
import org.springframework.util.ObjectUtils;
@@ -82,8 +81,7 @@ public class RedisCache extends AbstractValueAdaptingCache {
8281
*/
8382
protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) {
8483

85-
super(RedisAssertions.requireNonNull(cacheConfiguration, "CacheConfiguration must not be null")
86-
.getAllowCacheNullValues());
84+
super(cacheConfiguration.getAllowCacheNullValues());
8785

8886
Assert.notNull(name, "Name must not be null");
8987
Assert.notNull(cacheWriter, "CacheWriter must not be null");

src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.cache.CacheManager;
2828
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
2929
import org.springframework.data.redis.connection.RedisConnectionFactory;
30-
import org.springframework.data.redis.util.RedisAssertions;
3130
import org.springframework.lang.Nullable;
3231
import org.springframework.util.Assert;
3332

@@ -103,10 +102,11 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d
103102
private RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
104103
boolean allowRuntimeCacheCreation) {
105104

106-
this.defaultCacheConfiguration = RedisAssertions.requireNonNull(defaultCacheConfiguration,
107-
"DefaultCacheConfiguration must not be null");
105+
Assert.notNull(defaultCacheConfiguration, "DefaultCacheConfiguration must not be null");
106+
Assert.notNull(cacheWriter, "CacheWriter must not be null");
108107

109-
this.cacheWriter = RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null");
108+
this.defaultCacheConfiguration = defaultCacheConfiguration;
109+
this.cacheWriter = cacheWriter;
110110
this.initialCacheConfiguration = new LinkedHashMap<>();
111111
this.allowRuntimeCacheCreation = allowRuntimeCacheCreation;
112112
}
@@ -423,7 +423,10 @@ public static class RedisCacheManagerBuilder {
423423
* @see org.springframework.data.redis.cache.RedisCacheWriter
424424
*/
425425
public static RedisCacheManagerBuilder fromCacheWriter(RedisCacheWriter cacheWriter) {
426-
return new RedisCacheManagerBuilder(RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null"));
426+
427+
Assert.notNull(cacheWriter, "CacheWriter must not be null");
428+
429+
return new RedisCacheManagerBuilder(cacheWriter);
427430
}
428431

429432
/**
@@ -534,7 +537,10 @@ public RedisCacheManagerBuilder cacheDefaults(RedisCacheConfiguration defaultCac
534537
* @since 2.3
535538
*/
536539
public RedisCacheManagerBuilder cacheWriter(RedisCacheWriter cacheWriter) {
537-
this.cacheWriter = RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null");
540+
541+
Assert.notNull(cacheWriter, "CacheWriter must not be null");
542+
543+
this.cacheWriter = cacheWriter;
538544
return this;
539545
}
540546

@@ -558,8 +564,10 @@ public RedisCacheManagerBuilder enableStatistics() {
558564
*/
559565
public RedisCacheManagerBuilder initialCacheNames(Set<String> cacheNames) {
560566

561-
RedisAssertions.requireNonNull(cacheNames, "CacheNames must not be null")
562-
.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration));
567+
Assert.notNull(cacheNames, "CacheNames must not be null");
568+
Assert.noNullElements(cacheNames, "CacheNames must not be null");
569+
570+
cacheNames.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration));
563571

564572
return this;
565573
}
@@ -603,9 +611,9 @@ public RedisCacheManagerBuilder withCacheConfiguration(String cacheName,
603611
public RedisCacheManagerBuilder withInitialCacheConfigurations(
604612
Map<String, RedisCacheConfiguration> cacheConfigurations) {
605613

606-
RedisAssertions.requireNonNull(cacheConfigurations, "CacheConfigurations must not be null")
607-
.forEach((cacheName, cacheConfiguration) -> RedisAssertions.requireNonNull(cacheConfiguration,
608-
"RedisCacheConfiguration for cache [%s] must not be null", cacheName));
614+
Assert.notNull(cacheConfigurations, "CacheConfigurations must not be null!");
615+
cacheConfigurations.forEach((cacheName, configuration) -> Assert.notNull(configuration,
616+
String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName)));
609617

610618
this.initialCaches.putAll(cacheConfigurations);
611619

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.core.env.MapPropertySource;
2626
import org.springframework.core.env.PropertySource;
2727
import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration;
28-
import org.springframework.data.redis.util.RedisAssertions;
2928
import org.springframework.lang.Nullable;
3029
import org.springframework.util.Assert;
3130
import org.springframework.util.NumberUtils;
@@ -161,7 +160,10 @@ public Set<RedisNode> getClusterNodes() {
161160
* @param node must not be {@literal null}.
162161
*/
163162
public void addClusterNode(RedisNode node) {
164-
this.clusterNodes.add(RedisAssertions.requireNonNull(node, "ClusterNode must not be null"));
163+
164+
Assert.notNull(node, "ClusterNode must not be null");
165+
166+
this.clusterNodes.add(node);
165167
}
166168

167169
/**
@@ -211,7 +213,10 @@ public String getUsername() {
211213

212214
@Override
213215
public void setPassword(RedisPassword password) {
214-
this.password = RedisAssertions.requireNonNull(password, "RedisPassword must not be null");
216+
217+
Assert.notNull(password, "RedisPassword must not be null");
218+
219+
this.password = password;
215220
}
216221

217222
@Override

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.LinkedHashSet;
2323
import java.util.Set;
2424

25-
import org.springframework.data.redis.util.RedisAssertions;
2625
import org.springframework.lang.Nullable;
2726
import org.springframework.util.Assert;
2827
import org.springframework.util.CollectionUtils;
@@ -76,7 +75,9 @@ public RedisClusterNode(String id) {
7675

7776
this(SlotRange.empty());
7877

79-
this.id = RedisAssertions.requireNonNull(id, "Id must not be null");
78+
Assert.notNull(id, "Id must not be null");
79+
80+
this.id = id;
8081
}
8182

8283
/**
@@ -86,8 +87,10 @@ public RedisClusterNode(String id) {
8687
*/
8788
public RedisClusterNode(SlotRange slotRange) {
8889

90+
Assert.notNull(slotRange, "SlotRange must not be null");
91+
8992
this.flags = Collections.emptySet();
90-
this.slotRange = RedisAssertions.requireNonNull(slotRange,"SlotRange must not be null");
93+
this.slotRange = slotRange;
9194
}
9295

9396
/**
@@ -101,8 +104,10 @@ public RedisClusterNode(String host, int port, SlotRange slotRange) {
101104

102105
super(host, port);
103106

107+
Assert.notNull(slotRange, "SlotRange must not be null");
108+
104109
this.flags = Collections.emptySet();
105-
this.slotRange = RedisAssertions.requireNonNull(slotRange,"SlotRange must not be null");
110+
this.slotRange = slotRange;
106111
}
107112

108113
/**

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration;
6565
import org.springframework.data.redis.connection.RedisConfiguration.WithDatabaseIndex;
6666
import org.springframework.data.redis.connection.RedisConfiguration.WithPassword;
67-
import org.springframework.data.redis.util.RedisAssertions;
6867
import org.springframework.data.util.Optionals;
6968
import org.springframework.lang.Nullable;
7069
import org.springframework.util.Assert;
@@ -670,8 +669,11 @@ public AbstractRedisClient getNativeClient() {
670669
*/
671670
public AbstractRedisClient getRequiredNativeClient() {
672671

673-
return RedisAssertions.requireState(getNativeClient(),
674-
"Client not yet initialized; Did you forget to call initialize the bean");
672+
AbstractRedisClient client = getNativeClient();
673+
674+
Assert.state(client != null, "Client not yet initialized; Did you forget to call initialize the bean");
675+
676+
return client;
675677
}
676678

677679
@Nullable

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.function.Function;
2828

2929
import org.reactivestreams.Publisher;
30+
3031
import org.springframework.dao.InvalidDataAccessApiUsageException;
3132
import org.springframework.data.domain.Range;
3233
import org.springframework.data.redis.connection.Limit;
@@ -38,7 +39,6 @@
3839
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
3940
import org.springframework.data.redis.serializer.RedisSerializationContext;
4041
import org.springframework.data.redis.util.ByteUtils;
41-
import org.springframework.data.redis.util.RedisAssertions;
4242
import org.springframework.lang.Nullable;
4343
import org.springframework.util.Assert;
4444

@@ -745,8 +745,13 @@ private V readValue(ByteBuffer buffer) {
745745

746746
private V readRequiredValue(ByteBuffer buffer) {
747747

748-
return RedisAssertions.requireNonNull(readValue(buffer),
749-
() -> new InvalidDataAccessApiUsageException("Deserialized sorted set value is null"));
748+
V value = readValue(buffer);
749+
750+
if (value == null) {
751+
throw new InvalidDataAccessApiUsageException("Deserialized sorted set value is null");
752+
}
753+
754+
return value;
750755
}
751756

752757
private TypedTuple<V> readTypedTuple(Tuple raw) {

src/main/java/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.springframework.core.serializer.DefaultSerializer;
2121
import org.springframework.core.serializer.support.DeserializingConverter;
2222
import org.springframework.core.serializer.support.SerializingConverter;
23-
import org.springframework.data.redis.util.RedisAssertions;
2423
import org.springframework.lang.Nullable;
24+
import org.springframework.util.Assert;
2525

2626
/**
2727
* Java Serialization {@link RedisSerializer}.
@@ -77,8 +77,11 @@ public JdkSerializationRedisSerializer(@Nullable ClassLoader classLoader) {
7777
public JdkSerializationRedisSerializer(Converter<Object, byte[]> serializer,
7878
Converter<byte[], Object> deserializer) {
7979

80-
this.serializer = RedisAssertions.requireNonNull(serializer, "Serializer must not be null");
81-
this.deserializer = RedisAssertions.requireNonNull(deserializer, "Deserializer must not be null");
80+
Assert.notNull(serializer, "Serializer must not be null");
81+
Assert.notNull(deserializer, "Deserializer must not be null");
82+
83+
this.serializer = serializer;
84+
this.deserializer = deserializer;
8285
}
8386

8487
@Override

src/main/java/org/springframework/data/redis/util/RedisAssertions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
*
2626
* @author John Blum
2727
* @since 3.1.0
28+
* @deprecated since 3.3, will be removed in a future revision in favor of Spring's {@link Assert} utility.
2829
*/
30+
@Deprecated(since = "3.3", forRemoval = true)
2931
public abstract class RedisAssertions {
3032

3133
/**

src/test/java/org/springframework/data/redis/util/RedisAssertionsUnitTests.java

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)