Polishing.

Add getFirst/getLast to Reactive and Bound Operations.

Simplify getLast implementation.

Reorder methods, tweak Javadoc, add since tags.

Original pull request: #2966
See #2937
This commit is contained in:
Mark Paluch
2024-08-20 15:16:53 +02:00
parent a58bfd83c3
commit bd20c6d6f7
6 changed files with 136 additions and 85 deletions

View File

@@ -212,6 +212,24 @@ public interface BoundListOperations<K, V> extends BoundKeyOperations<K> {
@Nullable
Long remove(long count, Object value);
/**
* Returns the first element from the list at the bound {@code key}.
*
* @return {@literal null} when used in pipeline / transaction.
* @since 3.4
*/
@Nullable
V getFirst();
/**
* Returns the last element from the list at the bound {@code key}.
*
* @return {@literal null} when used in pipeline / transaction.
* @since 3.4
*/
@Nullable
V getLast();
/**
* Get element at {@code index} form list at the bound key.
*

View File

@@ -368,6 +368,30 @@ public interface ListOperations<K, V> {
@Nullable
Long remove(K key, long count, Object value);
/**
* Returns the first element from the list at {@code key}.
*
* @param key must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 3.4
*/
@Nullable
default V getFirst(K key) {
return index(key, 0);
}
/**
* Returns the last element from the list at {@code key}.
*
* @param key must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 3.4
*/
@Nullable
default V getLast(K key) {
return index(key, -1);
}
/**
* Get element at {@code index} form list at {@code key}.
*
@@ -526,7 +550,8 @@ public interface ListOperations<K, V> {
V rightPopAndLeftPush(K sourceKey, K destinationKey);
/**
* Remove the last element from list at {@code sourceKey}, append it to {@code destinationKey} and return its value.<br>
* Remove the last element from list at {@code sourceKey}, append it to {@code destinationKey} and return its
* value.<br>
* <b>Blocks connection</b> until element available or {@code timeout} reached.
*
* @param sourceKey must not be {@literal null}.
@@ -540,7 +565,8 @@ public interface ListOperations<K, V> {
V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
/**
* Remove the last element from list at {@code sourceKey}, append it to {@code destinationKey} and return its value.<br>
* Remove the last element from list at {@code sourceKey}, append it to {@code destinationKey} and return its
* value.<br>
* <b>Blocks connection</b> until element available or {@code timeout} reached.
*
* @param sourceKey must not be {@literal null}.
@@ -560,35 +586,5 @@ public interface ListOperations<K, V> {
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();
}

View File

@@ -26,6 +26,7 @@ import java.util.Collection;
import org.springframework.data.redis.core.ListOperations.MoveFrom;
import org.springframework.data.redis.core.ListOperations.MoveTo;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -276,6 +277,30 @@ public interface ReactiveListOperations<K, V> {
*/
Mono<Long> remove(K key, long count, Object value);
/**
* Returns the first element from the list at {@code key}.
*
* @param key must not be {@literal null}.
* @return
* @since 3.4
*/
@Nullable
default Mono<V> getFirst(K key) {
return index(key, 0);
}
/**
* Returns the last element from the list at {@code key}.
*
* @param key must not be {@literal null}.
* @return
* @since 3.4
*/
@Nullable
default Mono<V> getLast(K key) {
return index(key, -1);
}
/**
* Get element at {@code index} form list at {@code key}.
*

View File

@@ -349,7 +349,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
@Nullable
public E peek() {
return listOps.index(0);
return listOps.getFirst();
}
@Override
@@ -426,7 +426,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
@Nullable
public E peekLast() {
return listOps.index(-1);
return listOps.getLast();
}
@Override