DATAREDIS-874 - Polishing.

Reformat code. Add since tags. Extend tests.

Align Javadoc tonality across RedisAtomic… types.

Original pull request: #362.
This commit is contained in:
Mark Paluch
2018-10-16 12:01:29 +02:00
parent f2107ef81b
commit 148b1ae582
6 changed files with 440 additions and 138 deletions

View File

@@ -18,8 +18,8 @@ package org.springframework.data.redis.support.atomic;
import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.DoubleUnaryOperator;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleUnaryOperator;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.redis.connection.DataType;
@@ -145,7 +145,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
}
/**
* Gets the current value.
* Get the current value.
*
* @return the current value.
*/
@@ -160,7 +160,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
}
/**
* Sets to the given value.
* Set to the given value.
*
* @param newValue the new value.
*/
@@ -169,7 +169,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
}
/**
* Atomically sets to the given value and returns the old value.
* Set to the given value and return the old value.
*
* @param newValue the new value.
* @return the previous value.
@@ -182,7 +182,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
}
/**
* Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
* Atomically set the value to the given updated value if the current value {@code ==} the expected value.
*
* @param expect the expected value.
* @param update the new value.
@@ -194,134 +194,154 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
}
/**
* Atomically increments by one the current value.
* Atomically increment by one the current value.
*
* @return the previous value
* @return the previous value.
*/
public double getAndIncrement() {
return incrementAndGet() - 1.0;
}
/**
* Atomically decrements by one the current value.
* Atomically decrement by one the current value.
*
* @return the previous value
* @return the previous value.
*/
public double getAndDecrement() {
return decrementAndGet() + 1.0;
}
/**
* Atomically adds the given value to the current value.
* Atomically add the given value to current value.
*
* @param delta the value to add
* @return the previous value
* @param delta the value to add.
* @return the previous value.
*/
public double getAndAdd(double delta) {
return addAndGet(delta) - delta;
}
/**
* Atomically update the current value using the given update function.
* Atomically update the current value using the given {@link DoubleUnaryOperator update function}.
*
* @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects),
* because it will be applied several times if update attempts fail due to concurrent calls.
* because it will be applied several times if update attempts fail due to concurrent calls. Must not be
* {@literal null}.
* @return the previous value.
* @since 2.2
*/
public double getAndUpdate(DoubleUnaryOperator updateFunction) {
Assert.notNull(updateFunction, "Update function must not be null!");
double previousValue, newValue;
do {
previousValue = get();
newValue = updateFunction.applyAsDouble(previousValue);
} while (!compareAndSet(previousValue, newValue));
return previousValue;
}
/**
* Atomically update the current value using the given accumulator function.
* The new value is calculated by applying the accumulator function to the current value and the
* given `updateValue`.
* Atomically update the current value using the given {@link DoubleBinaryOperator accumulator function}. The new
* value is calculated by applying the accumulator function to the current value and the given {@code updateValue}.
*
* @param updateValue the value which will be passed into the accumulator function.
* @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side
* effects), because it will be applied several times if update attempts fail due to
* concurrent calls.
* effects), because it will be applied several times if update attempts fail due to concurrent calls. Must
* not be {@literal null}.
* @return the previous value.
* @since 2.2
*/
public double getAndAccumulate(double updateValue, DoubleBinaryOperator accumulatorFunction) {
Assert.notNull(accumulatorFunction, "Accumulator function must not be null!");
double previousValue, newValue;
do {
previousValue = get();
newValue = accumulatorFunction.applyAsDouble(previousValue, updateValue);
} while (!compareAndSet(previousValue, newValue));
return previousValue;
}
/**
* Atomically increments by one the current value.
* Atomically increment by one the current value.
*
* @return the updated value
* @return the updated value.
*/
public double incrementAndGet() {
return operations.increment(key, 1.0);
}
/**
* Atomically decrements by one the current value.
* Atomically decrement by one the current value.
*
* @return the updated value
* @return the updated value.
*/
public double decrementAndGet() {
return operations.increment(key, -1.0);
}
/**
* Atomically adds the given value to the current value.
* Atomically add the given value to current value.
*
* @param delta the value to add
* @return the updated value
* @param delta the value to add.
* @return the updated value.
*/
public double addAndGet(double delta) {
return operations.increment(key, delta);
}
/**
* Atomically update the current value using the given update function.
* Atomically update the current value using the given {@link DoubleUnaryOperator update function}.
*
* @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects),
* because it will be applied several times if update attempts fail due to concurrent calls.
* because it will be applied several times if update attempts fail due to concurrent calls. Must not be
* {@literal null}.
* @return the updated value.
* @since 2.2
*/
public double updateAndGet(DoubleUnaryOperator updateFunction) {
Assert.notNull(updateFunction, "Update function must not be null!");
double previousValue, newValue;
do {
previousValue = get();
newValue = updateFunction.applyAsDouble(previousValue);
} while (!compareAndSet(previousValue, newValue));
return newValue;
}
/**
* Atomically update the current value using the given accumulator function.
* The new value is calculated by applying the accumulator function to the current value and the
* given `updateValue`.
* Atomically update the current value using the given {@link DoubleBinaryOperator accumulator function}. The new
* value is calculated by applying the accumulator function to the current value and the given {@code updateValue}.
*
* @param updateValue the value which will be passed into the accumulator function.
* @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side
* effects), because it will be applied several times if update attempts fail due to
* concurrent calls.
* effects), because it will be applied several times if update attempts fail due to concurrent calls. Must
* not be {@literal null}.
* @return the updated value.
* @since 2.2
*/
public double accumulateAndGet(double updateValue, DoubleBinaryOperator accumulatorFunction) {
Assert.notNull(accumulatorFunction, "Accumulator function must not be null!");
double previousValue, newValue;
do {
previousValue = get();
newValue = accumulatorFunction.applyAsDouble(previousValue, updateValue);
} while (!compareAndSet(previousValue, newValue));
return newValue;
}

View File

@@ -167,7 +167,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
}
/**
* Set to the give value and return the old value.
* Set to the given value and return the old value.
*
* @param newValue the new value.
* @return the previous value.
@@ -180,7 +180,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
}
/**
* Atomically set the value to the given updated value if the current value <tt>==</tt> the expected value.
* Atomically set the value to the given updated value if the current value {@code ==} the expected value.
*
* @param expect the expected value.
* @param update the new value.
@@ -220,40 +220,50 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
}
/**
* Atomically update the current value using the given update function.
* Atomically update the current value using the given {@link IntUnaryOperator update function}.
*
* @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects),
* because it will be applied several times if update attempts fail due to concurrent calls.
* because it will be applied several times if update attempts fail due to concurrent calls. Must not be
* {@literal null}.
* @return the previous value.
* @since 2.2
*/
public int getAndUpdate(IntUnaryOperator updateFunction) {
Assert.notNull(updateFunction, "Update function must not be null!");
int previousValue, newValue;
do {
previousValue = get();
newValue = updateFunction.applyAsInt(previousValue);
} while (!compareAndSet(previousValue, newValue));
return previousValue;
}
/**
* Atomically update the current value using the given accumulator function.
* The new value is calculated by applying the accumulator function to the current value and the
* given `updateValue`.
* Atomically update the current value using the given {@link IntBinaryOperator accumulator function}. The new value
* is calculated by applying the accumulator function to the current value and the given {@code updateValue}.
*
* @param updateValue the value which will be passed into the accumulator function.
* @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side
* effects), because it will be applied several times if update attempts fail due to
* concurrent calls.
* effects), because it will be applied several times if update attempts fail due to concurrent calls. Must
* not be {@literal null}.
* @return the previous value.
* @since 2.2
*/
public int getAndAccumulate(int updateValue, IntBinaryOperator accumulatorFunction) {
Assert.notNull(accumulatorFunction, "Accumulator function must not be null!");
int previousValue, newValue;
do {
previousValue = get();
newValue = accumulatorFunction.applyAsInt(previousValue, updateValue);
} while (!compareAndSet(previousValue, newValue));
return previousValue;
}
@@ -286,40 +296,50 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
}
/**
* Atomically update the current value using the given update function.
* Atomically update the current value using the given {@link IntUnaryOperator update function}.
*
* @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects),
* because it will be applied several times if update attempts fail due to concurrent calls.
* because it will be applied several times if update attempts fail due to concurrent calls. Must not be
* {@literal null}.
* @return the updated value.
* @since 2.2
*/
public int updateAndGet(IntUnaryOperator updateFunction) {
Assert.notNull(updateFunction, "Update function must not be null!");
int previousValue, newValue;
do {
previousValue = get();
newValue = updateFunction.applyAsInt(previousValue);
} while (!compareAndSet(previousValue, newValue));
return newValue;
}
/**
* Atomically update the current value using the given accumulator function.
* The new value is calculated by applying the accumulator function to the current value and the
* given `updateValue`.
* Atomically update the current value using the given {@link IntBinaryOperator accumulator function}. The new value
* is calculated by applying the accumulator function to the current value and the given {@code updateValue}.
*
* @param updateValue the value which will be passed into the accumulator function.
* @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side
* effects), because it will be applied several times if update attempts fail due to
* concurrent calls.
* effects), because it will be applied several times if update attempts fail due to concurrent calls. Must
* not be {@literal null}.
* @return the updated value.
* @since 2.2
*/
public int accumulateAndGet(int updateValue, IntBinaryOperator accumulatorFunction) {
Assert.notNull(accumulatorFunction, "Accumulator function must not be null!");
int previousValue, newValue;
do {
previousValue = get();
newValue = accumulatorFunction.applyAsInt(previousValue, updateValue);
} while (!compareAndSet(previousValue, newValue));
return newValue;
}

View File

@@ -150,7 +150,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Gets the current value.
* Get the current value.
*
* @return the current value.
*/
@@ -165,16 +165,16 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Sets to the given value.
* Set to the given value.
*
* @param newValue the new value
* @param newValue the new value.
*/
public void set(long newValue) {
operations.set(key, newValue);
}
/**
* Atomically sets to the given value and returns the old value.
* Set to the given value and return the old value.
*
* @param newValue the new value.
* @return the previous value.
@@ -187,7 +187,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
* Atomically set the value to the given updated value if the current value {@code ==} the expected value.
*
* @param expect the expected value.
* @param update the new value.
@@ -199,7 +199,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically increments by one the current value.
* Atomically increment by one the current value.
*
* @return the previous value.
*/
@@ -208,7 +208,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically decrements by one the current value.
* Atomically decrement by one the current value.
*
* @return the previous value.
*/
@@ -217,7 +217,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically adds the given value to the current value.
* Atomically add the given value to current value.
*
* @param delta the value to add.
* @return the previous value.
@@ -227,45 +227,53 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically update the current value using the given update function.
* Atomically update the current value using the given {@link LongUnaryOperator update function}.
*
* @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects),
* because it will be applied several times if update attempts fail due to concurrent calls.
* because it will be applied several times if update attempts fail due to concurrent calls.
* @return the previous value.
* @since 2.2
*/
public long getAndUpdate(LongUnaryOperator updateFunction) {
Assert.notNull(updateFunction, "Update function must not be null!");
long previousValue, newValue;
do {
previousValue = get();
newValue = updateFunction.applyAsLong(previousValue);
} while (!compareAndSet(previousValue, newValue));
return previousValue;
}
/**
* Atomically update the current value using the given accumulator function.
* The new value is calculated by applying the accumulator function to the current value and the
* given `updateValue`.
* Atomically update the current value using the given {@link LongBinaryOperator accumulator function}. The new value
* is calculated by applying the accumulator function to the current value and the given {@code updateValue}.
*
* @param updateValue the value which will be passed into the accumulator function.
* @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side
* effects), because it will be applied several times if update attempts fail due to
* concurrent calls.
* effects), because it will be applied several times if update attempts fail due to concurrent calls.
* @return the previous value.
* @since 2.2
*/
public long getAndAccumulate(long updateValue, LongBinaryOperator accumulatorFunction) {
Assert.notNull(accumulatorFunction, "Accumulator function must not be null!");
long previousValue, newValue;
do {
previousValue = get();
newValue = accumulatorFunction.applyAsLong(previousValue, updateValue);
} while (!compareAndSet(previousValue, newValue));
return previousValue;
}
/**
* Atomically increments by one the current value.
* Atomically increment by one the current value.
*
* @return the updated value.
*/
@@ -274,7 +282,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically decrements by one the current value.
* Atomically decrement by one the current value.
*
* @return the updated value.
*/
@@ -283,7 +291,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically adds the given value to the current value.
* Atomically add the given value to current value.
*
* @param delta the value to add.
* @return the updated value.
@@ -293,40 +301,48 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
/**
* Atomically update the current value using the given update function.
* Atomically update the current value using the given {@link LongUnaryOperator update function}.
*
* @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects),
* because it will be applied several times if update attempts fail due to concurrent calls.
* because it will be applied several times if update attempts fail due to concurrent calls.
* @return the updated value.
* @since 2.2
*/
public long updateAndGet(LongUnaryOperator updateFunction) {
Assert.notNull(updateFunction, "Update function must not be null!");
long previousValue, newValue;
do {
previousValue = get();
newValue = updateFunction.applyAsLong(previousValue);
} while (!compareAndSet(previousValue, newValue));
return newValue;
}
/**
* Atomically update the current value using the given accumulator function.
* The new value is calculated by applying the accumulator function to the current value and the
* given `updateValue`.
* Atomically update the current value using the given {@link LongBinaryOperator accumulator function}. The new value
* is calculated by applying the accumulator function to the current value and the given {@code updateValue}.
*
* @param updateValue the value which will be passed into the accumulator function.
* @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side
* effects), because it will be applied several times if update attempts fail due to
* concurrent calls.
* effects), because it will be applied several times if update attempts fail due to concurrent calls.
* @return the updated value.
* @since 2.2
*/
public long accumulateAndGet(long updateValue, LongBinaryOperator accumulatorFunction) {
Assert.notNull(accumulatorFunction, "Accumulator function must not be null!");
long previousValue, newValue;
do {
previousValue = get();
newValue = accumulatorFunction.applyAsLong(previousValue, updateValue);
} while (!compareAndSet(previousValue, newValue));
return newValue;
}