Closed
Description
With the addition of SequencedCollection
in Java 21, List
supports methods getFirst()
and getLast()
.
Similarly, I think 'ListOperations' in 'RedisTemplate' could be improved by introducing a method like getFirst(key)
instead of index(key, 0)
.
How about add methods like the code I wrote below?
/**
* 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;
}
Activity
mp911de commentedon Aug 13, 2024
Generally, we keep our
…Operations
interfaces aligned with the corresponding Redis commands. Adding these convenience methods would be neat. Feel free to submit a pull request if you like.getFirst(K key)
andgetLast(K key)
methods toListOperations
#2966Polishing.
Add `getFirst(K key)` and `getLast(K key)` methods to `ListOperations`.