diff --git a/src/main/java/org/springframework/data/redis/core/ListOperations.java b/src/main/java/org/springframework/data/redis/core/ListOperations.java index f5c8948385..853f8863cd 100644 --- a/src/main/java/org/springframework/data/redis/core/ListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ListOperations.java @@ -34,6 +34,7 @@ * @author Christoph Strobl * @author Mark Paluch * @author dengliming + * @author Lee Jaeheon */ public interface ListOperations { @@ -559,5 +560,35 @@ default V rightPopAndLeftPush(K sourceKey, K destinationKey, Duration timeout) { return rightPopAndLeftPush(sourceKey, destinationKey, TimeoutUtils.toSeconds(timeout), TimeUnit.SECONDS); } + /** + * Returns the first element from list at {@code key}. + * + * @implSpec + * The implementation in this interface returns the result of calling {@code index(key, 0)}. + * + * @param key must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + */ + @Nullable + default V getFirst(K key) { + return index(key, 0); + } + + /** + * Returns the last element from list at {@code key}. + * + * @implSpec + * If the result of calling {@code size(key)} is not null, The implementation in this interface returns the + * result of calling {@code index(key, size - 1)}. Otherwise, it returns null. + * + * @param key must not be {@literal null}. + * @return {@literal null} when used in pipeline / transaction. + */ + @Nullable + default V getLast(K key) { + Long size = size(key); + return size != null ? index(key, size - 1) : null; + } + RedisOperations getOperations(); } diff --git a/src/test/java/org/springframework/data/redis/core/DefaultListOperationsIntegrationIntegrationTests.java b/src/test/java/org/springframework/data/redis/core/DefaultListOperationsIntegrationIntegrationTests.java index 559551a4b0..1280bfd803 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultListOperationsIntegrationIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultListOperationsIntegrationIntegrationTests.java @@ -40,6 +40,7 @@ * @author Jennifer Hickey * @author Thomas Darimont * @author Christoph Strobl + * @author Lee Jaeheon * @param Key test * @param Value test */ @@ -376,4 +377,32 @@ void lastIndexOf() { assertThat(listOps.rightPush(key, v3)).isEqualTo(Long.valueOf(4)); assertThat(listOps.lastIndexOf(key, v1)).isEqualTo(2); } + + @ParameterizedRedisTest // GH-2937 + void getFirst() { + + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + V v3 = valueFactory.instance(); + + listOps.rightPush(key, v1); + listOps.rightPush(key, v2); + listOps.rightPush(key, v3); + assertThat(listOps.getFirst(key)).isEqualTo(v1); + } + + @ParameterizedRedisTest // GH-2937 + void getLast() { + + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + V v3 = valueFactory.instance(); + + listOps.rightPush(key, v1); + listOps.rightPush(key, v2); + listOps.rightPush(key, v3); + assertThat(listOps.getLast(key)).isEqualTo(v3); + } }