com.atlassian.util.concurrent.atomic
Class AtomicReference<V>

java.lang.Object
  extended by java.util.concurrent.atomic.AtomicReference<V>
      extended by com.atlassian.util.concurrent.atomic.AtomicReference<V>
All Implemented Interfaces:
java.io.Serializable

public class AtomicReference<V>
extends java.util.concurrent.atomic.AtomicReference<V>

AtomicReference with richer functionality. This class implements commonly implemented patterns of use of compareAndSet such as #getAndSetIf(Object, Object) and update(Function).

Since:
0.0.12
See Also:
Serialized Form

Constructor Summary
AtomicReference()
          Creates a new AtomicReference with null initial value.
AtomicReference(V initialValue)
          Creates a new AtomicReference with the given initial value.
 
Method Summary
 V getOrSetAndGetIf(V oldValue, com.google.common.base.Supplier<V> newValue)
          Check the current value and if it matches the old value argument, set it to the one created by the new value supplier and return that instead.
 V getOrSetAndGetIf(V oldValue, V newValue)
          Check the current value and if it matches the old value argument, set it to the new value and return that instead.
 V update(com.google.common.base.Function<V,V> newValueFactory)
          Do the actual update.
 
Methods inherited from class java.util.concurrent.atomic.AtomicReference
compareAndSet, get, getAndSet, lazySet, set, toString, weakCompareAndSet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AtomicReference

public AtomicReference()
Creates a new AtomicReference with null initial value.


AtomicReference

public AtomicReference(V initialValue)
Creates a new AtomicReference with the given initial value.

Parameters:
initialValue - the initial value
Method Detail

getOrSetAndGetIf

public final V getOrSetAndGetIf(V oldValue,
                                com.google.common.base.Supplier<V> newValue)
Check the current value and if it matches the old value argument, set it to the one created by the new value supplier and return that instead. If the old value argument does not match, ignore both and just return the current value.

Type Parameters:
T - the object type.
Parameters:
oldValue - to check the current value against (reference equality check only).
newValue - a Supplier for a new value. May be called more than once.
Returns:
the current reference value if it doesn't match old value or a newly created value.

getOrSetAndGetIf

public final V getOrSetAndGetIf(V oldValue,
                                V newValue)
Check the current value and if it matches the old value argument, set it to the new value and return that instead. If the old value argument does not match, ignore both and just return the current value.

Type Parameters:
T - the object type.
Parameters:
oldValue - to check the current value against (reference equality check only)
newValue - the new value to set it to
Returns:
the current reference value if it doesn't match oldValue or a newly created value.

update

public final V update(com.google.common.base.Function<V,V> newValueFactory)
Do the actual update. Calls the factory method with the old value to do the update logic, then sets the value to that if it hasn't changed in the meantime.

Returns:
the new updated value.


Copyright © 2011 Atlassian. All Rights Reserved.