32
32
import org .springframework .util .StringUtils ;
33
33
34
34
/**
35
- * Configuration class used to set up a {@link RedisConnection} with {@link RedisConnectionFactory} for connecting
36
- * to <a href="https://redis.io/topics/sentinel">Redis Sentinel(s)</a>. Useful when setting up a highly available Redis
37
- * environment.
35
+ * {@link RedisConfiguration Configuration} class used to set up a {@link RedisConnection}
36
+ * with {@link RedisConnectionFactory} for connecting to <a href="https://redis.io/topics/sentinel">Redis Sentinel(s)</a>.
37
+ * Useful when setting up a highly available Redis environment.
38
38
*
39
39
* @author Christoph Strobl
40
40
* @author Thomas Darimont
@@ -104,20 +104,23 @@ public RedisSentinelConfiguration(PropertySource<?> propertySource) {
104
104
this .sentinels = new LinkedHashSet <>();
105
105
106
106
if (propertySource .containsProperty (REDIS_SENTINEL_MASTER_CONFIG_PROPERTY )) {
107
- this .setMaster (propertySource .getProperty (REDIS_SENTINEL_MASTER_CONFIG_PROPERTY ).toString ());
107
+ String sentinelMaster = String .valueOf (propertySource .getProperty (REDIS_SENTINEL_MASTER_CONFIG_PROPERTY ));
108
+ this .setMaster (sentinelMaster );
108
109
}
109
110
110
111
if (propertySource .containsProperty (REDIS_SENTINEL_NODES_CONFIG_PROPERTY )) {
111
- appendSentinels (
112
- commaDelimitedListToSet (propertySource . getProperty ( REDIS_SENTINEL_NODES_CONFIG_PROPERTY ). toString () ));
112
+ String sentinelNodes = String . valueOf ( propertySource . getProperty ( REDIS_SENTINEL_NODES_CONFIG_PROPERTY ));
113
+ appendSentinels ( commaDelimitedListToSet (sentinelNodes ));
113
114
}
114
115
115
116
if (propertySource .containsProperty (REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY )) {
116
- this .setSentinelPassword (propertySource .getProperty (REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY ).toString ());
117
+ String sentinelPassword = String .valueOf (propertySource .getProperty (REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY ));
118
+ this .setSentinelPassword (sentinelPassword );
117
119
}
118
120
119
121
if (propertySource .containsProperty (REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY )) {
120
- this .setSentinelUsername (propertySource .getProperty (REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY ).toString ());
122
+ String sentinelUsername = String .valueOf (propertySource .getProperty (REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY ));
123
+ this .setSentinelUsername (sentinelUsername );
121
124
}
122
125
}
123
126
@@ -128,7 +131,7 @@ public RedisSentinelConfiguration(PropertySource<?> propertySource) {
128
131
*/
129
132
public void setSentinels (Iterable <RedisNode > sentinels ) {
130
133
131
- Assert .notNull (sentinels , "Cannot set sentinels to ' null' " );
134
+ Assert .notNull (sentinels , "Cannot set sentinels to null" );
132
135
133
136
this .sentinels .clear ();
134
137
@@ -148,16 +151,19 @@ public Set<RedisNode> getSentinels() {
148
151
*/
149
152
public void addSentinel (RedisNode sentinel ) {
150
153
151
- Assert .notNull (sentinel , "Sentinel must not be 'null'" );
154
+ Assert .notNull (sentinel , "Sentinel must not be null" );
155
+
152
156
this .sentinels .add (sentinel );
153
157
}
154
158
155
159
public void setMaster (NamedNode master ) {
156
160
157
- Assert .notNull (master , "Sentinel master node must not be 'null'" );
161
+ Assert .notNull (master , "Sentinel master node must not be null" );
162
+
158
163
this .master = master ;
159
164
}
160
165
166
+ @ Nullable
161
167
public NamedNode getMaster () {
162
168
return master ;
163
169
}
@@ -217,7 +223,7 @@ public int getDatabase() {
217
223
@ Override
218
224
public void setDatabase (int index ) {
219
225
220
- Assert .isTrue (index >= 0 , () -> String .format ("Invalid DB index '%s' (a positive index required) " , index ));
226
+ Assert .isTrue (index >= 0 , () -> String .format ("Invalid DB index '%d'; non-negative index required" , index ));
221
227
222
228
this .database = index ;
223
229
}
@@ -270,44 +276,37 @@ public RedisPassword getSentinelPassword() {
270
276
}
271
277
272
278
@ Override
273
- public boolean equals (@ Nullable Object o ) {
274
- if (this == o ) {
279
+ public boolean equals (@ Nullable Object obj ) {
280
+
281
+ if (this == obj ) {
275
282
return true ;
276
283
}
277
- if (!(o instanceof RedisSentinelConfiguration )) {
278
- return false ;
279
- }
280
- RedisSentinelConfiguration that = (RedisSentinelConfiguration ) o ;
281
- if (database != that .database ) {
282
- return false ;
283
- }
284
- if (!ObjectUtils .nullSafeEquals (master , that .master )) {
285
- return false ;
286
- }
287
- if (!ObjectUtils .nullSafeEquals (sentinels , that .sentinels )) {
288
- return false ;
289
- }
290
- if (!ObjectUtils .nullSafeEquals (dataNodeUsername , that .dataNodeUsername )) {
291
- return false ;
292
- }
293
- if (!ObjectUtils .nullSafeEquals (dataNodePassword , that .dataNodePassword )) {
294
- return false ;
295
- }
296
- if (!ObjectUtils .nullSafeEquals (sentinelUsername , that .sentinelUsername )) {
284
+
285
+ if (!(obj instanceof RedisSentinelConfiguration that )) {
297
286
return false ;
298
287
}
299
- return ObjectUtils .nullSafeEquals (sentinelPassword , that .sentinelPassword );
288
+
289
+ return this .database == that .database
290
+ && ObjectUtils .nullSafeEquals (this .master , that .master )
291
+ && ObjectUtils .nullSafeEquals (this .sentinels , that .sentinels )
292
+ && ObjectUtils .nullSafeEquals (this .dataNodeUsername , that .dataNodeUsername )
293
+ && ObjectUtils .nullSafeEquals (this .dataNodePassword , that .dataNodePassword )
294
+ && ObjectUtils .nullSafeEquals (this .sentinelUsername , that .sentinelUsername )
295
+ && ObjectUtils .nullSafeEquals (this .sentinelPassword , that .sentinelPassword );
300
296
}
301
297
302
298
@ Override
303
299
public int hashCode () {
300
+
304
301
int result = ObjectUtils .nullSafeHashCode (master );
302
+
305
303
result = 31 * result + ObjectUtils .nullSafeHashCode (sentinels );
306
304
result = 31 * result + database ;
307
305
result = 31 * result + ObjectUtils .nullSafeHashCode (dataNodeUsername );
308
306
result = 31 * result + ObjectUtils .nullSafeHashCode (dataNodePassword );
309
307
result = 31 * result + ObjectUtils .nullSafeHashCode (sentinelUsername );
310
308
result = 31 * result + ObjectUtils .nullSafeHashCode (sentinelPassword );
309
+
311
310
return result ;
312
311
}
313
312
@@ -323,6 +322,7 @@ private static Map<String, Object> asMap(String master, Set<String> sentinelHost
323
322
Assert .noNullElements (sentinelHostAndPorts , "ClusterHostAndPorts must not contain null elements" );
324
323
325
324
Map <String , Object > map = new HashMap <>();
325
+
326
326
map .put (REDIS_SENTINEL_MASTER_CONFIG_PROPERTY , master );
327
327
map .put (REDIS_SENTINEL_NODES_CONFIG_PROPERTY , StringUtils .collectionToCommaDelimitedString (sentinelHostAndPorts ));
328
328
0 commit comments