Skip to content

Commit a58bfd8

Browse files
violetbeachmp911de
authored andcommitted
Add getFirst(K key) and getLast(K key) methods to ListOperations.
Original pull request: #2966 Closes #2937
1 parent e56e8c5 commit a58bfd8

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/main/java/org/springframework/data/redis/core/ListOperations.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Christoph Strobl
3535
* @author Mark Paluch
3636
* @author dengliming
37+
* @author Lee Jaeheon
3738
*/
3839
public interface ListOperations<K, V> {
3940

@@ -559,5 +560,35 @@ default V rightPopAndLeftPush(K sourceKey, K destinationKey, Duration timeout) {
559560
return rightPopAndLeftPush(sourceKey, destinationKey, TimeoutUtils.toSeconds(timeout), TimeUnit.SECONDS);
560561
}
561562

563+
/**
564+
* Returns the first element from list at {@code key}.
565+
*
566+
* @implSpec
567+
* The implementation in this interface returns the result of calling {@code index(key, 0)}.
568+
*
569+
* @param key must not be {@literal null}.
570+
* @return {@literal null} when used in pipeline / transaction.
571+
*/
572+
@Nullable
573+
default V getFirst(K key) {
574+
return index(key, 0);
575+
}
576+
577+
/**
578+
* Returns the last element from list at {@code key}.
579+
*
580+
* @implSpec
581+
* If the result of calling {@code size(key)} is not null, The implementation in this interface returns the
582+
* result of calling {@code index(key, size - 1)}. Otherwise, it returns null.
583+
*
584+
* @param key must not be {@literal null}.
585+
* @return {@literal null} when used in pipeline / transaction.
586+
*/
587+
@Nullable
588+
default V getLast(K key) {
589+
Long size = size(key);
590+
return size != null ? index(key, size - 1) : null;
591+
}
592+
562593
RedisOperations<K, V> getOperations();
563594
}

src/test/java/org/springframework/data/redis/core/DefaultListOperationsIntegrationIntegrationTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Jennifer Hickey
4141
* @author Thomas Darimont
4242
* @author Christoph Strobl
43+
* @author Lee Jaeheon
4344
* @param <K> Key test
4445
* @param <V> Value test
4546
*/
@@ -376,4 +377,32 @@ void lastIndexOf() {
376377
assertThat(listOps.rightPush(key, v3)).isEqualTo(Long.valueOf(4));
377378
assertThat(listOps.lastIndexOf(key, v1)).isEqualTo(2);
378379
}
380+
381+
@ParameterizedRedisTest // GH-2937
382+
void getFirst() {
383+
384+
K key = keyFactory.instance();
385+
V v1 = valueFactory.instance();
386+
V v2 = valueFactory.instance();
387+
V v3 = valueFactory.instance();
388+
389+
listOps.rightPush(key, v1);
390+
listOps.rightPush(key, v2);
391+
listOps.rightPush(key, v3);
392+
assertThat(listOps.getFirst(key)).isEqualTo(v1);
393+
}
394+
395+
@ParameterizedRedisTest // GH-2937
396+
void getLast() {
397+
398+
K key = keyFactory.instance();
399+
V v1 = valueFactory.instance();
400+
V v2 = valueFactory.instance();
401+
V v3 = valueFactory.instance();
402+
403+
listOps.rightPush(key, v1);
404+
listOps.rightPush(key, v2);
405+
listOps.rightPush(key, v3);
406+
assertThat(listOps.getLast(key)).isEqualTo(v3);
407+
}
379408
}

0 commit comments

Comments
 (0)