Skip to content

Commit 755c841

Browse files
mp911dechristophstrobl
authored andcommitted
Polish RedisZSet.
Improve Javadoc. Add static factory methods for easier RedisZSet construction. See #2007 Original Pull Request: #2088
1 parent bdf15de commit 755c841

File tree

5 files changed

+139
-5
lines changed

5 files changed

+139
-5
lines changed

src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.data.redis.core.RedisOperations;
2424
import org.springframework.lang.Nullable;
25+
import org.springframework.util.Assert;
2526

2627
/**
2728
* Base implementation for {@link RedisCollection}. Provides a skeletal implementation. Note that the collection support
@@ -47,6 +48,9 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
4748
*/
4849
public AbstractRedisCollection(String key, RedisOperations<String, E> operations) {
4950

51+
Assert.hasText(key, "Key must not be empty!");
52+
Assert.notNull(operations, "RedisOperations must not be null!");
53+
5054
this.key = key;
5155
this.operations = operations;
5256
}
@@ -217,7 +221,7 @@ public String toString() {
217221

218222
StringBuilder sb = new StringBuilder();
219223

220-
sb.append("RedisStore for key:");
224+
sb.append(String.format("%s for key:", getClass().getSimpleName()));
221225
sb.append(getKey());
222226

223227
return sb.toString();

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
4444

4545
private final BoundZSetOperations<String, E> boundZSetOps;
46-
private double defaultScore = 1;
46+
private final double defaultScore;
4747

4848
private class DefaultRedisSortedSetIterator extends RedisIterator<E> {
4949

src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private RedisStore createStore(DataType dt) {
112112
return new DefaultRedisSet(key, template);
113113

114114
case ZSET:
115-
return new DefaultRedisZSet(key, template);
115+
return RedisZSet.create(key, template);
116116

117117
case HASH:
118118
if (CollectionType.PROPERTIES.equals(type)) {

src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java

+131-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import java.util.SortedSet;
2424
import java.util.concurrent.TimeUnit;
2525

26+
import org.springframework.data.redis.connection.RedisZSetCommands;
2627
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
2728
import org.springframework.data.redis.connection.RedisZSetCommands.Range;
2829
import org.springframework.data.redis.core.BoundZSetOperations;
30+
import org.springframework.data.redis.core.RedisOperations;
2931
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
3032

3133
/**
@@ -41,16 +43,85 @@
4143
*/
4244
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
4345

46+
/**
47+
* Constructs a new {@link RedisZSet} instance with a default score of {@literal 1}.
48+
*
49+
* @param key Redis key of this set.
50+
* @param operations {@link RedisOperations} for the value type of this set.
51+
* @since 2.6
52+
*/
53+
static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations) {
54+
return new DefaultRedisZSet<>(key, operations, 1);
55+
}
56+
57+
/**
58+
* Constructs a new {@link RedisZSet} instance.
59+
*
60+
* @param key Redis key of this set.
61+
* @param operations {@link RedisOperations} for the value type of this set.
62+
* @param defaultScore
63+
* @since 2.6
64+
*/
65+
static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations, double defaultScore) {
66+
return new DefaultRedisZSet<>(key, operations, defaultScore);
67+
}
68+
69+
/**
70+
* Create a new {@link RedisZSet} by intersecting this sorted set and {@link RedisZSet} and store result in
71+
* destination {@code destKey}.
72+
*
73+
* @param set must not be {@literal null}.
74+
* @param destKey must not be {@literal null}.
75+
* @return a new {@link RedisZSet} pointing at {@code destKey}
76+
*/
4477
RedisZSet<E> intersectAndStore(RedisZSet<?> set, String destKey);
4578

79+
/**
80+
* Create a new {@link RedisZSet} by intersecting this sorted set and the collection {@link RedisZSet} and store
81+
* result in destination {@code destKey}.
82+
*
83+
* @param sets must not be {@literal null}.
84+
* @param destKey must not be {@literal null}.
85+
* @return a new {@link RedisZSet} pointing at {@code destKey}
86+
*/
4687
RedisZSet<E> intersectAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
4788

89+
/**
90+
* Create a new {@link RedisZSet} by union this sorted set and {@link RedisZSet} and store result in destination
91+
* {@code destKey}.
92+
*
93+
* @param set must not be {@literal null}.
94+
* @param destKey must not be {@literal null}.
95+
* @return a new {@link RedisZSet} pointing at {@code destKey}
96+
*/
4897
RedisZSet<E> unionAndStore(RedisZSet<?> set, String destKey);
4998

99+
/**
100+
* Create a new {@link RedisZSet} by union this sorted set and the collection {@link RedisZSet} and store result in
101+
* destination {@code destKey}.
102+
*
103+
* @param sets must not be {@literal null}.
104+
* @param destKey must not be {@literal null}.
105+
* @return a new {@link RedisZSet} pointing at {@code destKey}
106+
*/
50107
RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
51108

109+
/**
110+
* Get elements between {@code start} and {@code end} from sorted set.
111+
*
112+
* @param start
113+
* @param end
114+
* @return
115+
*/
52116
Set<E> range(long start, long end);
53117

118+
/**
119+
* Get elements in range from {@code start} to {@code end} from sorted set ordered from high to low.
120+
*
121+
* @param start
122+
* @param end
123+
* @return
124+
*/
54125
Set<E> reverseRange(long start, long end);
55126

56127
/**
@@ -105,29 +176,88 @@ default Set<E> reverseRangeByLex(Range range) {
105176
*/
106177
Set<E> reverseRangeByLex(Range range, Limit limit);
107178

179+
/**
180+
* Get elements where score is between {@code min} and {@code max} from sorted set.
181+
*
182+
* @param min
183+
* @param max
184+
* @return
185+
*/
108186
Set<E> rangeByScore(double min, double max);
109187

188+
/**
189+
* Get elements where score is between {@code min} and {@code max} from sorted set ordered from high to low.
190+
*
191+
* @param min
192+
* @param max
193+
* @return
194+
*/
110195
Set<E> reverseRangeByScore(double min, double max);
111196

197+
/**
198+
* Get set of {@link RedisZSetCommands.Tuple}s between {@code start} and {@code end} from sorted set.
199+
*
200+
* @param start
201+
* @param end
202+
* @return
203+
*/
112204
Set<TypedTuple<E>> rangeWithScores(long start, long end);
113205

206+
/**
207+
* Get set of {@link RedisZSetCommands.Tuple}s in range from {@code start} to {@code end} from sorted set ordered from
208+
* high to low.
209+
*
210+
* @param start
211+
* @param end
212+
* @return
213+
*/
114214
Set<TypedTuple<E>> reverseRangeWithScores(long start, long end);
115215

216+
/**
217+
* Get set of {@link RedisZSetCommands.Tuple}s where score is between {@code min} and {@code max} from sorted set.
218+
*
219+
* @param min
220+
* @param max
221+
* @return
222+
*/
116223
Set<TypedTuple<E>> rangeByScoreWithScores(double min, double max);
117224

225+
/**
226+
* Get set of {@link RedisZSetCommands.Tuple}s where score is between {@code min} and {@code max} from sorted set
227+
* ordered from high to low.
228+
*
229+
* @param min
230+
* @param max
231+
* @return
232+
*/
118233
Set<TypedTuple<E>> reverseRangeByScoreWithScores(double min, double max);
119234

235+
/**
236+
* Remove elements in range between {@code start} and {@code end} from sorted set.
237+
*
238+
* @param start
239+
* @param end
240+
* @return {@code this} set.
241+
*/
120242
RedisZSet<E> remove(long start, long end);
121243

122244
/**
123245
* Remove all elements in range.
124246
*
125247
* @param range must not be {@literal null}.
126-
* @return never {@literal null}.
248+
* @return {@code this} set.
127249
* @since 2.5
128250
*/
251+
// TODO: Switch to RedisZSet
129252
Set<E> removeByLex(Range range);
130253

254+
/**
255+
* Remove elements with scores between {@code min} and {@code max} from sorted set with the bound key.
256+
*
257+
* @param min
258+
* @param max
259+
* @return {@code this} set.
260+
*/
131261
RedisZSet<E> removeByScore(double min, double max);
132262

133263
/**

src/test/java/org/springframework/data/redis/support/collections/RedisZSetIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public RedisZSetIntegrationTests(ObjectFactory<Object> factory, RedisTemplate te
3636
}
3737

3838
RedisStore copyStore(RedisStore store) {
39-
return new DefaultRedisZSet(store.getKey().toString(), store.getOperations());
39+
return RedisZSet.create(store.getKey(), store.getOperations());
4040
}
4141

4242
AbstractRedisCollection<Object> createCollection() {

0 commit comments

Comments
 (0)