Skip to content

Commit d9db5d9

Browse files
committed
DATAREDIS-469 - Polishing.
Return zero (0) in getAndSet to prevent NullPointerExceptions. Remove trailing whitespace. Add tests for existing methods. Simplify tests. Original pull request: #182.
1 parent cbc7766 commit d9db5d9

File tree

6 files changed

+252
-99
lines changed

6 files changed

+252
-99
lines changed

src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
/**
3737
* Atomic double backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
3838
* operations.
39-
*
39+
*
4040
* @author Jennifer Hickey
4141
* @author Thomas Darimont
4242
* @author Christoph Strobl
43+
* @author Mark Paluch
4344
*/
4445
public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations<String> {
4546

@@ -51,7 +52,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
5152

5253
/**
5354
* Constructs a new <code>RedisAtomicDouble</code> instance. Uses the value existing in Redis or 0 if none is found.
54-
*
55+
*
5556
* @param redisCounter redis counter
5657
* @param factory connection factory
5758
*/
@@ -61,7 +62,7 @@ public RedisAtomicDouble(String redisCounter, RedisConnectionFactory factory) {
6162

6263
/**
6364
* Constructs a new <code>RedisAtomicDouble</code> instance.
64-
*
65+
*
6566
* @param redisCounter
6667
* @param factory
6768
* @param initialValue
@@ -96,7 +97,7 @@ private RedisAtomicDouble(String redisCounter, RedisConnectionFactory factory, D
9697

9798
/**
9899
* Constructs a new <code>RedisAtomicDouble</code> instance. Uses the value existing in Redis or 0 if none is found.
99-
*
100+
*
100101
* @param redisCounter the redis counter
101102
* @param template the template
102103
* @see #RedisAtomicDouble(String, RedisConnectionFactory, double)
@@ -110,7 +111,7 @@ public RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> te
110111
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
111112
* {@link #RedisAtomicDouble(String, RedisConnectionFactory, Double)} constructor which uses appropriate default
112113
* serializers.
113-
*
114+
*
114115
* @param redisCounter the redis counter
115116
* @param template the template
116117
* @param initialValue the initial value
@@ -141,7 +142,7 @@ private RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> t
141142

142143
/**
143144
* Gets the current value.
144-
*
145+
*
145146
* @return the current value
146147
*/
147148
public double get() {
@@ -156,7 +157,7 @@ public double get() {
156157

157158
/**
158159
* Sets to the given value.
159-
*
160+
*
160161
* @param newValue the new value
161162
*/
162163
public void set(double newValue) {
@@ -165,17 +166,23 @@ public void set(double newValue) {
165166

166167
/**
167168
* Atomically sets to the given value and returns the old value.
168-
*
169+
*
169170
* @param newValue the new value
170171
* @return the previous value
171172
*/
172173
public double getAndSet(double newValue) {
173-
return operations.getAndSet(key, newValue);
174+
175+
Double value = operations.getAndSet(key, newValue);
176+
if (value != null) {
177+
return value.doubleValue();
178+
}
179+
180+
return 0;
174181
}
175182

176183
/**
177184
* Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
178-
*
185+
*
179186
* @param expect the expected value
180187
* @param update the new value
181188
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
@@ -204,7 +211,7 @@ public Boolean execute(RedisOperations operations) {
204211

205212
/**
206213
* Atomically increments by one the current value.
207-
*
214+
*
208215
* @return the previous value
209216
*/
210217
public double getAndIncrement() {
@@ -213,7 +220,7 @@ public double getAndIncrement() {
213220

214221
/**
215222
* Atomically decrements by one the current value.
216-
*
223+
*
217224
* @return the previous value
218225
*/
219226
public double getAndDecrement() {
@@ -222,7 +229,7 @@ public double getAndDecrement() {
222229

223230
/**
224231
* Atomically adds the given value to the current value.
225-
*
232+
*
226233
* @param delta the value to add
227234
* @return the previous value
228235
*/
@@ -232,7 +239,7 @@ public double getAndAdd(final double delta) {
232239

233240
/**
234241
* Atomically increments by one the current value.
235-
*
242+
*
236243
* @return the updated value
237244
*/
238245
public double incrementAndGet() {
@@ -241,7 +248,7 @@ public double incrementAndGet() {
241248

242249
/**
243250
* Atomically decrements by one the current value.
244-
*
251+
*
245252
* @return the updated value
246253
*/
247254
public double decrementAndGet() {
@@ -250,7 +257,7 @@ public double decrementAndGet() {
250257

251258
/**
252259
* Atomically adds the given value to the current value.
253-
*
260+
*
254261
* @param delta the value to add
255262
* @return the updated value
256263
*/
@@ -260,7 +267,7 @@ public double addAndGet(double delta) {
260267

261268
/**
262269
* Returns the String representation of the current value.
263-
*
270+
*
264271
* @return the String representation of the current value.
265272
*/
266273
public String toString() {

src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011-2016 the original author or authors.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -36,11 +36,12 @@
3636
/**
3737
* Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
3838
* operations.
39-
*
39+
*
4040
* @see java.util.concurrent.atomic.AtomicInteger
4141
* @author Costin Leau
4242
* @author Thomas Darimont
4343
* @author Christoph Strobl
44+
* @author Mark Paluch
4445
*/
4546
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {
4647

@@ -52,7 +53,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
5253

5354
/**
5455
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
55-
*
56+
*
5657
* @param redisCounter redis counter
5758
* @param factory connection factory
5859
*/
@@ -62,7 +63,7 @@ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) {
6263

6364
/**
6465
* Constructs a new <code>RedisAtomicInteger</code> instance.
65-
*
66+
*
6667
* @param redisCounter the redis counter
6768
* @param factory the factory
6869
* @param initialValue the initial value
@@ -73,7 +74,7 @@ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, i
7374

7475
/**
7576
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
76-
*
77+
*
7778
* @param redisCounter the redis counter
7879
* @param template the template
7980
* @see #RedisAtomicInteger(String, RedisConnectionFactory, int)
@@ -87,7 +88,7 @@ public RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>
8788
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
8889
* {@link #RedisAtomicInteger(String, RedisConnectionFactory, Integer)} constructor which uses appropriate default
8990
* serializers.
90-
*
91+
*
9192
* @param redisCounter the redis counter
9293
* @param template the template
9394
* @param initialValue the initial value
@@ -139,7 +140,7 @@ private RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>
139140

140141
/**
141142
* Get the current value.
142-
*
143+
*
143144
* @return the current value
144145
*/
145146
public int get() {
@@ -154,7 +155,7 @@ public int get() {
154155

155156
/**
156157
* Set to the given value.
157-
*
158+
*
158159
* @param newValue the new value
159160
*/
160161
public void set(int newValue) {
@@ -163,17 +164,23 @@ public void set(int newValue) {
163164

164165
/**
165166
* Set to the give value and return the old value.
166-
*
167+
*
167168
* @param newValue the new value
168169
* @return the previous value
169170
*/
170171
public int getAndSet(int newValue) {
171-
return operations.getAndSet(key, newValue);
172+
173+
Integer value = operations.getAndSet(key, newValue);
174+
if (value != null) {
175+
return value.intValue();
176+
}
177+
178+
return 0;
172179
}
173180

174181
/**
175182
* Atomically set the value to the given updated value if the current value <tt>==</tt> the expected value.
176-
*
183+
*
177184
* @param expect the expected value
178185
* @param update the new value
179186
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
@@ -202,7 +209,7 @@ public Boolean execute(RedisOperations operations) {
202209

203210
/**
204211
* Atomically increment by one the current value.
205-
*
212+
*
206213
* @return the previous value
207214
*/
208215
public int getAndIncrement() {
@@ -211,7 +218,7 @@ public int getAndIncrement() {
211218

212219
/**
213220
* Atomically decrement by one the current value.
214-
*
221+
*
215222
* @return the previous value
216223
*/
217224
public int getAndDecrement() {
@@ -220,7 +227,7 @@ public int getAndDecrement() {
220227

221228
/**
222229
* Atomically add the given value to current value.
223-
*
230+
*
224231
* @param delta the value to add
225232
* @return the previous value
226233
*/
@@ -230,7 +237,7 @@ public int getAndAdd(final int delta) {
230237

231238
/**
232239
* Atomically increment by one the current value.
233-
*
240+
*
234241
* @return the updated value
235242
*/
236243
public int incrementAndGet() {
@@ -239,7 +246,7 @@ public int incrementAndGet() {
239246

240247
/**
241248
* Atomically decrement by one the current value.
242-
*
249+
*
243250
* @return the updated value
244251
*/
245252
public int decrementAndGet() {
@@ -248,7 +255,7 @@ public int decrementAndGet() {
248255

249256
/**
250257
* Atomically add the given value to current value.
251-
*
258+
*
252259
* @param delta the value to add
253260
* @return the updated value
254261
*/
@@ -258,7 +265,7 @@ public int addAndGet(int delta) {
258265

259266
/**
260267
* Returns the String representation of the current value.
261-
*
268+
*
262269
* @return the String representation of the current value.
263270
*/
264271
public String toString() {

0 commit comments

Comments
 (0)