Skip to content

Commit 362396b

Browse files
Move identityConverter to Converters.
Move the no op identity converter to Converters and reuse those across the connection package. Fix nullables and minor code format issues along the way. See: #1951 Original Pull Request: #1960
1 parent 53ce848 commit 362396b

File tree

9 files changed

+218
-238
lines changed

9 files changed

+218
-238
lines changed

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

Lines changed: 190 additions & 197 deletions
Large diffs are not rendered by default.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ abstract public class Converters {
7676
private static final byte[] ZERO = new byte[] { '0' };
7777
private static final String CLUSTER_NODES_LINE_SEPARATOR = "\n";
7878

79+
/**
80+
* Returns a {@link Converter} that always returns its input argument.
81+
*
82+
* @param <T> the type of the input and output objects to the function
83+
* @return a function that always returns its input argument
84+
* @since 2.5
85+
*/
86+
public static <T> Converter<T, T> identityConverter() {
87+
return t -> t;
88+
}
89+
7990
public static Boolean stringToBoolean(String source) {
8091
return ObjectUtils.nullSafeEquals("OK", source);
8192
}

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

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

2323
import org.springframework.core.convert.converter.Converter;
24-
import org.springframework.lang.NonNull;
2524
import org.springframework.util.Assert;
2625

2726
/**
@@ -50,7 +49,6 @@ public SetConverter(Converter<S, T> itemConverter) {
5049
* @see org.springframework.core.convert.converter.Converter#convert(Object)
5150
*/
5251
@Override
53-
@NonNull
5452
public Set<T> convert(Set<S> source) {
5553

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
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;
2524

2625
/**
2726
* {@link Converter} implementation to create one {@link RedisClientInfo} per line entry in given {@link String} array.
@@ -43,7 +42,6 @@ public class StringToRedisClientInfoConverter implements Converter<String[], Lis
4342
* @see org.springframework.core.convert.converter.Converter#convert(Object)
4443
*/
4544
@Override
46-
@NonNull
4745
public List<RedisClientInfo> convert(String[] lines) {
4846

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public class JedisConnection extends AbstractRedisConnection {
7979
nullDefault) -> doInvoke(true, directFunction, pipelineFunction, converter, nullDefault));
8080

8181
private final @Nullable Pool<Jedis> pool;
82-
private final int dbIndex;
8382
private final String clientName;
8483

8584
private List<JedisResult> pipelinedResults = new ArrayList<>();
@@ -124,7 +123,6 @@ protected JedisConnection(Jedis jedis, @Nullable Pool<Jedis> pool, int dbIndex,
124123

125124
this.jedis = jedis;
126125
this.pool = pool;
127-
this.dbIndex = dbIndex;
128126
this.clientName = clientName;
129127

130128
// select the db

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.Supplier;
3030

3131
import org.springframework.core.convert.converter.Converter;
32+
import org.springframework.data.redis.connection.convert.Converters;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.util.Assert;
3435

@@ -65,16 +66,6 @@ class JedisInvoker {
6566
this.synchronizer = synchronizer;
6667
}
6768

68-
/**
69-
* Returns a {@link Converter} that always returns its input argument.
70-
*
71-
* @param <T> the type of the input and output objects to the function
72-
* @return a function that always returns its input argument
73-
*/
74-
static <T> Converter<T, T> identityConverter() {
75-
return t -> t;
76-
}
77-
7869
/**
7970
* Invoke the {@link ConnectionFunction0} and return its result.
8071
*
@@ -87,7 +78,7 @@ <R> R just(ConnectionFunction0<R> function) {
8778

8879
return synchronizer.invoke(function::apply, it -> {
8980
throw new UnsupportedOperationException("Operation not supported in pipelining/transaction mode");
90-
}, identityConverter(), () -> null);
81+
}, Converters.identityConverter(), () -> null);
9182
}
9283

9384
/**
@@ -102,7 +93,7 @@ <R> R just(ConnectionFunction0<R> function, PipelineFunction0<R> pipelineFunctio
10293
Assert.notNull(function, "ConnectionFunction must not be null!");
10394
Assert.notNull(pipelineFunction, "PipelineFunction must not be null!");
10495

105-
return synchronizer.invoke(function::apply, pipelineFunction::apply, identityConverter(), () -> null);
96+
return synchronizer.invoke(function::apply, pipelineFunction::apply, Converters.identityConverter(), () -> null);
10697
}
10798

10899
/**
@@ -568,7 +559,7 @@ interface ManyInvocationSpec<S> {
568559
* @return the result as {@link List}.
569560
*/
570561
default List<S> toList() {
571-
return toList(identityConverter());
562+
return toList(Converters.identityConverter());
572563
}
573564

574565
/**
@@ -587,7 +578,7 @@ default List<S> toList() {
587578
* @return the result as {@link Set}.
588579
*/
589580
default Set<S> toSet() {
590-
return toSet(identityConverter());
581+
return toSet(Converters.identityConverter());
591582
}
592583

593584
/**
@@ -915,8 +906,9 @@ static class DefaultSingleInvocationSpec<S> implements SingleInvocationSpec<S> {
915906
private final Function<MultiKeyPipelineBase, Response<S>> parentPipelineFunction;
916907
private final Synchronizer synchronizer;
917908

918-
public DefaultSingleInvocationSpec(Function<Jedis, S> parentFunction,
909+
DefaultSingleInvocationSpec(Function<Jedis, S> parentFunction,
919910
Function<MultiKeyPipelineBase, Response<S>> parentPipelineFunction, Synchronizer synchronizer) {
911+
920912
this.parentFunction = parentFunction;
921913
this.parentPipelineFunction = parentPipelineFunction;
922914
this.synchronizer = synchronizer;
@@ -946,7 +938,7 @@ static class DefaultManyInvocationSpec<S> implements ManyInvocationSpec<S> {
946938
private final Function<MultiKeyPipelineBase, Response<Collection<S>>> parentPipelineFunction;
947939
private final Synchronizer synchronizer;
948940

949-
public DefaultManyInvocationSpec(Function<Jedis, ? extends Collection<S>> parentFunction,
941+
DefaultManyInvocationSpec(Function<Jedis, ? extends Collection<S>> parentFunction,
950942
Function<MultiKeyPipelineBase, Response<? extends Collection<S>>> parentPipelineFunction,
951943
Synchronizer synchronizer) {
952944

@@ -1008,7 +1000,7 @@ interface Synchronizer {
10081000
@SuppressWarnings({ "unchecked", "rawtypes" })
10091001
default <I, T> T invoke(Function<Jedis, I> callFunction,
10101002
Function<MultiKeyPipelineBase, Response<I>> pipelineFunction) {
1011-
return (T) doInvoke((Function) callFunction, (Function) pipelineFunction, identityConverter(), () -> null);
1003+
return (T) doInvoke((Function) callFunction, (Function) pipelineFunction, Converters.identityConverter(), () -> null);
10121004
}
10131005

10141006
@Nullable

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ protected ScanIteration<byte[]> doScan(long cursorId, ScanOptions options) {
176176
protected void doClose() {
177177
JedisKeyCommands.this.connection.close();
178178
}
179-
180-
;
181-
182179
}.open();
183180
}
184181

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceInvoker.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.function.Supplier;
2828

2929
import org.springframework.core.convert.converter.Converter;
30+
import org.springframework.data.redis.connection.convert.Converters;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233

@@ -66,16 +67,6 @@ class LettuceInvoker {
6667
this.synchronizer = synchronizer;
6768
}
6869

69-
/**
70-
* Returns a {@link Converter} that always returns its input argument.
71-
*
72-
* @param <T> the type of the input and output objects to the function
73-
* @return a function that always returns its input argument
74-
*/
75-
static <T> Converter<T, T> identityConverter() {
76-
return t -> t;
77-
}
78-
7970
/**
8071
* Invoke the {@link ConnectionFunction0} and return its result.
8172
*
@@ -86,7 +77,7 @@ <R> R just(ConnectionFunction0<R> function) {
8677

8778
Assert.notNull(function, "ConnectionFunction must not be null!");
8879

89-
return synchronizer.invoke(() -> function.apply(connection), identityConverter(), () -> null);
80+
return synchronizer.invoke(() -> function.apply(connection), Converters.identityConverter(), () -> null);
9081
}
9182

9283
/**
@@ -421,7 +412,7 @@ interface ManyInvocationSpec<S> {
421412
* @return the result as {@link List}.
422413
*/
423414
default List<S> toList() {
424-
return toList(identityConverter());
415+
return toList(Converters.identityConverter());
425416
}
426417

427418
/**
@@ -440,7 +431,7 @@ default List<S> toList() {
440431
* @return the result as {@link Set}.
441432
*/
442433
default Set<S> toSet() {
443-
return toSet(identityConverter());
434+
return toSet(Converters.identityConverter());
444435
}
445436

446437
/**
@@ -665,12 +656,13 @@ public <T> Set<T> toSet(Converter<S, T> converter) {
665656
/**
666657
* Interface to define a synchronization function to evaluate {@link RedisFuture}.
667658
*/
659+
@FunctionalInterface
668660
interface Synchronizer {
669661

670662
@Nullable
671663
@SuppressWarnings({ "unchecked", "rawtypes" })
672664
default <I, T> T invoke(Supplier<RedisFuture<I>> futureSupplier) {
673-
return (T) doInvoke((Supplier) futureSupplier, identityConverter(), () -> null);
665+
return (T) doInvoke((Supplier) futureSupplier, Converters.identityConverter(), () -> null);
674666
}
675667

676668
@Nullable

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceZSetCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Set;
2727

2828
import org.springframework.data.redis.connection.RedisZSetCommands;
29+
import org.springframework.data.redis.connection.convert.Converters;
2930
import org.springframework.data.redis.core.Cursor;
3031
import org.springframework.data.redis.core.KeyBoundCursor;
3132
import org.springframework.data.redis.core.ScanIteration;
@@ -185,7 +186,7 @@ public Set<byte[]> zRevRange(byte[] key, long start, long end) {
185186
Assert.notNull(key, "Key must not be null!");
186187

187188
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrange, key, start, end)
188-
.toSet(LettuceInvoker.identityConverter());
189+
.toSet(Converters.identityConverter());
189190
}
190191

191192
/*

0 commit comments

Comments
 (0)