Class AtomicArray<T,V>
java.lang.Object
org.jboss.logmanager.AtomicArray<T,V>
- Type Parameters:
T- the type which contains the target fieldV- the array value type
Utility for snapshot/copy-on-write arrays. To use these methods, two things are required: an immutable array
stored on a volatile field, and an instance of
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.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final V[]private final AtomicReferenceFieldUpdater<T, V[]> -
Constructor Summary
ConstructorsConstructorDescriptionAtomicArray(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType) Construct an instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidAtomically replace the array with a new array which is one element longer, and which includes the given value.voidadd(T instance, V value, Comparator<? super V> comparator) Add a value to a sorted array.booleanaddIfAbsent(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.booleanaddIfAbsent(T instance, V value, Comparator<? super V> comparator) Add a value to a sorted array if it is not already present.voidConvenience method to set the field value to the empty array.booleancompareAndSet(T instance, V[] expect, V[] update) Compare and set the array.private static <V> V[]static <T,V> AtomicArray <T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType) Convenience method to create an instance.V[]Atomically get and update the value of this array.private static intinsertionPoint(int searchResult) private static <V> V[]newInstance(Class<V> componentType, int length) booleanAtomically 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.booleanremove(T instance, V value, Comparator<? super V> comparator) Remove a value to a sorted array.intAtomically 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.voidUpdate the value of this array.voidsort(T instance, Comparator<? super V> comparator) Sort an array.
-
Field Details
-
updater
-
componentType
-
emptyArray
-
-
Constructor Details
-
AtomicArray
Construct an instance.- Parameters:
updater- the field updatercomponentType- the component class
-
-
Method Details
-
create
public static <T,V> AtomicArray<T,V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType) Convenience method to create an instance.- Type Parameters:
T- the type which contains the target fieldV- the array value type- Parameters:
updater- the field updatercomponentType- the component class- Returns:
- the new instance
-
clear
Convenience method to set the field value to the empty array. Empty array instances are shared.- Parameters:
instance- the instance holding the field
-
set
-
getAndSet
-
copyOf
-
add
-
addIfAbsent
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. This method does a linear search for the target value.- Parameters:
instance- the instance holding the fieldvalue- the updated valueidentity-trueif comparisons should be done using reference identity, orfalseto use theequals()method- Returns:
trueif the value was added, orfalseif it was already present
-
remove
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.- Parameters:
instance- the instance holding the fieldvalue- the updated valueidentity-trueif comparisons should be done using reference identity, orfalseto use theequals()method- Returns:
trueif the value was removed, orfalseif it was not present
-
removeAll
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.- Parameters:
instance- the instance holding the fieldvalue- the updated valueidentity-trueif comparisons should be done using reference identity, orfalseto use theequals()method- Returns:
- the number of values removed
-
add
Add a value to a sorted array. Does not check for duplicates.- Parameters:
instance- the instance holding the fieldvalue- the value to addcomparator- a comparator, ornullto use natural ordering
-
addIfAbsent
Add a value to a sorted array if it is not already present. Does not check for duplicates.- Parameters:
instance- the instance holding the fieldvalue- the value to addcomparator- a comparator, ornullto use natural ordering
-
remove
Remove a value to a sorted array. Does not check for duplicates. If there are multiple occurrances of a value, there is no guarantee as to which one is removed.- Parameters:
instance- the instance holding the fieldvalue- the value to removecomparator- a comparator, ornullto use natural ordering
-
sort
Sort an array.- Parameters:
instance- the instance holding the fieldcomparator- a comparator, ornullto use natural ordering
-
insertionPoint
private static int insertionPoint(int searchResult) -
newInstance
-
compareAndSet
-