Skip to content

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

Closed
@violetbeach

Description

@violetbeach
Contributor

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

mp911de commented on Aug 13, 2024

@mp911de
Member

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.

added a commit that references this issue on Aug 20, 2024
added this to the 3.4 M1 (2024.1.0) milestone on Aug 20, 2024
self-assigned this
on Aug 20, 2024
added a commit that references this issue on Aug 21, 2024
a58bfd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @mp911de@christophstrobl@spring-projects-issues@violetbeach

    Issue actions

      Add getFirst(K key), getLast(K key) methods like Java 21 · Issue #2937 · spring-projects/spring-data-redis