DATAREDIS-288 - Improved support for collection parameters in ListOperations.
Added additional leftPushAll and rightPushAll method overloads that accept a Collection parameter. Previously we only accepted varargs which required users to convert collections to arrays. Based on David Liu's pull request: #100. Original pull request: #114.
This commit is contained in:
committed by
Christoph Strobl
parent
cf46d6e766
commit
d89773387a
@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author David Liu
|
||||
*/
|
||||
abstract class AbstractOperations<K, V> {
|
||||
|
||||
@@ -120,6 +121,17 @@ abstract class AbstractOperations<K, V> {
|
||||
return rawValues;
|
||||
}
|
||||
|
||||
byte[][] rawValues(Collection<V> values) {
|
||||
|
||||
byte[][] rawValues = new byte[values.size()][];
|
||||
int i = 0;
|
||||
for (V value : values) {
|
||||
rawValues[i++] = rawValue(value);
|
||||
}
|
||||
|
||||
return rawValues;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<HK> byte[] rawHashKey(HK hashKey) {
|
||||
Assert.notNull(hashKey, "non null hash key required");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -26,6 +27,8 @@ import org.springframework.util.CollectionUtils;
|
||||
* Default implementation of {@link ListOperations}.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author David Liu
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements ListOperations<K, V> {
|
||||
|
||||
@@ -245,4 +248,31 @@ class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements Li
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long rightPushAll(K key, Collection<V> values) {
|
||||
|
||||
final byte[] rawKey = rawKey(key);
|
||||
final byte[][] rawValues = rawValues(values);
|
||||
|
||||
return execute(new RedisCallback<Long>() {
|
||||
public Long doInRedis(RedisConnection connection) {
|
||||
return connection.rPush(rawKey, rawValues);
|
||||
}
|
||||
}, true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long leftPushAll(K key, Collection<V> values) {
|
||||
|
||||
final byte[] rawKey = rawKey(key);
|
||||
final byte[][] rawValues = rawValues(values);
|
||||
|
||||
return execute(new RedisCallback<Long>() {
|
||||
public Long doInRedis(RedisConnection connection) {
|
||||
return connection.lPush(rawKey, rawValues);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -22,6 +23,8 @@ import java.util.concurrent.TimeUnit;
|
||||
* Redis list specific operations.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author David Liu
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface ListOperations<K, V> {
|
||||
|
||||
@@ -35,6 +38,14 @@ public interface ListOperations<K, V> {
|
||||
|
||||
Long leftPushAll(K key, V... values);
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param values
|
||||
* @return
|
||||
* @since 1.5
|
||||
*/
|
||||
Long leftPushAll(K key, Collection<V> values);
|
||||
|
||||
Long leftPushIfPresent(K key, V value);
|
||||
|
||||
Long leftPush(K key, V pivot, V value);
|
||||
@@ -43,6 +54,14 @@ public interface ListOperations<K, V> {
|
||||
|
||||
Long rightPushAll(K key, V... values);
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param values
|
||||
* @return
|
||||
* @since 1.5
|
||||
*/
|
||||
Long rightPushAll(K key, Collection<V> values);
|
||||
|
||||
Long rightPushIfPresent(K key, V value);
|
||||
|
||||
Long rightPush(K key, V pivot, V value);
|
||||
|
||||
Reference in New Issue
Block a user