Skip to content

Commit 3286d57

Browse files
committed
Polishing.
Reorder methods. Remove unnecessary null guards. Introduce hashCode/equals methods to RedisConfiguration implementations. Refactor tests. Let LettuceClientConfiguration.builder apply settings from RedisURI. Introduce SentinelMasterId to implement equals method. See #2116. Original pull request: #2117.
1 parent 19e9e74 commit 3286d57

14 files changed

+542
-244
lines changed

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

+39
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.lang.Nullable;
3232
import org.springframework.util.Assert;
3333
import org.springframework.util.NumberUtils;
34+
import org.springframework.util.ObjectUtils;
3435
import org.springframework.util.StringUtils;
3536

3637
/**
@@ -223,6 +224,44 @@ public void setPassword(RedisPassword password) {
223224
this.password = password;
224225
}
225226

227+
/*
228+
* (non-Javadoc)
229+
* @see java.lang.Object#equals(java.lang.Object)
230+
*/
231+
@Override
232+
public boolean equals(Object o) {
233+
if (this == o) {
234+
return true;
235+
}
236+
if (!(o instanceof RedisClusterConfiguration)) {
237+
return false;
238+
}
239+
RedisClusterConfiguration that = (RedisClusterConfiguration) o;
240+
if (!ObjectUtils.nullSafeEquals(clusterNodes, that.clusterNodes)) {
241+
return false;
242+
}
243+
if (!ObjectUtils.nullSafeEquals(maxRedirects, that.maxRedirects)) {
244+
return false;
245+
}
246+
if (!ObjectUtils.nullSafeEquals(username, that.username)) {
247+
return false;
248+
}
249+
return ObjectUtils.nullSafeEquals(password, that.password);
250+
}
251+
252+
/*
253+
* (non-Javadoc)
254+
* @see java.lang.Object#hashCode()
255+
*/
256+
@Override
257+
public int hashCode() {
258+
int result = ObjectUtils.nullSafeHashCode(clusterNodes);
259+
result = 31 * result + ObjectUtils.nullSafeHashCode(maxRedirects);
260+
result = 31 * result + ObjectUtils.nullSafeHashCode(username);
261+
result = 31 * result + ObjectUtils.nullSafeHashCode(password);
262+
return result;
263+
}
264+
226265
private RedisNode readHostAndPortFromString(String hostAndPort) {
227266

228267
String[] args = split(hostAndPort, ":");

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ interface SentinelConfiguration extends WithDatabaseIndex, WithPassword {
349349
*
350350
* @param name must not be {@literal null}.
351351
*/
352-
default void setMaster(final String name) {
352+
default void setMaster(String name) {
353353

354354
Assert.notNull(name, "Name of sentinel master must not be null.");
355355

356-
setMaster(() -> name);
356+
setMaster(new SentinelMasterId(name));
357357
}
358358

359359
/**

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

+47
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.redis.connection.RedisConfiguration.SentinelConfiguration;
2929
import org.springframework.lang.Nullable;
3030
import org.springframework.util.Assert;
31+
import org.springframework.util.ObjectUtils;
3132
import org.springframework.util.StringUtils;
3233

3334
/**
@@ -289,6 +290,52 @@ public RedisPassword getSentinelPassword() {
289290
return sentinelPassword;
290291
}
291292

293+
/*
294+
* (non-Javadoc)
295+
* @see java.lang.Object#equals(java.lang.Object)
296+
*/
297+
@Override
298+
public boolean equals(Object o) {
299+
if (this == o) {
300+
return true;
301+
}
302+
if (!(o instanceof RedisSentinelConfiguration)) {
303+
return false;
304+
}
305+
RedisSentinelConfiguration that = (RedisSentinelConfiguration) o;
306+
if (database != that.database) {
307+
return false;
308+
}
309+
if (!ObjectUtils.nullSafeEquals(master, that.master)) {
310+
return false;
311+
}
312+
if (!ObjectUtils.nullSafeEquals(sentinels, that.sentinels)) {
313+
return false;
314+
}
315+
if (!ObjectUtils.nullSafeEquals(dataNodeUsername, that.dataNodeUsername)) {
316+
return false;
317+
}
318+
if (!ObjectUtils.nullSafeEquals(dataNodePassword, that.dataNodePassword)) {
319+
return false;
320+
}
321+
return ObjectUtils.nullSafeEquals(sentinelPassword, that.sentinelPassword);
322+
}
323+
324+
/*
325+
* (non-Javadoc)
326+
* @see java.lang.Object#hashCode()
327+
*/
328+
@Override
329+
public int hashCode() {
330+
int result = ObjectUtils.nullSafeHashCode(master);
331+
result = 31 * result + ObjectUtils.nullSafeHashCode(sentinels);
332+
result = 31 * result + database;
333+
result = 31 * result + ObjectUtils.nullSafeHashCode(dataNodeUsername);
334+
result = 31 * result + ObjectUtils.nullSafeHashCode(dataNodePassword);
335+
result = 31 * result + ObjectUtils.nullSafeHashCode(sentinelPassword);
336+
return result;
337+
}
338+
292339
private RedisNode readHostAndPortFromString(String hostAndPort) {
293340

294341
String[] args = split(hostAndPort, ":");

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

+39
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.data.redis.connection.RedisConfiguration.DomainSocketConfiguration;
1919
import org.springframework.lang.Nullable;
2020
import org.springframework.util.Assert;
21+
import org.springframework.util.ObjectUtils;
2122

2223
/**
2324
* Configuration class used for setting up {@link RedisConnection} via {@link RedisConnectionFactory} connecting to
@@ -133,4 +134,42 @@ public void setPassword(RedisPassword password) {
133134

134135
this.password = password;
135136
}
137+
138+
/*
139+
* (non-Javadoc)
140+
* @see java.lang.Object#equals(java.lang.Object)
141+
*/
142+
@Override
143+
public boolean equals(Object o) {
144+
if (this == o) {
145+
return true;
146+
}
147+
if (!(o instanceof RedisSocketConfiguration)) {
148+
return false;
149+
}
150+
RedisSocketConfiguration that = (RedisSocketConfiguration) o;
151+
if (database != that.database) {
152+
return false;
153+
}
154+
if (!ObjectUtils.nullSafeEquals(socket, that.socket)) {
155+
return false;
156+
}
157+
if (!ObjectUtils.nullSafeEquals(username, that.username)) {
158+
return false;
159+
}
160+
return ObjectUtils.nullSafeEquals(password, that.password);
161+
}
162+
163+
/*
164+
* (non-Javadoc)
165+
* @see java.lang.Object#hashCode()
166+
*/
167+
@Override
168+
public int hashCode() {
169+
int result = ObjectUtils.nullSafeHashCode(socket);
170+
result = 31 * result + database;
171+
result = 31 * result + ObjectUtils.nullSafeHashCode(username);
172+
result = 31 * result + ObjectUtils.nullSafeHashCode(password);
173+
return result;
174+
}
136175
}

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

+43
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.data.redis.connection.RedisConfiguration.WithPassword;
2121
import org.springframework.lang.Nullable;
2222
import org.springframework.util.Assert;
23+
import org.springframework.util.ObjectUtils;
2324

2425
/**
2526
* Configuration class used for setting up {@link RedisConnection} via {@link RedisConnectionFactory} using connecting
@@ -166,4 +167,46 @@ public void setPassword(RedisPassword password) {
166167

167168
this.password = password;
168169
}
170+
171+
/*
172+
* (non-Javadoc)
173+
* @see java.lang.Object#equals(java.lang.Object)
174+
*/
175+
@Override
176+
public boolean equals(Object o) {
177+
if (this == o) {
178+
return true;
179+
}
180+
if (!(o instanceof RedisStandaloneConfiguration)) {
181+
return false;
182+
}
183+
RedisStandaloneConfiguration that = (RedisStandaloneConfiguration) o;
184+
if (port != that.port) {
185+
return false;
186+
}
187+
if (database != that.database) {
188+
return false;
189+
}
190+
if (!ObjectUtils.nullSafeEquals(hostName, that.hostName)) {
191+
return false;
192+
}
193+
if (!ObjectUtils.nullSafeEquals(username, that.username)) {
194+
return false;
195+
}
196+
return ObjectUtils.nullSafeEquals(password, that.password);
197+
}
198+
199+
/*
200+
* (non-Javadoc)
201+
* @see java.lang.Object#hashCode()
202+
*/
203+
@Override
204+
public int hashCode() {
205+
int result = ObjectUtils.nullSafeHashCode(hostName);
206+
result = 31 * result + port;
207+
result = 31 * result + database;
208+
result = 31 * result + ObjectUtils.nullSafeHashCode(username);
209+
result = 31 * result + ObjectUtils.nullSafeHashCode(password);
210+
return result;
211+
}
169212
}

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

+39
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.redis.connection.RedisConfiguration.StaticMasterReplicaConfiguration;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.Assert;
25+
import org.springframework.util.ObjectUtils;
2526

2627
/**
2728
* Configuration class used for setting up {@link RedisConnection} via {@link RedisConnectionFactory} using the provided
@@ -181,4 +182,42 @@ public void setPassword(RedisPassword password) {
181182
public List<RedisStandaloneConfiguration> getNodes() {
182183
return Collections.unmodifiableList(nodes);
183184
}
185+
186+
/*
187+
* (non-Javadoc)
188+
* @see java.lang.Object#equals(java.lang.Object)
189+
*/
190+
@Override
191+
public boolean equals(Object o) {
192+
if (this == o) {
193+
return true;
194+
}
195+
if (!(o instanceof RedisStaticMasterReplicaConfiguration)) {
196+
return false;
197+
}
198+
RedisStaticMasterReplicaConfiguration that = (RedisStaticMasterReplicaConfiguration) o;
199+
if (database != that.database) {
200+
return false;
201+
}
202+
if (!ObjectUtils.nullSafeEquals(nodes, that.nodes)) {
203+
return false;
204+
}
205+
if (!ObjectUtils.nullSafeEquals(username, that.username)) {
206+
return false;
207+
}
208+
return ObjectUtils.nullSafeEquals(password, that.password);
209+
}
210+
211+
/*
212+
* (non-Javadoc)
213+
* @see java.lang.Object#hashCode()
214+
*/
215+
@Override
216+
public int hashCode() {
217+
int result = ObjectUtils.nullSafeHashCode(nodes);
218+
result = 31 * result + database;
219+
result = 31 * result + ObjectUtils.nullSafeHashCode(username);
220+
result = 31 * result + ObjectUtils.nullSafeHashCode(password);
221+
return result;
222+
}
184223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.connection;
17+
18+
import org.springframework.lang.NonNull;
19+
import org.springframework.util.Assert;
20+
import org.springframework.util.ObjectUtils;
21+
22+
/**
23+
* Simple {@link NamedNode}.
24+
*
25+
* @author Mark Paluch
26+
* @since 2.5.3
27+
*/
28+
class SentinelMasterId implements NamedNode {
29+
30+
private final String name;
31+
32+
public SentinelMasterId(String name) {
33+
Assert.hasText(name, "Sentinel Master Id must not be null or empty");
34+
this.name = name;
35+
}
36+
37+
/*
38+
* (non-Javadoc)
39+
* @see org.springframework.data.redis.connection.NamedNode#getName()
40+
*/
41+
@NonNull
42+
@Override
43+
public String getName() {
44+
return name;
45+
}
46+
47+
/*
48+
* (non-Javadoc)
49+
* @see java.lang.Object#toString()
50+
*/
51+
@Override
52+
public String toString() {
53+
return getName();
54+
}
55+
56+
/*
57+
* (non-Javadoc)
58+
* @see java.lang.Object#equals(java.lang.Object)
59+
*/
60+
@Override
61+
public boolean equals(Object o) {
62+
if (this == o) {
63+
return true;
64+
}
65+
if (!(o instanceof SentinelMasterId)) {
66+
return false;
67+
}
68+
SentinelMasterId that = (SentinelMasterId) o;
69+
return ObjectUtils.nullSafeEquals(name, that.name);
70+
}
71+
72+
/*
73+
* (non-Javadoc)
74+
* @see java.lang.Object#hashCode()
75+
*/
76+
@Override
77+
public int hashCode() {
78+
return ObjectUtils.nullSafeHashCode(name);
79+
}
80+
}

0 commit comments

Comments
 (0)