T
- the type which contains the target fieldV
- the array value typefinal class AtomicArray<T,V>
extends java.lang.Object
AtomicReferenceFieldUpdater
which corresponds to that field. Some of these methods perform multi-step operations; if the array field value is
changed in the middle of such an operation, the operation is retried. To avoid spinning, in some situations it
may be advisable to hold a write lock to prevent multiple concurrent updates.Modifier and Type | Field and Description |
---|---|
private java.lang.Class<V> |
componentType |
private V[] |
emptyArray |
private java.util.concurrent.atomic.AtomicReferenceFieldUpdater<T,V[]> |
updater |
Constructor and Description |
---|
AtomicArray(java.util.concurrent.atomic.AtomicReferenceFieldUpdater<T,V[]> updater,
java.lang.Class<V> componentType)
Construct an instance.
|
Modifier and Type | Method and Description |
---|---|
void |
add(T instance,
V value)
Atomically replace the array with a new array which is one element longer, and which includes the given value.
|
void |
add(T instance,
V value,
java.util.Comparator<? super V> comparator)
Add a value to a sorted array.
|
boolean |
addIfAbsent(T instance,
V value,
boolean identity)
Atomically replace the array with a new array which is one element longer, and which includes the given value,
if the value is not already present within the array.
|
boolean |
addIfAbsent(T instance,
V value,
java.util.Comparator<? super V> comparator)
Add a value to a sorted array if it is not already present.
|
void |
clear(T instance)
Convenience method to set the field value to the empty array.
|
boolean |
compareAndSet(T instance,
V[] expect,
V[] update)
Compare and set the array.
|
private static <V> V[] |
copyOf(java.lang.Class<V> componentType,
V[] old,
int newLen) |
static <T,V> AtomicArray<T,V> |
create(java.util.concurrent.atomic.AtomicReferenceFieldUpdater<T,V[]> updater,
java.lang.Class<V> componentType)
Convenience method to create an instance.
|
V[] |
getAndSet(T instance,
V[] value)
Atomically get and update the value of this array.
|
private static int |
insertionPoint(int searchResult) |
private static <V> V[] |
newInstance(java.lang.Class<V> componentType,
int length) |
boolean |
remove(T instance,
V value,
boolean identity)
Atomically replace the array with a new array which does not include the first occurrance of the given value, if
the value is present in the array.
|
boolean |
remove(T instance,
V value,
java.util.Comparator<? super V> comparator)
Remove a value to a sorted array.
|
int |
removeAll(T instance,
V value,
boolean identity)
Atomically replace the array with a new array which does not include any occurrances of the given value, if
the value is present in the array.
|
void |
set(T instance,
V[] value)
Update the value of this array.
|
void |
sort(T instance,
java.util.Comparator<? super V> comparator)
Sort an array.
|
private final java.lang.Class<V> componentType
private final V[] emptyArray
public static <T,V> AtomicArray<T,V> create(java.util.concurrent.atomic.AtomicReferenceFieldUpdater<T,V[]> updater, java.lang.Class<V> componentType)
T
- the type which contains the target fieldV
- the array value typeupdater
- the field updatercomponentType
- the component classpublic void clear(T instance)
instance
- the instance holding the fieldpublic void set(T instance, V[] value)
instance
- the instance holding the fieldvalue
- the new valuepublic V[] getAndSet(T instance, V[] value)
instance
- the instance holding the fieldvalue
- the new valueprivate static <V> V[] copyOf(java.lang.Class<V> componentType, V[] old, int newLen)
public void add(T instance, V value)
instance
- the instance holding the fieldvalue
- the updated valuepublic boolean addIfAbsent(T instance, V value, boolean identity)
instance
- the instance holding the fieldvalue
- the updated valueidentity
- true
if comparisons should be done using reference identity, or false
to use the equals()
methodtrue
if the value was added, or false
if it was already presentpublic boolean remove(T instance, V value, boolean identity)
instance
- the instance holding the fieldvalue
- the updated valueidentity
- true
if comparisons should be done using reference identity, or false
to use the equals()
methodtrue
if the value was removed, or false
if it was not presentpublic int removeAll(T instance, V value, boolean identity)
instance
- the instance holding the fieldvalue
- the updated valueidentity
- true
if comparisons should be done using reference identity, or false
to use the equals()
methodpublic void add(T instance, V value, java.util.Comparator<? super V> comparator)
instance
- the instance holding the fieldvalue
- the value to addcomparator
- a comparator, or null
to use natural orderingpublic boolean addIfAbsent(T instance, V value, java.util.Comparator<? super V> comparator)
instance
- the instance holding the fieldvalue
- the value to addcomparator
- a comparator, or null
to use natural orderingpublic boolean remove(T instance, V value, java.util.Comparator<? super V> comparator)
instance
- the instance holding the fieldvalue
- the value to removecomparator
- a comparator, or null
to use natural orderingpublic void sort(T instance, java.util.Comparator<? super V> comparator)
instance
- the instance holding the fieldcomparator
- a comparator, or null
to use natural orderingprivate static int insertionPoint(int searchResult)
private static <V> V[] newInstance(java.lang.Class<V> componentType, int length)