Add key/value null checking for RedissonMap object

pull/689/head
Nikita 8 years ago
parent c0f9177b87
commit 70be4685f0

@ -15,7 +15,6 @@
*/
package org.redisson;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.util.AbstractCollection;
@ -86,6 +85,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Integer> valueSizeAsync(K key) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
return commandExecutor.readAsync(getName(), codec, RedisCommands.HSTRLEN, getName(key), key);
}
@ -101,6 +104,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Boolean> containsKeyAsync(Object key) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
return commandExecutor.readAsync(getName(key), codec, RedisCommands.HEXISTS, getName(key), key);
}
@ -111,6 +118,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Boolean> containsValueAsync(Object value) {
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 4),
"local s = redis.call('hvals', KEYS[1]);" +
"for i = 1, #s, 1 do "
@ -232,6 +243,13 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<V> putIfAbsentAsync(K key, V value) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT,
"if redis.call('hsetnx', KEYS[1], ARGV[1], ARGV[2]) == 1 then "
+ "return nil "
@ -248,6 +266,13 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Boolean> fastPutIfAbsentAsync(K key, V value) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.writeAsync(getName(key), codec, RedisCommands.HSETNX, getName(key), key, value);
}
@ -258,6 +283,13 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Boolean> removeAsync(Object key, Object value) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REMOVE_VALUE,
"if redis.call('hget', KEYS[1], ARGV[1]) == ARGV[2] then "
+ "return redis.call('hdel', KEYS[1], ARGV[1]) "
@ -274,6 +306,17 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Boolean> replaceAsync(K key, V oldValue, V newValue) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (oldValue == null) {
throw new NullPointerException("map oldValue can't be null");
}
if (newValue == null) {
throw new NullPointerException("map newValue can't be null");
}
return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REPLACE_VALUE,
"if redis.call('hget', KEYS[1], ARGV[1]) == ARGV[2] then "
+ "redis.call('hset', KEYS[1], ARGV[1], ARGV[3]); "
@ -291,6 +334,13 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<V> replaceAsync(K key, V value) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REPLACE,
"if redis.call('hexists', KEYS[1], ARGV[1]) == 1 then "
+ "local v = redis.call('hget', KEYS[1], ARGV[1]); "
@ -304,6 +354,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<V> getAsync(K key) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
return commandExecutor.readAsync(getName(key), codec, RedisCommands.HGET, getName(key), key);
}
@ -313,6 +367,13 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<V> putAsync(K key, V value) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT,
"local v = redis.call('hget', KEYS[1], ARGV[1]); "
+ "redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); "
@ -323,6 +384,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<V> removeAsync(K key) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REMOVE,
"local v = redis.call('hget', KEYS[1], ARGV[1]); "
+ "redis.call('hdel', KEYS[1], ARGV[1]); "
@ -332,6 +397,13 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<Boolean> fastPutAsync(K key, V value) {
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
return commandExecutor.writeAsync(getName(key), codec, RedisCommands.HSET, getName(key), key, value);
}
@ -370,14 +442,17 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
@Override
public RFuture<V> addAndGetAsync(K key, Number value) {
try {
byte[] keyState = codec.getMapKeyEncoder().encode(key);
return commandExecutor.writeAsync(getName(key), StringCodec.INSTANCE,
new RedisCommand<Object>("HINCRBYFLOAT", new NumberConvertor(value.getClass())),
getName(key), keyState, new BigDecimal(value.toString()).toPlainString());
} catch (IOException e) {
throw new IllegalArgumentException(e);
if (key == null) {
throw new NullPointerException("map key can't be null");
}
if (value == null) {
throw new NullPointerException("map value can't be null");
}
byte[] keyState = encodeMapKey(key);
return commandExecutor.writeAsync(getName(key), StringCodec.INSTANCE,
new RedisCommand<Object>("HINCRBYFLOAT", new NumberConvertor(value.getClass())),
getName(key), keyState, new BigDecimal(value.toString()).toPlainString());
}
@Override

@ -291,21 +291,16 @@ public class RedissonMapTest extends BaseTest {
assertThat(counter).isEqualTo(size);
}
@Test
public void testNull() {
@Test(expected = NullPointerException.class)
public void testNullValue() {
Map<Integer, String> map = redisson.getMap("simple12");
map.put(1, null);
map.put(2, null);
map.put(3, "43");
assertThat(map.size()).isEqualTo(3);
}
String val = map.get(2);
assertThat(val).isNull();
String val2 = map.get(1);
assertThat(val2).isNull();
String val3 = map.get(3);
assertThat(val3).isEqualTo("43");
@Test(expected = NullPointerException.class)
public void testNullKey() {
Map<Integer, String> map = redisson.getMap("simple12");
map.put(null, "1");
}
@Test

Loading…
Cancel
Save