diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java index 993c657ac..68027f187 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java @@ -187,6 +187,15 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.support.collections.RedisSet#randomElement() + */ + @Override + public E randomValue() { + return boundSetOps.randomMember(); + } + /* * (non-Javadoc) * @see java.util.AbstractCollection#add(java.lang.Object) diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java index d4687f855..fe7851f6c 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java @@ -277,6 +277,15 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore()); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.support.collections.RedisZSet#randomElement() + */ + @Override + public E randomValue() { + return boundZSetOps.randomMember(); + } + /* * (non-Javadoc) * @see org.springframework.data.redis.support.collections.RedisZSet#range(long, long) diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java b/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java index c08e3c664..84895dc25 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisSet.java @@ -82,8 +82,8 @@ public interface RedisSet extends RedisCollection, Set { Set diff(Collection> sets); /** - * Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in - * destination {@code destKey}. + * Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination + * {@code destKey}. * * @param set must not be {@literal null}. * @param destKey must not be {@literal null}. @@ -93,8 +93,8 @@ public interface RedisSet extends RedisCollection, Set { RedisSet intersectAndStore(RedisSet set, String destKey); /** - * Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store - * result in destination {@code destKey}. + * Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store result + * in destination {@code destKey}. * * @param sets must not be {@literal null}. * @param destKey must not be {@literal null}. @@ -125,6 +125,14 @@ public interface RedisSet extends RedisCollection, Set { */ RedisSet unionAndStore(Collection> sets, String destKey); + /** + * Get random element from the set. + * + * @return + * @since 2.6 + */ + E randomValue(); + /** * Create a new {@link RedisSet} by diffing this sorted set and {@link RedisSet} and store result in destination * {@code destKey}. diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java index 377b5660e..eaa430b2f 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java @@ -236,6 +236,14 @@ public interface RedisZSet extends RedisCollection, Set { */ RedisZSet unionAndStore(Collection> sets, String destKey); + /** + * Get random element from the set. + * + * @return + * @since 2.6 + */ + E randomValue(); + /** * Get elements between {@code start} and {@code end} from sorted set. * diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java index 11ede63c0..68937dade 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java @@ -302,4 +302,14 @@ public abstract class AbstractRedisSetIntegrationTests extends AbstractRedisC } cursor.close(); } + + @ParameterizedRedisTest // GH-2049 + void randMemberReturnsSomething() { + + Object[] valuesArray = new Object[]{getT(), getT(), getT()}; + + collection.addAll((List) Arrays.asList(valuesArray)); + + assertThat(set.randomValue()).isIn(valuesArray); + } } diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java index c648061de..da0e12ad4 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java @@ -21,6 +21,7 @@ import static org.assertj.core.api.Assumptions.*; import java.io.IOException; import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -856,4 +857,15 @@ public abstract class AbstractRedisZSetTestIntegration extends AbstractRedisC assertThat(zSet.addIfAbsent(t1, 1)).isTrue(); assertThat(zSet.addIfAbsent(t1, 1)).isFalse(); } + + @ParameterizedRedisTest // GH-2049 + @EnabledOnCommand("ZRANDMEMBER") + void randMemberReturnsSomething() { + + Object[] valuesArray = new Object[]{getT(), getT(), getT()}; + + collection.addAll((List) Arrays.asList(valuesArray)); + + assertThat(zSet.randomValue()).isIn(valuesArray); + } }