1
1
/*
2
2
* Copyright 2011-2016 the original author or authors.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
7
+ *
8
8
* http://www.apache.org/licenses/LICENSE-2.0
9
- *
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36
36
/**
37
37
* Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
38
38
* operations.
39
- *
39
+ *
40
40
* @see java.util.concurrent.atomic.AtomicInteger
41
41
* @author Costin Leau
42
42
* @author Thomas Darimont
43
43
* @author Christoph Strobl
44
+ * @author Mark Paluch
44
45
*/
45
46
public class RedisAtomicInteger extends Number implements Serializable , BoundKeyOperations <String > {
46
47
@@ -52,7 +53,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
52
53
53
54
/**
54
55
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
55
- *
56
+ *
56
57
* @param redisCounter redis counter
57
58
* @param factory connection factory
58
59
*/
@@ -62,7 +63,7 @@ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) {
62
63
63
64
/**
64
65
* Constructs a new <code>RedisAtomicInteger</code> instance.
65
- *
66
+ *
66
67
* @param redisCounter the redis counter
67
68
* @param factory the factory
68
69
* @param initialValue the initial value
@@ -73,7 +74,7 @@ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, i
73
74
74
75
/**
75
76
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
76
- *
77
+ *
77
78
* @param redisCounter the redis counter
78
79
* @param template the template
79
80
* @see #RedisAtomicInteger(String, RedisConnectionFactory, int)
@@ -87,7 +88,7 @@ public RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>
87
88
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
88
89
* {@link #RedisAtomicInteger(String, RedisConnectionFactory, Integer)} constructor which uses appropriate default
89
90
* serializers.
90
- *
91
+ *
91
92
* @param redisCounter the redis counter
92
93
* @param template the template
93
94
* @param initialValue the initial value
@@ -139,7 +140,7 @@ private RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>
139
140
140
141
/**
141
142
* Get the current value.
142
- *
143
+ *
143
144
* @return the current value
144
145
*/
145
146
public int get () {
@@ -154,7 +155,7 @@ public int get() {
154
155
155
156
/**
156
157
* Set to the given value.
157
- *
158
+ *
158
159
* @param newValue the new value
159
160
*/
160
161
public void set (int newValue ) {
@@ -163,17 +164,23 @@ public void set(int newValue) {
163
164
164
165
/**
165
166
* Set to the give value and return the old value.
166
- *
167
+ *
167
168
* @param newValue the new value
168
169
* @return the previous value
169
170
*/
170
171
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 ;
172
179
}
173
180
174
181
/**
175
182
* Atomically set the value to the given updated value if the current value <tt>==</tt> the expected value.
176
- *
183
+ *
177
184
* @param expect the expected value
178
185
* @param update the new value
179
186
* @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) {
202
209
203
210
/**
204
211
* Atomically increment by one the current value.
205
- *
212
+ *
206
213
* @return the previous value
207
214
*/
208
215
public int getAndIncrement () {
@@ -211,7 +218,7 @@ public int getAndIncrement() {
211
218
212
219
/**
213
220
* Atomically decrement by one the current value.
214
- *
221
+ *
215
222
* @return the previous value
216
223
*/
217
224
public int getAndDecrement () {
@@ -220,7 +227,7 @@ public int getAndDecrement() {
220
227
221
228
/**
222
229
* Atomically add the given value to current value.
223
- *
230
+ *
224
231
* @param delta the value to add
225
232
* @return the previous value
226
233
*/
@@ -230,7 +237,7 @@ public int getAndAdd(final int delta) {
230
237
231
238
/**
232
239
* Atomically increment by one the current value.
233
- *
240
+ *
234
241
* @return the updated value
235
242
*/
236
243
public int incrementAndGet () {
@@ -239,7 +246,7 @@ public int incrementAndGet() {
239
246
240
247
/**
241
248
* Atomically decrement by one the current value.
242
- *
249
+ *
243
250
* @return the updated value
244
251
*/
245
252
public int decrementAndGet () {
@@ -248,7 +255,7 @@ public int decrementAndGet() {
248
255
249
256
/**
250
257
* Atomically add the given value to current value.
251
- *
258
+ *
252
259
* @param delta the value to add
253
260
* @return the updated value
254
261
*/
@@ -258,7 +265,7 @@ public int addAndGet(int delta) {
258
265
259
266
/**
260
267
* Returns the String representation of the current value.
261
- *
268
+ *
262
269
* @return the String representation of the current value.
263
270
*/
264
271
public String toString () {
0 commit comments