Skip to content

Add getFirst(K key), getLast(K key) methods like Java 21 #2937

Closed
@violetbeach

Description

@violetbeach

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;
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions