Skip to content

Commit 5f9cb22

Browse files
DATAREDIS-574 - Polishing.
Updated reference Documentation, JavaDoc and add some minor code changes (rename methods, fix return types, diamond operators,…). Original Pull Request: spring-projects#236
1 parent 3819ded commit 5f9cb22

18 files changed

+294
-315
lines changed

src/main/asciidoc/reference/redis-repositories.adoc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,7 @@ class RedisOperationsProducer {
678678
@Produces
679679
RedisConnectionFactory redisConnectionFactory() {
680680
681-
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
682-
jedisConnectionFactory.setHostName("localhost");
683-
jedisConnectionFactory.setPort(6379);
681+
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(new RedisStandaloneConfiguration());
684682
jedisConnectionFactory.afterPropertiesSet();
685683
686684
return jedisConnectionFactory;

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@ public class RedisClusterConfiguration {
5353
* Creates new {@link RedisClusterConfiguration}.
5454
*/
5555
public RedisClusterConfiguration() {
56-
this(new MapPropertySource("RedisClusterConfiguration", Collections.<String, Object> emptyMap()));
56+
this(new MapPropertySource("RedisClusterConfiguration", Collections.emptyMap()));
5757
}
5858

5959
/**
@@ -64,12 +64,13 @@ public RedisClusterConfiguration() {
6464
* clusterHostAndPorts[0] = 127.0.0.1:23679
6565
* clusterHostAndPorts[1] = 127.0.0.1:23680 ...
6666
* </code>
67+
*
6768
* <pre>
6869
*
6970
* @param clusterNodes must not be {@literal null}.
7071
*/
7172
public RedisClusterConfiguration(Collection<String> clusterNodes) {
72-
this(new MapPropertySource("RedisClusterConfiguration", asMap(clusterNodes, -1, -1, null)));
73+
this(new MapPropertySource("RedisClusterConfiguration", asMap(clusterNodes, -1)));
7374
}
7475

7576
/**
@@ -90,11 +91,11 @@ public RedisClusterConfiguration(PropertySource<?> propertySource) {
9091

9192
notNull(propertySource, "PropertySource must not be null!");
9293

93-
this.clusterNodes = new LinkedHashSet<RedisNode>();
94+
this.clusterNodes = new LinkedHashSet<>();
9495

9596
if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) {
96-
appendClusterNodes(commaDelimitedListToSet(propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)
97-
.toString()));
97+
appendClusterNodes(
98+
commaDelimitedListToSet(propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY).toString()));
9899
}
99100
if (propertySource.containsProperty(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY)) {
100101
this.maxRedirects = NumberUtils.parseNumber(
@@ -139,7 +140,7 @@ public void addClusterNode(RedisNode node) {
139140
}
140141

141142
/**
142-
* @return
143+
* @return this.
143144
*/
144145
public RedisClusterConfiguration clusterNode(RedisNode node) {
145146

@@ -148,14 +149,14 @@ public RedisClusterConfiguration clusterNode(RedisNode node) {
148149
}
149150

150151
/**
151-
* @return
152+
* @return max number of redirects to follow or {@literal null} if not set.
152153
*/
153154
public Integer getMaxRedirects() {
154155
return maxRedirects != null && maxRedirects > Integer.MIN_VALUE ? maxRedirects : null;
155156
}
156157

157158
/**
158-
* @param maxRedirects
159+
* @param maxRedirects the max number of redirects to follow.
159160
*/
160161
public void setMaxRedirects(int maxRedirects) {
161162

@@ -164,9 +165,9 @@ public void setMaxRedirects(int maxRedirects) {
164165
}
165166

166167
/**
167-
* @param host
168-
* @param port
169-
* @return
168+
* @param host Redis cluster node host name or ip address.
169+
* @param port Redis cluster node port.
170+
* @return this.
170171
*/
171172
public RedisClusterConfiguration clusterNode(String host, Integer port) {
172173
return clusterNode(new RedisNode(host, port));
@@ -180,7 +181,9 @@ private void appendClusterNodes(Set<String> hostAndPorts) {
180181
}
181182

182183
/**
183-
* @return
184+
* Get the {@link RedisPassword} defined.
185+
*
186+
* @return never {@literal null}.
184187
* @since 2.0
185188
*/
186189
public RedisPassword getPassword() {
@@ -204,26 +207,24 @@ private RedisNode readHostAndPortFromString(String hostAndPort) {
204207

205208
notNull(args, "HostAndPort need to be seperated by ':'.");
206209
isTrue(args.length == 2, "Host and Port String needs to specified as host:port");
207-
return new RedisNode(args[0], Integer.valueOf(args[1]).intValue());
210+
return new RedisNode(args[0], Integer.valueOf(args[1]));
208211
}
209212

210213
/**
211214
* @param clusterHostAndPorts must not be {@literal null} or empty.
212-
* @param timeout
213-
* @param redirects
215+
* @param redirects the max number of redirects to follow.
214216
* @param password can be {@literal null} or empty.
215-
* @return
217+
* @return cluster config map with properties.
216218
*/
217-
private static Map<String, Object> asMap(Collection<String> clusterHostAndPorts, long timeout, int redirects,
218-
String password) {
219+
private static Map<String, Object> asMap(Collection<String> clusterHostAndPorts, int redirects) {
219220

220221
notNull(clusterHostAndPorts, "ClusterHostAndPorts must not be null!");
221222

222-
Map<String, Object> map = new HashMap<String, Object>();
223+
Map<String, Object> map = new HashMap<>();
223224
map.put(REDIS_CLUSTER_NODES_CONFIG_PROPERTY, StringUtils.collectionToCommaDelimitedString(clusterHostAndPorts));
224225

225226
if (redirects >= 0) {
226-
map.put(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY, Integer.valueOf(redirects));
227+
map.put(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY, redirects);
227228
}
228229

229230
return map;

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.function.Function;
2626

2727
import org.springframework.util.Assert;
28+
import org.springframework.util.ObjectUtils;
29+
import org.springframework.util.StringUtils;
2830

2931
/**
3032
* Value object which may or may not contain a Redis password.
@@ -34,13 +36,14 @@
3436
* The password is stored as character array.
3537
*
3638
* @author Mark Paluch
39+
* @author Christoph Strobl
3740
* @since 2.0
3841
*/
3942
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
4043
@EqualsAndHashCode
4144
public class RedisPassword {
4245

43-
private static final RedisPassword NONE = new RedisPassword(new char[0]);
46+
private static final RedisPassword NONE = new RedisPassword(new char[] {});
4447

4548
private final char[] thePassword;
4649

@@ -53,21 +56,21 @@ public class RedisPassword {
5356
public static RedisPassword of(String passwordAsString) {
5457

5558
return Optional.ofNullable(passwordAsString) //
56-
.filter(it -> it.length() != 0) //
59+
.filter(StringUtils::hasText) //
5760
.map(it -> new RedisPassword(it.toCharArray())) //
5861
.orElseGet(RedisPassword::none);
5962
}
6063

6164
/**
6265
* Create a {@link RedisPassword} from a {@code char} array.
6366
*
64-
* @param passwordAsChars the password as string.
67+
* @param passwordAsChars the password as char array.
6568
* @return the {@link RedisPassword} for {@code passwordAsChars}.
6669
*/
6770
public static RedisPassword of(char[] passwordAsChars) {
6871

6972
return Optional.ofNullable(passwordAsChars) //
70-
.filter(it -> it.length != 0) //
73+
.filter(it -> !ObjectUtils.isEmpty(passwordAsChars)) //
7174
.map(it -> new RedisPassword(Arrays.copyOf(it, it.length))) //
7275
.orElseGet(RedisPassword::none);
7376
}
@@ -87,7 +90,7 @@ public static RedisPassword none() {
8790
* @return {@code true} if there is a password present, otherwise {@code false}
8891
*/
8992
public boolean isPresent() {
90-
return thePassword.length != 0;
93+
return !ObjectUtils.isEmpty(thePassword);
9194
}
9295

9396
/**
@@ -117,11 +120,7 @@ public <R> Optional<R> map(Function<char[], R> mapper) {
117120

118121
Assert.notNull(mapper, "Mapper function must not be null!");
119122

120-
if (isPresent()) {
121-
return Optional.ofNullable(mapper.apply(thePassword));
122-
}
123-
124-
return Optional.empty();
123+
return toOptional().map(mapper);
125124
}
126125

127126
/**
@@ -140,7 +139,8 @@ public Optional<char[]> toOptional() {
140139
return Optional.empty();
141140
}
142141

143-
/* (non-Javadoc)
142+
/*
143+
* (non-Javadoc)
144144
* @see java.lang.Object#toString()
145145
*/
146146
@Override

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

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class RedisSentinelConfiguration {
5454
* Creates new {@link RedisSentinelConfiguration}.
5555
*/
5656
public RedisSentinelConfiguration() {
57-
this(new MapPropertySource("RedisSentinelConfiguration", Collections.<String, Object> emptyMap()));
57+
this(new MapPropertySource("RedisSentinelConfiguration", Collections.emptyMap()));
5858
}
5959

6060
/**
@@ -89,7 +89,7 @@ public RedisSentinelConfiguration(PropertySource<?> propertySource) {
8989

9090
notNull(propertySource, "PropertySource must not be null!");
9191

92-
this.sentinels = new LinkedHashSet<RedisNode>();
92+
this.sentinels = new LinkedHashSet<>();
9393

9494
if (propertySource.containsProperty(REDIS_SENTINEL_MASTER_CONFIG_PROPERTY)) {
9595
this.setMaster(propertySource.getProperty(REDIS_SENTINEL_MASTER_CONFIG_PROPERTY).toString());
@@ -145,13 +145,7 @@ public void addSentinel(RedisNode sentinel) {
145145
public void setMaster(final String name) {
146146

147147
notNull(name, "Name of sentinel master must not be null.");
148-
setMaster(new NamedNode() {
149-
150-
@Override
151-
public String getName() {
152-
return name;
153-
}
154-
});
148+
setMaster(() -> name);
155149
}
156150

157151
/**
@@ -168,16 +162,16 @@ public void setMaster(NamedNode master) {
168162
/**
169163
* Get the {@literal Sentinel} master node.
170164
*
171-
* @return
165+
* @return get the master node.
172166
*/
173167
public NamedNode getMaster() {
174168
return master;
175169
}
176170

177171
/**
178172
* @see #setMaster(String)
179-
* @param master
180-
* @return
173+
* @param master The master node name.
174+
* @return this.
181175
*/
182176
public RedisSentinelConfiguration master(String master) {
183177
this.setMaster(master);
@@ -186,8 +180,8 @@ public RedisSentinelConfiguration master(String master) {
186180

187181
/**
188182
* @see #setMaster(NamedNode)
189-
* @param master
190-
* @return
183+
* @param master the master node
184+
* @return this.
191185
*/
192186
public RedisSentinelConfiguration master(NamedNode master) {
193187
this.setMaster(master);
@@ -196,8 +190,8 @@ public RedisSentinelConfiguration master(NamedNode master) {
196190

197191
/**
198192
* @see #addSentinel(RedisNode)
199-
* @param sentinel
200-
* @return
193+
* @param sentinel the node to add as sentinel.
194+
* @return this.
201195
*/
202196
public RedisSentinelConfiguration sentinel(RedisNode sentinel) {
203197
this.addSentinel(sentinel);
@@ -206,9 +200,9 @@ public RedisSentinelConfiguration sentinel(RedisNode sentinel) {
206200

207201
/**
208202
* @see #sentinel(RedisNode)
209-
* @param host
210-
* @param port
211-
* @return
203+
* @param host redis sentinel node host name or ip.
204+
* @param port redis sentinel port.
205+
* @return this.
212206
*/
213207
public RedisSentinelConfiguration sentinel(String host, Integer port) {
214208
return sentinel(new RedisNode(host, port));
@@ -222,7 +216,7 @@ private void appendSentinels(Set<String> hostAndPorts) {
222216
}
223217

224218
/**
225-
* @return
219+
* @return the initial db index.
226220
* @since 2.0
227221
*/
228222
public int getDatabase() {
@@ -237,13 +231,13 @@ public int getDatabase() {
237231
*/
238232
public void setDatabase(int index) {
239233

240-
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
234+
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index));
241235

242236
this.database = index;
243237
}
244238

245239
/**
246-
* @return
240+
* @return never {@literal null}.
247241
* @since 2.0
248242
*/
249243
public RedisPassword getPassword() {
@@ -273,14 +267,14 @@ private RedisNode readHostAndPortFromString(String hostAndPort) {
273267
/**
274268
* @param master must not be {@literal null} or empty.
275269
* @param sentinelHostAndPorts must not be {@literal null}.
276-
* @return
270+
* @return configuration map.
277271
*/
278272
private static Map<String, Object> asMap(String master, Set<String> sentinelHostAndPorts) {
279273

280274
hasText(master, "Master address must not be null or empty!");
281275
notNull(sentinelHostAndPorts, "SentinelHostAndPorts must not be null!");
282276

283-
Map<String, Object> map = new HashMap<String, Object>();
277+
Map<String, Object> map = new HashMap<>();
284278
map.put(REDIS_SENTINEL_MASTER_CONFIG_PROPERTY, master);
285279
map.put(REDIS_SENTINEL_NODES_CONFIG_PROPERTY, StringUtils.collectionToCommaDelimitedString(sentinelHostAndPorts));
286280

0 commit comments

Comments
 (0)