Spring Data Key-Value

org.springframework.data.keyvalue.redis.support.atomic
Class RedisAtomicInteger

java.lang.Object
  extended by java.lang.Number
      extended by org.springframework.data.keyvalue.redis.support.atomic.RedisAtomicInteger
All Implemented Interfaces:
Serializable, KeyBound<String>

public class RedisAtomicInteger
extends Number
implements Serializable, KeyBound<String>

Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS operations.

Author:
Costin Leau
See Also:
AtomicInteger, Serialized Form

Constructor Summary
RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory)
          Constructs a new RedisAtomicInteger instance.
RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, int initialValue)
          Constructs a new RedisAtomicInteger instance.
RedisAtomicInteger(String redisCounter, RedisOperations<String,Integer> operations)
          Constructs a new RedisAtomicInteger instance.
RedisAtomicInteger(String redisCounter, RedisOperations<String,Integer> operations, int initialValue)
          Constructs a new RedisAtomicInteger instance with the given initial value.
 
Method Summary
 int addAndGet(int delta)
          Atomically add the given value to current value.
 boolean compareAndSet(int expect, int update)
          Atomically set the value to the given updated value if the current value == the expected value.
 int decrementAndGet()
          Atomically decrement by one the current value.
 double doubleValue()
           
 float floatValue()
           
 int get()
          Get the current value.
 int getAndAdd(int delta)
          Atomically add the given value to current value.
 int getAndDecrement()
          Atomically decrement by one the current value.
 int getAndIncrement()
          Atomically increment by one the current value.
 int getAndSet(int newValue)
          Set to the give value and return the old value.
 String getKey()
          Returns the key associated with this entity.
 int incrementAndGet()
          Atomically increment by one the current value.
 int intValue()
           
 long longValue()
           
 void set(int newValue)
          Set to the given value.
 String toString()
          Returns the String representation of the current value.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RedisAtomicInteger

public RedisAtomicInteger(String redisCounter,
                          RedisConnectionFactory factory)
Constructs a new RedisAtomicInteger instance.

Parameters:
redisCounter - redis counter
factory - connection factory

RedisAtomicInteger

public RedisAtomicInteger(String redisCounter,
                          RedisConnectionFactory factory,
                          int initialValue)
Constructs a new RedisAtomicInteger instance.

Parameters:
redisCounter -
factory -
initialValue -

RedisAtomicInteger

public RedisAtomicInteger(String redisCounter,
                          RedisOperations<String,Integer> operations)
Constructs a new RedisAtomicInteger instance. Uses as initial value the data from the backing store (sets the counter to 0 if no value is found). Use RedisAtomicInteger(String, RedisOperations, int) to set the counter to a certain value as an alternative constructor or set(int).

Parameters:
redisCounter -
operations -

RedisAtomicInteger

public RedisAtomicInteger(String redisCounter,
                          RedisOperations<String,Integer> operations,
                          int initialValue)
Constructs a new RedisAtomicInteger instance with the given initial value.

Parameters:
redisCounter -
operations -
initialValue -
Method Detail

getKey

public String getKey()
Description copied from interface: KeyBound
Returns the key associated with this entity.

Specified by:
getKey in interface KeyBound<String>
Returns:
key associated with the implementing entity

get

public int get()
Get the current value.

Returns:
the current value

set

public void set(int newValue)
Set to the given value.

Parameters:
newValue - the new value

getAndSet

public int getAndSet(int newValue)
Set to the give value and return the old value.

Parameters:
newValue - the new value
Returns:
the previous value

compareAndSet

public boolean compareAndSet(int expect,
                             int update)
Atomically set the value to the given updated value if the current value == the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the actual value was not equal to the expected value.

getAndIncrement

public int getAndIncrement()
Atomically increment by one the current value.

Returns:
the previous value

getAndDecrement

public int getAndDecrement()
Atomically decrement by one the current value.

Returns:
the previous value

getAndAdd

public int getAndAdd(int delta)
Atomically add the given value to current value.

Parameters:
delta - the value to add
Returns:
the previous value

incrementAndGet

public int incrementAndGet()
Atomically increment by one the current value.

Returns:
the updated value

decrementAndGet

public int decrementAndGet()
Atomically decrement by one the current value.

Returns:
the updated value

addAndGet

public int addAndGet(int delta)
Atomically add the given value to current value.

Parameters:
delta - the value to add
Returns:
the updated value

toString

public String toString()
Returns the String representation of the current value.

Overrides:
toString in class Object
Returns:
the String representation of the current value.

intValue

public int intValue()
Specified by:
intValue in class Number

longValue

public long longValue()
Specified by:
longValue in class Number

floatValue

public float floatValue()
Specified by:
floatValue in class Number

doubleValue

public double doubleValue()
Specified by:
doubleValue in class Number

Spring Data Key-Value

Copyright © 2010-2011 SpringSource. All Rights Reserved.