com.atlassian.util.concurrent.atomic
Class AtomicReferenceArray<E>

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

public class AtomicReferenceArray<E>
extends java.util.concurrent.atomic.AtomicReferenceArray<E>

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

Since:
0.0.12
See Also:
Serialized Form

Constructor Summary
AtomicReferenceArray(E[] initialValue)
          Creates a new AtomicReferenceArray with the same length as, and all elements copied from, the given array.
AtomicReferenceArray(int length)
          Creates a new AtomicReferenceArray of given length.
 
Method Summary
 E getOrSetAndGetIf(int index, E oldValue, E newValue)
          Check the current value and if it matches the old value argument, set it to the new value and return that instead.
 E getOrSetAndGetIf(int index, E oldValue, com.google.common.base.Supplier<E> 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.
 E update(int index, com.google.common.base.Function<E,E> newValueFactory)
          Do the actual update.
 
Methods inherited from class java.util.concurrent.atomic.AtomicReferenceArray
compareAndSet, get, getAndSet, lazySet, length, set, toString, weakCompareAndSet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AtomicReferenceArray

public AtomicReferenceArray(int length)
Creates a new AtomicReferenceArray of given length.

Parameters:
length - the length of the array

AtomicReferenceArray

public AtomicReferenceArray(E[] initialValue)
Creates a new AtomicReferenceArray with the same length as, and all elements copied from, the given array.

Parameters:
array - the array to copy elements from
Throws:
java.lang.NullPointerException - if array is null
Method Detail

getOrSetAndGetIf

public final E getOrSetAndGetIf(int index,
                                E oldValue,
                                com.google.common.base.Supplier<E> 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 E getOrSetAndGetIf(int index,
                                E oldValue,
                                E 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 E update(int index,
                      com.google.common.base.Function<E,E> 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.