DATAREDIS-746 - Polishing.

Move delegates to default methods.

Original Pull Request: #314
This commit is contained in:
Christoph Strobl
2018-03-05 09:55:24 +01:00
parent 56a01625d5
commit 468345115a
15 changed files with 56 additions and 154 deletions

View File

@@ -1295,15 +1295,6 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
return convertAndReturn(delegate.zIncrBy(key, increment, value), identityConverter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return convertAndReturn(delegate.zInterStore(destKey, aggregate, weights, sets), identityConverter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])
@@ -1565,14 +1556,6 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
return convertAndReturn(delegate.zScore(key, value), identityConverter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return convertAndReturn(delegate.zUnionStore(destKey, aggregate, weights, sets), identityConverter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])

View File

@@ -1375,7 +1375,7 @@ public interface ReactiveZSetCommands {
* @since 2.1
*/
public ZUnionStoreCommand applyWeights(Weights weights) {
return new ZUnionStoreCommand(getKey(), sourceKeys, weights.toList(), aggregateFunction);
return applyWeights(weights.toList());
}
/**
@@ -1575,7 +1575,7 @@ public interface ReactiveZSetCommands {
* @since 2.1
*/
public ZInterStoreCommand applyWeights(Weights weights) {
return new ZInterStoreCommand(getKey(), sourceKeys, weights.toList(), aggregateFunction);
return applyWeights(weights.toList());
}
/**

View File

@@ -16,9 +16,11 @@
package org.springframework.data.redis.connection;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
@@ -51,13 +53,14 @@ public interface RedisZSetCommands {
* element in every input sorted set is multiplied by this factor before being passed to the aggregation function.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.1
*/
class Weights {
private final double[] weights;
private final List<Double> weights;
Weights(double[] weights) {
private Weights(List<Double> weights) {
this.weights = weights;
}
@@ -70,7 +73,7 @@ public interface RedisZSetCommands {
public static Weights of(int... weights) {
Assert.notNull(weights, "Weights must not be null!");
return new Weights(Arrays.stream(weights).mapToDouble(value -> value).toArray());
return new Weights(Arrays.stream(weights).mapToDouble(value -> value).boxed().collect(Collectors.toList()));
}
/**
@@ -83,7 +86,7 @@ public interface RedisZSetCommands {
Assert.notNull(weights, "Weights must not be null!");
return new Weights(Arrays.copyOf(weights, weights.length));
return new Weights(DoubleStream.of(weights).boxed().collect(Collectors.toList()));
}
/**
@@ -96,7 +99,7 @@ public interface RedisZSetCommands {
Assert.isTrue(count >= 0, "Count of input sorted sets must be greater or equal to zero!");
return new Weights(IntStream.range(0, count).mapToDouble(value -> 1).toArray());
return new Weights(IntStream.range(0, count).mapToDouble(value -> 1).boxed().collect(Collectors.toList()));
}
/**
@@ -120,13 +123,13 @@ public interface RedisZSetCommands {
}
/**
* Creates a new {@link Weights} object that contains all weights with {@link DoubleUnaryOperator} applied.
* Creates a new {@link Weights} object that contains all weights with {@link Function} applied.
*
* @param operator operator function.
* @return the new {@link Weights} with {@link DoubleUnaryOperator} applied.
*/
public Weights apply(DoubleUnaryOperator operator) {
return new Weights(DoubleStream.of(weights).map(operator).toArray());
public Weights apply(Function<Double, Double> operator) {
return new Weights(weights.stream().map(operator).collect(Collectors.toList()));
}
/**
@@ -137,26 +140,21 @@ public interface RedisZSetCommands {
* @throws IndexOutOfBoundsException if the index is out of range
*/
public double getWeight(int index) {
if (index > size() || index < 0) {
throw new IndexOutOfBoundsException("No such weight");
}
return weights[index];
return weights.get(index);
}
/**
* @return number of weights.
*/
public int size() {
return weights.length;
return weights.size();
}
/**
* @return an array containing all of the weights in this list in proper sequence (from first to last element).
*/
public double[] toArray() {
return Arrays.copyOf(weights, weights.length);
return weights.stream().mapToDouble(Double::doubleValue).toArray();
}
/**
@@ -164,7 +162,7 @@ public interface RedisZSetCommands {
* element).
*/
public List<Double> toList() {
return Arrays.stream(weights).boxed().collect(Collectors.toList());
return Collections.unmodifiableList(weights);
}
}
@@ -821,7 +819,9 @@ public interface RedisZSetCommands {
* @see <a href="http://redis.io/commands/zunionstore">Redis Documentation: ZUNIONSTORE</a>
*/
@Nullable
Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets);
default Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zUnionStore(destKey, aggregate, Weights.of(weights), sets);
}
/**
* Union sorted {@code sets} and store result in destination {@code key}.
@@ -859,7 +859,9 @@ public interface RedisZSetCommands {
* @see <a href="http://redis.io/commands/zinterstore">Redis Documentation: ZINTERSTORE</a>
*/
@Nullable
Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets);
default Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zInterStore(destKey, aggregate, Weights.of(weights), sets);
}
/**
* Intersect sorted {@code sets} and store result in destination {@code key}.

View File

@@ -625,15 +625,6 @@ class JedisClusterZSetCommands implements RedisZSetCommands {
throw new InvalidDataAccessApiUsageException("ZUNIONSTORE can only be executed when all keys map to the same slot");
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zUnionStore(destKey, aggregate, Weights.of(weights), sets);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])
@@ -689,14 +680,6 @@ class JedisClusterZSetCommands implements RedisZSetCommands {
throw new InvalidDataAccessApiUsageException("ZINTERSTORE can only be executed when all keys map to the same slot");
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zInterStore(destKey, aggregate, Weights.of(weights), sets);
}
/*
* (non-Javadoc)

View File

@@ -579,15 +579,6 @@ class JedisZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zUnionStore(destKey, aggregate, Weights.of(weights), sets);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])
@@ -646,15 +637,6 @@ class JedisZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zInterStore(destKey, aggregate, Weights.of(weights), sets);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])

View File

@@ -569,15 +569,6 @@ class LettuceZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zUnionStore(destKey, aggregate, Weights.of(weights), sets);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])
@@ -634,15 +625,6 @@ class LettuceZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
return zInterStore(destKey, aggregate, Weights.of(weights), sets);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights, byte[][])

View File

@@ -32,7 +32,7 @@ import org.springframework.lang.Nullable;
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author Wongoo
* @author Wongoo (望哥)
*/
public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {

View File

@@ -32,7 +32,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author wongoo
* @author Wongoo (望哥)
*/
class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundZSetOperations<K, V> {

View File

@@ -373,18 +373,6 @@ class DefaultReactiveZSetOperations<K, V> implements ReactiveZSetOperations<K, V
.flatMap(serialized -> connection.zUnionStore(rawKey(destKey), serialized)));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveZSetOperations#unionAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate)
*/
@Override
public Mono<Long> unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
Assert.notNull(otherKeys, "Other keys must not be null!");
return unionAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveZSetOperations#unionAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights)
@@ -435,18 +423,6 @@ class DefaultReactiveZSetOperations<K, V> implements ReactiveZSetOperations<K, V
.flatMap(serialized -> connection.zInterStore(rawKey(destKey), serialized)));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveZSetOperations#intersectAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate)
*/
@Override
public Mono<Long> intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
Assert.notNull(otherKeys, "Other keys must not be null!");
return intersectAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveZSetOperations#intersectAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights)

View File

@@ -33,7 +33,7 @@ import org.springframework.data.redis.connection.RedisZSetCommands.Weights;
* @author Thomas Darimont
* @author David Liu
* @author Mark Paluch
* @author wongoo
* @author Wongoo (望哥)
*/
class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZSetOperations<K, V> {
@@ -99,15 +99,6 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
return execute(connection -> connection.zInterStore(rawDestKey, rawKeys), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ZSetOperations#intersectAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate)
*/
@Override
public Long intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
return intersectAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ZSetOperations#intersectAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights)
@@ -433,15 +424,6 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
return execute(connection -> connection.zUnionStore(rawDestKey, rawKeys), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ZSetOperations#unionAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate)
*/
@Override
public Long unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
return unionAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ZSetOperations#unionAndStore(java.lang.Object, java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights)

View File

@@ -310,7 +310,9 @@ public interface ReactiveZSetOperations<K, V> {
* @since 2.1
* @see <a href="http://redis.io/commands/zunionstore">Redis Documentation: ZUNIONSTORE</a>
*/
Mono<Long> unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate);
default Mono<Long> unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
return unionAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/**
* Union sorted sets at {@code key} and {@code otherKeys} and store result in destination {@code destKey}.
@@ -359,7 +361,9 @@ public interface ReactiveZSetOperations<K, V> {
* @since 2.1
* @see <a href="http://redis.io/commands/zinterstore">Redis Documentation: ZINTERSTORE</a>
*/
Mono<Long> intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate);
default Mono<Long> intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
return intersectAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/**
* Intersect sorted sets at {@code key} and {@code otherKeys} and store result in destination {@code destKey}.

View File

@@ -32,7 +32,7 @@ import org.springframework.lang.Nullable;
* @author Christoph Strobl
* @author Mark Paluch
* @author Rosty Kerei
* @author wongoo
* @author Wongoo (望哥)
*/
public interface ZSetOperations<K, V> {
@@ -378,7 +378,9 @@ public interface ZSetOperations<K, V> {
* @see <a href="http://redis.io/commands/zunionstore">Redis Documentation: ZUNIONSTORE</a>
*/
@Nullable
Long unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate);
default Long unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
return unionAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/**
* Union sorted sets at {@code key} and {@code otherKeys} and store result in destination {@code destKey}.
@@ -431,7 +433,9 @@ public interface ZSetOperations<K, V> {
* @see <a href="http://redis.io/commands/zinterstore">Redis Documentation: ZINTERSTORE</a>
*/
@Nullable
Long intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate);
default Long intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate) {
return intersectAndStore(key, otherKeys, destKey, aggregate, Weights.fromSetCount(1 + otherKeys.size()));
}
/**
* Intersect sorted sets at {@code key} and {@code otherKeys} and store result in destination {@code destKey}.