diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java index 7151030b4..70429b4d5 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java @@ -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. * diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java index 97a877626..91e8850c0 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java @@ -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. * diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java index db195cdbe..1e92e3d3b 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java @@ -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. * diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java new file mode 100644 index 000000000..71c22bb07 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.support.atomic; + +import static org.mockito.Mockito.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * Unit tests for {@link RedisAtomicDouble}. + * + * @author Mark Paluch + */ +@RunWith(MockitoJUnitRunner.class) +public class RedisAtomicDoubleUnitTests { + + @Mock RedisOperations operationsMock; + @Mock ValueOperations valueOperationsMock; + + @Test // DATAREDIS-872 + @SuppressWarnings("unchecked") + public void shouldUseSetIfAbsentForInitialValue() { + + when(operationsMock.opsForValue()).thenReturn(valueOperationsMock); + when(operationsMock.getKeySerializer()).thenReturn(mock(RedisSerializer.class)); + when(operationsMock.getValueSerializer()).thenReturn(mock(RedisSerializer.class)); + + new RedisAtomicDouble("id", operationsMock); + + verify(valueOperationsMock).setIfAbsent("id", 0d); + } +} diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java new file mode 100644 index 000000000..c8eb4a1fc --- /dev/null +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.support.atomic; + +import static org.mockito.Mockito.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * Unit tests for {@link RedisAtomicInteger}. + * + * @author Mark Paluch + */ +@RunWith(MockitoJUnitRunner.class) +public class RedisAtomicIntegerUnitTests { + + @Mock RedisOperations operationsMock; + @Mock ValueOperations valueOperationsMock; + + @Test // DATAREDIS-872 + @SuppressWarnings("unchecked") + public void shouldUseSetIfAbsentForInitialValue() { + + when(operationsMock.opsForValue()).thenReturn(valueOperationsMock); + when(operationsMock.getKeySerializer()).thenReturn(mock(RedisSerializer.class)); + when(operationsMock.getValueSerializer()).thenReturn(mock(RedisSerializer.class)); + + new RedisAtomicInteger("id", operationsMock); + + verify(valueOperationsMock).setIfAbsent("id", 0); + } +} diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java new file mode 100644 index 000000000..4c5be8573 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.support.atomic; + +import static org.mockito.Mockito.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * Unit tests for {@link RedisAtomicLong}. + * + * @author Mark Paluch + */ +@RunWith(MockitoJUnitRunner.class) +public class RedisAtomicLongUnitTests { + + @Mock RedisOperations operationsMock; + @Mock ValueOperations valueOperationsMock; + + @Test // DATAREDIS-872 + @SuppressWarnings("unchecked") + public void shouldUseSetIfAbsentForInitialValue() { + + when(operationsMock.opsForValue()).thenReturn(valueOperationsMock); + when(operationsMock.getKeySerializer()).thenReturn(mock(RedisSerializer.class)); + when(operationsMock.getValueSerializer()).thenReturn(mock(RedisSerializer.class)); + + new RedisAtomicLong("id", operationsMock); + + verify(valueOperationsMock).setIfAbsent("id", 0L); + } +}