Skip to content

Add getFirst(K key) and getLast(K key) methods to ListOperations #2966

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author dengliming
* @author Lee Jaeheon
*/
public interface ListOperations<K, V> {

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

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

RedisOperations<K, V> getOperations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author Lee Jaeheon
* @param <K> Key test
* @param <V> Value test
*/
Expand Down Expand Up @@ -376,4 +377,32 @@ void lastIndexOf() {
assertThat(listOps.rightPush(key, v3)).isEqualTo(Long.valueOf(4));
assertThat(listOps.lastIndexOf(key, v1)).isEqualTo(2);
}

@ParameterizedRedisTest // GH-2937
void getFirst() {

K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
V v3 = valueFactory.instance();

listOps.rightPush(key, v1);
listOps.rightPush(key, v2);
listOps.rightPush(key, v3);
assertThat(listOps.getFirst(key)).isEqualTo(v1);
}

@ParameterizedRedisTest // GH-2937
void getLast() {

K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
V v3 = valueFactory.instance();

listOps.rightPush(key, v1);
listOps.rightPush(key, v2);
listOps.rightPush(key, v3);
assertThat(listOps.getLast(key)).isEqualTo(v3);
}
}
Loading