Skip to content

Commit 53ce848

Browse files
mp911dechristophstrobl
authored andcommitted
Refactor converters.
Prefer lambda-style converters. Replace indirections via Converters API with method references. Original Pull Request: #1960
1 parent c7eef8f commit 53ce848

File tree

8 files changed

+329
-371
lines changed

8 files changed

+329
-371
lines changed

src/main/java/org/springframework/data/redis/connection/convert/Converters.java

Lines changed: 149 additions & 152 deletions
Large diffs are not rendered by default.

src/main/java/org/springframework/data/redis/connection/convert/SetConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.stream.Collectors;
2222

2323
import org.springframework.core.convert.converter.Converter;
24+
import org.springframework.lang.NonNull;
2425
import org.springframework.util.Assert;
2526

2627
/**
@@ -33,7 +34,7 @@
3334
*/
3435
public class SetConverter<S, T> implements Converter<Set<S>, Set<T>> {
3536

36-
private Converter<S, T> itemConverter;
37+
private final Converter<S, T> itemConverter;
3738

3839
/**
3940
* @param itemConverter The {@link Converter} to use for converting individual Set items. Must not be {@literal null}.
@@ -49,6 +50,7 @@ public SetConverter(Converter<S, T> itemConverter) {
4950
* @see org.springframework.core.convert.converter.Converter#convert(Object)
5051
*/
5152
@Override
53+
@NonNull
5254
public Set<T> convert(Set<S> source) {
5355

5456
return source.stream().map(itemConverter::convert)

src/main/java/org/springframework/data/redis/connection/convert/StringToRedisClientInfoConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.core.convert.converter.Converter;
2222
import org.springframework.data.redis.core.types.RedisClientInfo;
2323
import org.springframework.data.redis.core.types.RedisClientInfo.RedisClientInfoBuilder;
24+
import org.springframework.lang.NonNull;
2425

2526
/**
2627
* {@link Converter} implementation to create one {@link RedisClientInfo} per line entry in given {@link String} array.
@@ -42,6 +43,7 @@ public class StringToRedisClientInfoConverter implements Converter<String[], Lis
4243
* @see org.springframework.core.convert.converter.Converter#convert(Object)
4344
*/
4445
@Override
46+
@NonNull
4547
public List<RedisClientInfo> convert(String[] lines) {
4648

4749
List<RedisClientInfo> clientInfoList = new ArrayList<>(lines.length);

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterZSetCommands.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.dao.InvalidDataAccessApiUsageException;
2525
import org.springframework.data.redis.connection.ClusterSlotHashUtil;
2626
import org.springframework.data.redis.connection.RedisZSetCommands;
27+
import org.springframework.data.redis.connection.convert.SetConverter;
2728
import org.springframework.data.redis.core.Cursor;
2829
import org.springframework.data.redis.core.ScanCursor;
2930
import org.springframework.data.redis.core.ScanIteration;
@@ -40,6 +41,8 @@
4041
*/
4142
class JedisClusterZSetCommands implements RedisZSetCommands {
4243

44+
private static final SetConverter<redis.clients.jedis.Tuple, Tuple> TUPLE_SET_CONVERTER = new SetConverter<>(
45+
JedisConverters::toTuple);
4346
private final JedisClusterConnection connection;
4447

4548
JedisClusterZSetCommands(JedisClusterConnection connection) {
@@ -181,9 +184,9 @@ public Set<Tuple> zRangeByScoreWithScores(byte[] key, Range range, Limit limit)
181184

182185
try {
183186
if (limit.isUnlimited()) {
184-
return JedisConverters.toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max));
187+
return toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max));
185188
}
186-
return JedisConverters.toTupleSet(
189+
return toTupleSet(
187190
connection.getCluster().zrangeByScoreWithScores(key, min, max, limit.getOffset(), limit.getCount()));
188191
} catch (Exception ex) {
189192
throw convertJedisAccessException(ex);
@@ -228,9 +231,9 @@ public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, Range range, Limit limi
228231

229232
try {
230233
if (limit.isUnlimited()) {
231-
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min));
234+
return toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min));
232235
}
233-
return JedisConverters.toTupleSet(
236+
return toTupleSet(
234237
connection.getCluster().zrevrangeByScoreWithScores(key, max, min, limit.getOffset(), limit.getCount()));
235238
} catch (Exception ex) {
236239
throw convertJedisAccessException(ex);
@@ -379,7 +382,7 @@ public Set<Tuple> zRangeWithScores(byte[] key, long start, long end) {
379382
Assert.notNull(key, "Key must not be null!");
380383

381384
try {
382-
return JedisConverters.toTupleSet(connection.getCluster().zrangeWithScores(key, start, end));
385+
return toTupleSet(connection.getCluster().zrangeWithScores(key, start, end));
383386
} catch (Exception ex) {
384387
throw convertJedisAccessException(ex);
385388
}
@@ -411,7 +414,7 @@ public Set<Tuple> zRangeByScoreWithScores(byte[] key, double min, double max) {
411414
Assert.notNull(key, "Key must not be null!");
412415

413416
try {
414-
return JedisConverters.toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max));
417+
return toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max));
415418
} catch (Exception ex) {
416419
throw convertJedisAccessException(ex);
417420
}
@@ -452,7 +455,7 @@ public Set<Tuple> zRangeByScoreWithScores(byte[] key, double min, double max, lo
452455
}
453456

454457
try {
455-
return JedisConverters.toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max,
458+
return toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max,
456459
Long.valueOf(offset).intValue(), Long.valueOf(count).intValue()));
457460
} catch (Exception ex) {
458461
throw convertJedisAccessException(ex);
@@ -485,7 +488,7 @@ public Set<Tuple> zRevRangeWithScores(byte[] key, long start, long end) {
485488
Assert.notNull(key, "Key must not be null!");
486489

487490
try {
488-
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeWithScores(key, start, end));
491+
return toTupleSet(connection.getCluster().zrevrangeWithScores(key, start, end));
489492
} catch (Exception ex) {
490493
throw convertJedisAccessException(ex);
491494
}
@@ -517,7 +520,7 @@ public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, double min, double max)
517520
Assert.notNull(key, "Key must not be null!");
518521

519522
try {
520-
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min));
523+
return toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min));
521524
} catch (Exception ex) {
522525
throw convertJedisAccessException(ex);
523526
}
@@ -558,7 +561,7 @@ public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, double min, double max,
558561
}
559562

560563
try {
561-
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min,
564+
return toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min,
562565
Long.valueOf(offset).intValue(), Long.valueOf(count).intValue()));
563566
} catch (Exception ex) {
564567
throw convertJedisAccessException(ex);
@@ -819,4 +822,8 @@ private DataAccessException convertJedisAccessException(Exception ex) {
819822
return connection.convertJedisAccessException(ex);
820823
}
821824

825+
private static Set<Tuple> toTupleSet(Set<redis.clients.jedis.Tuple> source) {
826+
return TUPLE_SET_CONVERTER.convert(source);
827+
}
828+
822829
}

0 commit comments

Comments
 (0)