Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
violetbeach opened this issue Jul 8, 2024 · 1 comment
Closed

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

violetbeach opened this issue Jul 8, 2024 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@violetbeach
Copy link
Contributor

violetbeach commented Jul 8, 2024

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;
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 8, 2024
@christophstrobl christophstrobl added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 15, 2024
@mp911de
Copy link
Member

mp911de commented 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.

mp911de added a commit that referenced this issue Aug 20, 2024
Add getFirst/getLast to Reactive and Bound Operations.

Simplify getLast implementation.

Reorder methods, tweak Javadoc, add since tags.

Original pull request: #2966
See #2937
@mp911de mp911de added this to the 3.4 M1 (2024.1.0) milestone Aug 20, 2024
@mp911de mp911de self-assigned this Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants