DATAREDIS-872 - Polishing.

Create unit tests for RedisAtomic counter initialization. Refactor setIfAbsent(…) method to initializeIfAbsent() to not expose additional API methods.

Original pull request: #367.
This commit is contained in:
Mark Paluch
2018-10-19 12:27:01 +02:00
parent 2d4fbc9aa0
commit acd8b6bd4a
6 changed files with 171 additions and 36 deletions

View File

@@ -91,7 +91,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
@@ -135,12 +135,16 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
}
private void initializeIfAbsent() {
operations.setIfAbsent(key, (double) 0);
}
/**
* Get the current value.
*
@@ -165,16 +169,6 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
operations.set(key, newValue);
}
/**
* Sets to the given value, only if {@code key} does not exist.
*
* @param newValue the new value.
* @return true if successful. False return indicates that {@code key} already existed.
*/
public Boolean setIfAbsent(double newValue) {
return operations.setIfAbsent(key, newValue);
}
/**
* Set to the given value and return the old value.
*

View File

@@ -114,7 +114,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
@@ -133,12 +133,16 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
}
private void initializeIfAbsent() {
operations.setIfAbsent(key, 0);
}
/**
* Get the current value.
*
@@ -163,16 +167,6 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
operations.set(key, newValue);
}
/**
* Sets to the given value, only if {@code key} does not exist.
*
* @param newValue the new value.
* @return true if successful. False return indicates that {@code key} already existed.
*/
public Boolean setIfAbsent(int newValue) {
return operations.setIfAbsent(key, newValue);
}
/**
* Set to the given value and return the old value.
*

View File

@@ -92,7 +92,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
@@ -140,12 +140,16 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
}
private void initializeIfAbsent() {
operations.setIfAbsent(key, (long) 0);
}
/**
* Get the current value.
*
@@ -170,16 +174,6 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
operations.set(key, newValue);
}
/**
* Sets to the given value, only if {@code key} does not exist.
*
* @param newValue the new value.
* @return true if successful. False return indicates that {@code key} already existed.
*/
public Boolean setIfAbsent(long newValue) {
return operations.setIfAbsent(key, newValue);
}
/**
* Set to the given value and return the old value.
*