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
This commit is contained in:
Christoph Strobl
2021-02-10 09:41:47 +01:00
parent 53ce84851c
commit 362396b21c
9 changed files with 218 additions and 238 deletions

View File

@@ -76,6 +76,17 @@ abstract public class Converters {
private static final byte[] ZERO = new byte[] { '0' };
private static final String CLUSTER_NODES_LINE_SEPARATOR = "\n";
/**
* Returns a {@link Converter} that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
* @since 2.5
*/
public static <T> Converter<T, T> identityConverter() {
return t -> t;
}
public static Boolean stringToBoolean(String source) {
return ObjectUtils.nullSafeEquals("OK", source);
}

View File

@@ -21,7 +21,6 @@ import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
/**
@@ -50,7 +49,6 @@ public class SetConverter<S, T> implements Converter<Set<S>, Set<T>> {
* @see org.springframework.core.convert.converter.Converter#convert(Object)
*/
@Override
@NonNull
public Set<T> convert(Set<S> source) {
return source.stream().map(itemConverter::convert)

View File

@@ -21,7 +21,6 @@ import java.util.List;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.core.types.RedisClientInfo.RedisClientInfoBuilder;
import org.springframework.lang.NonNull;
/**
* {@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
* @see org.springframework.core.convert.converter.Converter#convert(Object)
*/
@Override
@NonNull
public List<RedisClientInfo> convert(String[] lines) {
List<RedisClientInfo> clientInfoList = new ArrayList<>(lines.length);

View File

@@ -79,7 +79,6 @@ public class JedisConnection extends AbstractRedisConnection {
nullDefault) -> doInvoke(true, directFunction, pipelineFunction, converter, nullDefault));
private final @Nullable Pool<Jedis> pool;
private final int dbIndex;
private final String clientName;
private List<JedisResult> pipelinedResults = new ArrayList<>();
@@ -124,7 +123,6 @@ public class JedisConnection extends AbstractRedisConnection {
this.jedis = jedis;
this.pool = pool;
this.dbIndex = dbIndex;
this.clientName = clientName;
// select the db

View File

@@ -29,6 +29,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -65,16 +66,6 @@ class JedisInvoker {
this.synchronizer = synchronizer;
}
/**
* Returns a {@link Converter} that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Converter<T, T> identityConverter() {
return t -> t;
}
/**
* Invoke the {@link ConnectionFunction0} and return its result.
*
@@ -87,7 +78,7 @@ class JedisInvoker {
return synchronizer.invoke(function::apply, it -> {
throw new UnsupportedOperationException("Operation not supported in pipelining/transaction mode");
}, identityConverter(), () -> null);
}, Converters.identityConverter(), () -> null);
}
/**
@@ -102,7 +93,7 @@ class JedisInvoker {
Assert.notNull(function, "ConnectionFunction must not be null!");
Assert.notNull(pipelineFunction, "PipelineFunction must not be null!");
return synchronizer.invoke(function::apply, pipelineFunction::apply, identityConverter(), () -> null);
return synchronizer.invoke(function::apply, pipelineFunction::apply, Converters.identityConverter(), () -> null);
}
/**
@@ -568,7 +559,7 @@ class JedisInvoker {
* @return the result as {@link List}.
*/
default List<S> toList() {
return toList(identityConverter());
return toList(Converters.identityConverter());
}
/**
@@ -587,7 +578,7 @@ class JedisInvoker {
* @return the result as {@link Set}.
*/
default Set<S> toSet() {
return toSet(identityConverter());
return toSet(Converters.identityConverter());
}
/**
@@ -915,8 +906,9 @@ class JedisInvoker {
private final Function<MultiKeyPipelineBase, Response<S>> parentPipelineFunction;
private final Synchronizer synchronizer;
public DefaultSingleInvocationSpec(Function<Jedis, S> parentFunction,
DefaultSingleInvocationSpec(Function<Jedis, S> parentFunction,
Function<MultiKeyPipelineBase, Response<S>> parentPipelineFunction, Synchronizer synchronizer) {
this.parentFunction = parentFunction;
this.parentPipelineFunction = parentPipelineFunction;
this.synchronizer = synchronizer;
@@ -946,7 +938,7 @@ class JedisInvoker {
private final Function<MultiKeyPipelineBase, Response<Collection<S>>> parentPipelineFunction;
private final Synchronizer synchronizer;
public DefaultManyInvocationSpec(Function<Jedis, ? extends Collection<S>> parentFunction,
DefaultManyInvocationSpec(Function<Jedis, ? extends Collection<S>> parentFunction,
Function<MultiKeyPipelineBase, Response<? extends Collection<S>>> parentPipelineFunction,
Synchronizer synchronizer) {
@@ -1008,7 +1000,7 @@ class JedisInvoker {
@SuppressWarnings({ "unchecked", "rawtypes" })
default <I, T> T invoke(Function<Jedis, I> callFunction,
Function<MultiKeyPipelineBase, Response<I>> pipelineFunction) {
return (T) doInvoke((Function) callFunction, (Function) pipelineFunction, identityConverter(), () -> null);
return (T) doInvoke((Function) callFunction, (Function) pipelineFunction, Converters.identityConverter(), () -> null);
}
@Nullable

View File

@@ -176,9 +176,6 @@ class JedisKeyCommands implements RedisKeyCommands {
protected void doClose() {
JedisKeyCommands.this.connection.close();
}
;
}.open();
}

View File

@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.function.Supplier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -66,16 +67,6 @@ class LettuceInvoker {
this.synchronizer = synchronizer;
}
/**
* Returns a {@link Converter} that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Converter<T, T> identityConverter() {
return t -> t;
}
/**
* Invoke the {@link ConnectionFunction0} and return its result.
*
@@ -86,7 +77,7 @@ class LettuceInvoker {
Assert.notNull(function, "ConnectionFunction must not be null!");
return synchronizer.invoke(() -> function.apply(connection), identityConverter(), () -> null);
return synchronizer.invoke(() -> function.apply(connection), Converters.identityConverter(), () -> null);
}
/**
@@ -421,7 +412,7 @@ class LettuceInvoker {
* @return the result as {@link List}.
*/
default List<S> toList() {
return toList(identityConverter());
return toList(Converters.identityConverter());
}
/**
@@ -440,7 +431,7 @@ class LettuceInvoker {
* @return the result as {@link Set}.
*/
default Set<S> toSet() {
return toSet(identityConverter());
return toSet(Converters.identityConverter());
}
/**
@@ -665,12 +656,13 @@ class LettuceInvoker {
/**
* Interface to define a synchronization function to evaluate {@link RedisFuture}.
*/
@FunctionalInterface
interface Synchronizer {
@Nullable
@SuppressWarnings({ "unchecked", "rawtypes" })
default <I, T> T invoke(Supplier<RedisFuture<I>> futureSupplier) {
return (T) doInvoke((Supplier) futureSupplier, identityConverter(), () -> null);
return (T) doInvoke((Supplier) futureSupplier, Converters.identityConverter(), () -> null);
}
@Nullable

View File

@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Set;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.KeyBoundCursor;
import org.springframework.data.redis.core.ScanIteration;
@@ -185,7 +186,7 @@ class LettuceZSetCommands implements RedisZSetCommands {
Assert.notNull(key, "Key must not be null!");
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrange, key, start, end)
.toSet(LettuceInvoker.identityConverter());
.toSet(Converters.identityConverter());
}
/*