15
15
*/
16
16
package org .springframework .data .redis .connection ;
17
17
18
- import static org .springframework .util .StringUtils .* ;
18
+ import static org .springframework .util .StringUtils .commaDelimitedListToSet ;
19
19
20
20
import java .util .Collection ;
21
21
import java .util .Collections ;
27
27
import org .springframework .core .env .MapPropertySource ;
28
28
import org .springframework .core .env .PropertySource ;
29
29
import org .springframework .data .redis .connection .RedisConfiguration .ClusterConfiguration ;
30
+ import org .springframework .data .redis .util .RedisAssertions ;
30
31
import org .springframework .lang .Nullable ;
31
32
import org .springframework .util .Assert ;
32
33
import org .springframework .util .NumberUtils ;
@@ -47,11 +48,14 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
47
48
private static final String REDIS_CLUSTER_NODES_CONFIG_PROPERTY = "spring.redis.cluster.nodes" ;
48
49
private static final String REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY = "spring.redis.cluster.max-redirects" ;
49
50
50
- private Set <RedisNode > clusterNodes ;
51
51
private @ Nullable Integer maxRedirects ;
52
- private @ Nullable String username = null ;
52
+
53
53
private RedisPassword password = RedisPassword .none ();
54
54
55
+ private final Set <RedisNode > clusterNodes ;
56
+
57
+ private @ Nullable String username = null ;
58
+
55
59
/**
56
60
* Creates new {@link RedisClusterConfiguration}.
57
61
*/
@@ -94,12 +98,12 @@ public RedisClusterConfiguration(PropertySource<?> propertySource) {
94
98
this .clusterNodes = new LinkedHashSet <>();
95
99
96
100
if (propertySource .containsProperty (REDIS_CLUSTER_NODES_CONFIG_PROPERTY )) {
97
- appendClusterNodes (
98
- commaDelimitedListToSet (propertySource . getProperty ( REDIS_CLUSTER_NODES_CONFIG_PROPERTY ). toString ( )));
101
+ Object redisClusterNodes = propertySource . getProperty ( REDIS_CLUSTER_NODES_CONFIG_PROPERTY );
102
+ appendClusterNodes ( commaDelimitedListToSet (String . valueOf ( redisClusterNodes )));
99
103
}
100
104
if (propertySource .containsProperty (REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY )) {
101
- this . maxRedirects = NumberUtils . parseNumber (
102
- propertySource . getProperty ( REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY ). toString ( ), Integer .class );
105
+ Object clusterMaxRedirects = propertySource . getProperty ( REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY );
106
+ this . maxRedirects = NumberUtils . parseNumber ( String . valueOf ( clusterMaxRedirects ), Integer .class );
103
107
}
104
108
}
105
109
@@ -110,7 +114,7 @@ public RedisClusterConfiguration(PropertySource<?> propertySource) {
110
114
*/
111
115
public void setClusterNodes (Iterable <RedisNode > nodes ) {
112
116
113
- Assert .notNull (nodes , "Cannot set cluster nodes to ' null' " );
117
+ Assert .notNull (nodes , "Cannot set cluster nodes to null" );
114
118
115
119
this .clusterNodes .clear ();
116
120
@@ -130,9 +134,7 @@ public Set<RedisNode> getClusterNodes() {
130
134
* @param node must not be {@literal null}.
131
135
*/
132
136
public void addClusterNode (RedisNode node ) {
133
-
134
- Assert .notNull (node , "ClusterNode must not be 'null'" );
135
- this .clusterNodes .add (node );
137
+ this .clusterNodes .add (RedisAssertions .requireNonNull (node , "ClusterNode must not be null" ));
136
138
}
137
139
138
140
/**
@@ -141,6 +143,7 @@ public void addClusterNode(RedisNode node) {
141
143
public RedisClusterConfiguration clusterNode (RedisNode node ) {
142
144
143
145
this .clusterNodes .add (node );
146
+
144
147
return this ;
145
148
}
146
149
@@ -155,6 +158,7 @@ public Integer getMaxRedirects() {
155
158
public void setMaxRedirects (int maxRedirects ) {
156
159
157
160
Assert .isTrue (maxRedirects >= 0 , "MaxRedirects must be greater or equal to 0" );
161
+
158
162
this .maxRedirects = maxRedirects ;
159
163
}
160
164
@@ -192,31 +196,24 @@ public RedisPassword getPassword() {
192
196
193
197
@ Override
194
198
public void setPassword (RedisPassword password ) {
195
-
196
- Assert .notNull (password , "RedisPassword must not be null" );
197
-
198
- this .password = password ;
199
+ this .password = RedisAssertions .requireNonNull (password , "RedisPassword must not be null" );
199
200
}
200
201
201
202
@ Override
202
- public boolean equals (@ Nullable Object o ) {
203
- if (this == o ) {
203
+ public boolean equals (@ Nullable Object obj ) {
204
+
205
+ if (this == obj ) {
204
206
return true ;
205
207
}
206
- if (!(o instanceof RedisClusterConfiguration )) {
207
- return false ;
208
- }
209
- RedisClusterConfiguration that = (RedisClusterConfiguration ) o ;
210
- if (!ObjectUtils .nullSafeEquals (clusterNodes , that .clusterNodes )) {
211
- return false ;
212
- }
213
- if (!ObjectUtils .nullSafeEquals (maxRedirects , that .maxRedirects )) {
214
- return false ;
215
- }
216
- if (!ObjectUtils .nullSafeEquals (username , that .username )) {
208
+
209
+ if (!(obj instanceof RedisClusterConfiguration that )) {
217
210
return false ;
218
211
}
219
- return ObjectUtils .nullSafeEquals (password , that .password );
212
+
213
+ return ObjectUtils .nullSafeEquals (this .clusterNodes , that .clusterNodes )
214
+ && ObjectUtils .nullSafeEquals (this .maxRedirects , that .maxRedirects )
215
+ && ObjectUtils .nullSafeEquals (this .username , that .username )
216
+ && ObjectUtils .nullSafeEquals (this .password , that .password );
220
217
}
221
218
222
219
@ Override
@@ -239,6 +236,7 @@ private static Map<String, Object> asMap(Collection<String> clusterHostAndPorts,
239
236
Assert .noNullElements (clusterHostAndPorts , "ClusterHostAndPorts must not contain null elements" );
240
237
241
238
Map <String , Object > map = new HashMap <>();
239
+
242
240
map .put (REDIS_CLUSTER_NODES_CONFIG_PROPERTY , StringUtils .collectionToCommaDelimitedString (clusterHostAndPorts ));
243
241
244
242
if (redirects >= 0 ) {
@@ -247,5 +245,4 @@ private static Map<String, Object> asMap(Collection<String> clusterHostAndPorts,
247
245
248
246
return map ;
249
247
}
250
-
251
248
}
0 commit comments