diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceInvoker.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceInvoker.java index 83af96084..1322e2516 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceInvoker.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceInvoker.java @@ -52,6 +52,7 @@ import org.springframework.util.Assert; * completion or record the future along {@link Converter} for further processing. * * @author Mark Paluch + * @author Christoph Strobl * @since 2.5 */ class LettuceInvoker { @@ -60,6 +61,7 @@ class LettuceInvoker { private final Synchronizer synchronizer; LettuceInvoker(RedisClusterAsyncCommands connection, Synchronizer synchronizer) { + this.connection = connection; this.synchronizer = synchronizer; } @@ -82,7 +84,7 @@ class LettuceInvoker { @Nullable R just(ConnectionFunction0 function) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return synchronizer.invoke(() -> function.apply(connection), identityConverter(), () -> null); } @@ -91,11 +93,12 @@ class LettuceInvoker { * Invoke the {@link ConnectionFunction1} and return its result. * * @param function must not be {@literal null}. + * @param t1 first argument. */ @Nullable R just(ConnectionFunction1 function, T1 t1) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return synchronizer.invoke(() -> function.apply(connection, t1)); } @@ -104,11 +107,13 @@ class LettuceInvoker { * Invoke the {@link ConnectionFunction2} and return its result. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. */ @Nullable R just(ConnectionFunction2 function, T1 t1, T2 t2) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return synchronizer.invoke(() -> function.apply(connection, t1, t2)); } @@ -117,11 +122,14 @@ class LettuceInvoker { * Invoke the {@link ConnectionFunction3} and return its result. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. */ @Nullable R just(ConnectionFunction3 function, T1 t1, T2 t2, T3 t3) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return synchronizer.invoke(() -> function.apply(connection, t1, t2, t3)); } @@ -130,11 +138,15 @@ class LettuceInvoker { * Invoke the {@link ConnectionFunction4} and return its result. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. */ @Nullable R just(ConnectionFunction4 function, T1 t1, T2 t2, T3 t3, T4 t4) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return synchronizer.invoke(() -> function.apply(connection, t1, t2, t3, t4)); } @@ -143,11 +155,17 @@ class LettuceInvoker { * Invoke the {@link ConnectionFunction5} and return its result. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. + * @param t5 fifth argument. */ @Nullable R just(ConnectionFunction5 function, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { - Assert.notNull(function, "ConnectionFunction must not be null"); + + Assert.notNull(function, "ConnectionFunction must not be null!"); return synchronizer.invoke(() -> function.apply(connection, t1, t2, t3, t4, t5)); } @@ -160,7 +178,7 @@ class LettuceInvoker { */ SingleInvocationSpec from(ConnectionFunction0 function) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return new DefaultSingleInvocationSpec<>(() -> function.apply(connection), synchronizer); } @@ -170,10 +188,11 @@ class LettuceInvoker { * further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. */ SingleInvocationSpec from(ConnectionFunction1 function, T1 t1) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return from(it -> function.apply(it, t1)); } @@ -183,10 +202,12 @@ class LettuceInvoker { * further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. */ SingleInvocationSpec from(ConnectionFunction2 function, T1 t1, T2 t2) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return from(it -> function.apply(it, t1, t2)); } @@ -196,10 +217,13 @@ class LettuceInvoker { * further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. */ SingleInvocationSpec from(ConnectionFunction3 function, T1 t1, T2 t2, T3 t3) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return from(it -> function.apply(it, t1, t2, t3)); } @@ -209,11 +233,15 @@ class LettuceInvoker { * further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. */ SingleInvocationSpec from(ConnectionFunction4 function, T1 t1, T2 t2, T3 t3, T4 t4) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return from(it -> function.apply(it, t1, t2, t3, t4)); } @@ -223,11 +251,16 @@ class LettuceInvoker { * further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. + * @param t5 fifth argument. */ SingleInvocationSpec from(ConnectionFunction5 function, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return from(it -> function.apply(it, t1, t2, t3, t4, t5)); } @@ -240,7 +273,7 @@ class LettuceInvoker { */ , E> ManyInvocationSpec fromMany(ConnectionFunction0 function) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return new DefaultManyInvocationSpec<>(() -> function.apply(connection), synchronizer); } @@ -250,10 +283,11 @@ class LettuceInvoker { * and return a {@link ManyInvocationSpec} for further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. */ , E, T1> ManyInvocationSpec fromMany(ConnectionFunction1 function, T1 t1) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return fromMany(it -> function.apply(it, t1)); } @@ -263,11 +297,13 @@ class LettuceInvoker { * and return a {@link ManyInvocationSpec} for further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. */ , E, T1, T2> ManyInvocationSpec fromMany(ConnectionFunction2 function, T1 t1, T2 t2) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return fromMany(it -> function.apply(it, t1, t2)); } @@ -277,11 +313,14 @@ class LettuceInvoker { * and return a {@link ManyInvocationSpec} for further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. */ , E, T1, T2, T3> ManyInvocationSpec fromMany(ConnectionFunction3 function, T1 t1, T2 t2, T3 t3) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return fromMany(it -> function.apply(it, t1, t2, t3)); } @@ -291,11 +330,15 @@ class LettuceInvoker { * and return a {@link ManyInvocationSpec} for further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. */ , E, T1, T2, T3, T4> ManyInvocationSpec fromMany( ConnectionFunction4 function, T1 t1, T2 t2, T3 t3, T4 t4) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return fromMany(it -> function.apply(it, t1, t2, t3, t4)); } @@ -305,11 +348,16 @@ class LettuceInvoker { * and return a {@link ManyInvocationSpec} for further composition. * * @param function must not be {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. + * @param t5 fifth argument. */ , E, T1, T2, T3, T4, T5> ManyInvocationSpec fromMany( ConnectionFunction5 function, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { - Assert.notNull(function, "ConnectionFunction must not be null"); + Assert.notNull(function, "ConnectionFunction must not be null!"); return fromMany(it -> function.apply(it, t1, t2, t3, t4, t5)); } @@ -319,7 +367,7 @@ class LettuceInvoker { * * @param */ - public interface SingleInvocationSpec { + interface SingleInvocationSpec { /** * Materialize the pipeline by invoking the {@code ConnectionFunction} and returning the result after applying @@ -334,7 +382,21 @@ class LettuceInvoker { /** * Materialize the pipeline by invoking the {@code ConnectionFunction} and returning the result after applying - * {@link Converter}. + * {@link Converter} or return the {@literal nullDefault} value if not present. + * + * @param converter must not be {@literal null}. + * @param nullDefault can be {@literal null}. + * @param target type. + * @return the converted result, can be {@literal null}. + */ + @Nullable + default T orElse(Converter converter, @Nullable T nullDefault) { + return getOrElse(converter, () -> nullDefault); + } + + /** + * Materialize the pipeline by invoking the {@code ConnectionFunction} and returning the result after applying + * {@link Converter} or return the {@literal nullDefault} value if not present. * * @param converter must not be {@literal null}. * @param nullDefault must not be {@literal null}. @@ -351,7 +413,7 @@ class LettuceInvoker { * * @param */ - public interface ManyInvocationSpec { + interface ManyInvocationSpec { /** * Materialize the pipeline by invoking the {@code ConnectionFunction} and returning the result. @@ -398,10 +460,12 @@ class LettuceInvoker { * @param */ @FunctionalInterface - public interface ConnectionFunction0 { + interface ConnectionFunction0 { /** * Apply this function to the arguments and return a {@link RedisFuture}. + * + * @param connection the connection in use. Never {@literal null}. */ RedisFuture apply(RedisClusterAsyncCommands connection); } @@ -413,10 +477,13 @@ class LettuceInvoker { * @param */ @FunctionalInterface - public interface ConnectionFunction1 { + interface ConnectionFunction1 { /** * Apply this function to the arguments and return a {@link RedisFuture}. + * + * @param connection the connection in use. Never {@literal null}. + * @param t1 first argument. */ RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1); } @@ -429,10 +496,14 @@ class LettuceInvoker { * @param */ @FunctionalInterface - public interface ConnectionFunction2 { + interface ConnectionFunction2 { /** * Apply this function to the arguments and return a {@link RedisFuture}. + * + * @param connection the connection in use. Never {@literal null}. + * @param t1 first argument. + * @param t2 second argument. */ RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2); } @@ -446,10 +517,15 @@ class LettuceInvoker { * @param */ @FunctionalInterface - public interface ConnectionFunction3 { + interface ConnectionFunction3 { /** * Apply this function to the arguments and return a {@link RedisFuture}. + * + * @param connection the connection in use. Never {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. */ RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2, T3 t3); } @@ -464,10 +540,16 @@ class LettuceInvoker { * @param */ @FunctionalInterface - public interface ConnectionFunction4 { + interface ConnectionFunction4 { /** * Apply this function to the arguments and return a {@link RedisFuture}. + * + * @param connection the connection in use. Never {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. */ RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2, T3 t3, T4 t4); } @@ -483,80 +565,21 @@ class LettuceInvoker { * @param */ @FunctionalInterface - public interface ConnectionFunction5 { + interface ConnectionFunction5 { /** * Apply this function to the arguments and return a {@link RedisFuture}. + * + * @param connection the connection in use. Never {@literal null}. + * @param t1 first argument. + * @param t2 second argument. + * @param t3 third argument. + * @param t4 fourth argument. + * @param t5 fifth argument. */ RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5); } - /** - * A function accepting {@link RedisClusterAsyncCommands} with 6 arguments. - * - * @param - * @param - * @param - * @param - * @param - * @param - * @param - */ - @FunctionalInterface - public interface ConnectionFunction6 { - - /** - * Apply this function to the arguments and return a {@link RedisFuture}. - */ - RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, - T6 t6); - } - - /** - * A function accepting {@link RedisClusterAsyncCommands} with 7 arguments. - * - * @param - * @param - * @param - * @param - * @param - * @param - * @param - * @param - */ - @FunctionalInterface - public interface ConnectionFunction7 { - - /** - * Apply this function to the arguments and return a {@link RedisFuture}. - */ - RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, - T7 t7); - } - - /** - * A function accepting {@link RedisClusterAsyncCommands} with 8 arguments. - * - * @param - * @param - * @param - * @param - * @param - * @param - * @param - * @param - * @param - */ - @FunctionalInterface - public interface ConnectionFunction8 { - - /** - * Apply this function to the arguments and return a {@link RedisFuture}. - */ - RedisFuture apply(RedisClusterAsyncCommands connection, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, - T7 t7, T8 t8); - } - static class DefaultSingleInvocationSpec implements SingleInvocationSpec { private final Supplier> parent; @@ -579,7 +602,7 @@ class LettuceInvoker { @Override public T getOrElse(Converter converter, Supplier nullDefault) { - Assert.notNull(converter, "Converter must not be null"); + Assert.notNull(converter, "Converter must not be null!"); return synchronizer.invoke(parent, converter, nullDefault); } @@ -591,6 +614,7 @@ class LettuceInvoker { private final Synchronizer synchronizer; public DefaultManyInvocationSpec(Supplier>> parent, Synchronizer synchronizer) { + this.parent = (Supplier) parent; this.synchronizer = synchronizer; } @@ -598,7 +622,7 @@ class LettuceInvoker { @Override public List toList(Converter converter) { - Assert.notNull(converter, "Converter must not be null"); + Assert.notNull(converter, "Converter must not be null!"); return synchronizer.invoke(parent, source -> { @@ -619,7 +643,7 @@ class LettuceInvoker { @Override public Set toSet(Converter converter) { - Assert.notNull(converter, "Converter must not be null"); + Assert.notNull(converter, "Converter must not be null!"); return synchronizer.invoke(parent, source -> { @@ -653,6 +677,7 @@ class LettuceInvoker { @SuppressWarnings({ "unchecked", "rawtypes" }) default T invoke(Supplier> futureSupplier, Converter converter, Supplier nullDefault) { + return (T) doInvoke((Supplier) futureSupplier, (Converter) converter, (Supplier) nullDefault); } @@ -660,6 +685,5 @@ class LettuceInvoker { @Nullable Object doInvoke(Supplier> futureSupplier, Converter converter, Supplier nullDefault); - } } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceKeyCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceKeyCommands.java index 1fd74e326..f69cb7b83 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceKeyCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceKeyCommands.java @@ -406,8 +406,8 @@ class LettuceKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null!"); - return connection.invoke().from(RedisKeyAsyncCommands::objectEncoding, key).getOrElse(ValueEncoding::of, - () -> RedisValueEncoding.VACANT); + return connection.invoke().from(RedisKeyAsyncCommands::objectEncoding, key).orElse(ValueEncoding::of, + RedisValueEncoding.VACANT); } /* diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceListCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceListCommands.java index 5ba9aaae9..8c3500772 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceListCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceListCommands.java @@ -239,15 +239,6 @@ class LettuceListCommands implements RedisListCommands { .from(RedisListAsyncCommands::blpop, timeout, keys).get(LettuceListCommands::toBytesList); } - private static List toBytesList(KeyValue source) { - - List list = new ArrayList<>(2); - list.add(source.getKey()); - list.add(source.getValue()); - - return list; - } - /* * (non-Javadoc) * @see org.springframework.data.redis.connection.RedisListCommands#bRPop(int, byte[][]) @@ -289,4 +280,12 @@ class LettuceListCommands implements RedisListCommands { srcKey, dstKey); } + private static List toBytesList(KeyValue source) { + + List list = new ArrayList<>(2); + list.add(source.getKey()); + list.add(source.getValue()); + + return list; + } } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceServerCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceServerCommands.java index 68fe9fbda..d597078e6 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceServerCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceServerCommands.java @@ -188,7 +188,6 @@ class LettuceServerCommands implements RedisServerCommands { Assert.hasText(value, "Value must not be null or empty!"); connection.invokeStatus().just(RedisServerAsyncCommands::configSet, param, value); - } /* diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceStringCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceStringCommands.java index 1a563187c..e9d149027 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceStringCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceStringCommands.java @@ -110,7 +110,7 @@ class LettuceStringCommands implements RedisStringCommands { return connection.invoke() .from(RedisStringAsyncCommands::set, key, value, LettuceConverters.toSetArgs(expiration, option)) - .getOrElse(LettuceConverters.stringToBooleanConverter(), () -> false); + .orElse(LettuceConverters.stringToBooleanConverter(), false); } /*