Top Description Fields Constructors Methods
org.jboss.logmanager

pack-priv final Class AtomicArray<T, V>

extends Object
Class Inheritance
Type Parameters
<T>
the type which contains the target field
<V>
the array value type
Imports
java.lang.reflect.Array, java.util.Arrays, .Comparator, java.util.concurrent.atomic.AtomicReferenceFieldUpdater, java.util.logging.Handler

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

Modifier and TypeField and Description
private final Class<V>
private final V[]
private final AtomicReferenceFieldUpdater<T, V[]>

Constructor Summary

AccessConstructor and Description
public
AtomicArray(AtomicReferenceFieldUpdater<T, V[]>
the field updater
updater
,
Class<V>
the component class
componentType
)

Construct an instance.

Method Summary

Modifier and TypeMethod and Description
public void
add(T
the instance holding the field
instance
,
V
the updated value
value
)

Atomically replace the array with a new array which is one element longer, and which includes the given value.

public void
add(T
the instance holding the field
instance
,
V
the value to add
value
,
Comparator<? super V>
a comparator, or null to use natural ordering
comparator
)

Add a value to a sorted array.

public boolean

Returns:

true if the value was added, or false if it was already present
addIfAbsent
(T
the instance holding the field
instance
,
V
the updated value
value
,
boolean
true if comparisons should be done using reference identity, or false to use the equals() method
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.

public boolean
addIfAbsent(T
the instance holding the field
instance
,
V
the value to add
value
,
Comparator<? super V>
a comparator, or null to use natural ordering
comparator
)

Add a value to a sorted array if it is not already present.

public void
clear(T
the instance holding the field
instance
)

Convenience method to set the field value to the empty array.

public boolean

Returns:

true if the value was updated or false if the expected value did not match
compareAndSet
(final T
the instance holding the field
instance
,
final V[]
the expected value
expect
,
final V[]
the update value
update
)

Compare and set the array.

private static <V> V[]
copyOf(final Class<V> componentType, V[] old, int newLen)

public static <
the type which contains the target field
T
,
the array value type
V
>
AtomicArray<T, V>

Returns:

the new instance
create
(AtomicReferenceFieldUpdater<T, V[]>
the field updater
updater
,
Class<V>
the component class
componentType
)

Convenience method to create an instance.

public V[]
getAndSet(T
the instance holding the field
instance
,
V[]
the new value
value
)

Atomically get and update the value of this array.

private static int
insertionPoint(int searchResult)

private static <V> V[]
newInstance(Class<V> componentType, int length)

public boolean

Returns:

true if the value was removed, or false if it was not present
remove
(T
the instance holding the field
instance
,
V
the updated value
value
,
boolean
true if comparisons should be done using reference identity, or false to use the equals() method
identity
)

Atomically replace the array with a new array which does not include the first occurrence of the given value, if the value is present in the array.

public boolean
remove(T
the instance holding the field
instance
,
V
the value to remove
value
,
Comparator<? super V>
a comparator, or null to use natural ordering
comparator
)

Remove a value to a sorted array.

public int

Returns:

the number of values removed
removeAll
(T
the instance holding the field
instance
,
V
the updated value
value
,
boolean
true if comparisons should be done using reference identity, or false to use the equals() method
identity
)

Atomically replace the array with a new array which does not include any occurrences of the given value, if the value is present in the array.

public void
set(T
the instance holding the field
instance
,
V[]
the new value
value
)

Update the value of this array.

public void
sort(T
the instance holding the field
instance
,
Comparator<? super V>
a comparator, or null to use natural ordering
comparator
)

Sort an array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

componentTypeback to summary
private final Class<V> componentType
emptyArrayback to summary
private final V[] emptyArray
updaterback to summary
private final AtomicReferenceFieldUpdater<T, V[]> updater

Constructor Detail

AtomicArrayback to summary
public AtomicArray(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType)

Construct an instance.

Parameters
updater:AtomicReferenceFieldUpdater<T, V[]>

the field updater

componentType:Class<V>

the component class

Method Detail

addback to summary
public 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.

Parameters
instance:T

the instance holding the field

value:V

the updated value

addback to summary
public void add(T instance, V value, Comparator<? super V> comparator)

Add a value to a sorted array. Does not check for duplicates.

Parameters
instance:T

the instance holding the field

value:V

the value to add

comparator:Comparator<? super V>

a comparator, or null to use natural ordering

addIfAbsentback to summary
public 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. This method does a linear search for the target value.

Parameters
instance:T

the instance holding the field

value:V

the updated value

identity:boolean

true if comparisons should be done using reference identity, or false to use the equals() method

Returns:boolean

true if the value was added, or false if it was already present

addIfAbsentback to summary
public boolean addIfAbsent(T instance, V value, Comparator<? super V> comparator)

Add a value to a sorted array if it is not already present. Does not check for duplicates.

Parameters
instance:T

the instance holding the field

value:V

the value to add

comparator:Comparator<? super V>

a comparator, or null to use natural ordering

clearback to summary
public void clear(T instance)

Convenience method to set the field value to the empty array. Empty array instances are shared.

Parameters
instance:T

the instance holding the field

compareAndSetback to summary
public boolean compareAndSet(final T instance, final V[] expect, final V[] update)

Compare and set the array.

Parameters
instance:T

the instance holding the field

expect:V[]

the expected value

update:V[]

the update value

Returns:boolean

true if the value was updated or false if the expected value did not match

copyOfback to summary
private static <V> V[] copyOf(final Class<V> componentType, V[] old, int newLen)
Annotations
@SuppressWarnings:unchecked
createback to summary
public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType)

Convenience method to create an instance.

Parameters
<T>
the type which contains the target field
<V>
the array value type
updater:AtomicReferenceFieldUpdater<T, V[]>

the field updater

componentType:Class<V>

the component class

Returns:AtomicArray<T, V>

the new instance

getAndSetback to summary
public V[] getAndSet(T instance, V[] value)

Atomically get and update the value of this array.

Parameters
instance:T

the instance holding the field

value:V[]

the new value

insertionPointback to summary
private static int insertionPoint(int searchResult)
newInstanceback to summary
private static <V> V[] newInstance(Class<V> componentType, int length)
Annotations
@SuppressWarnings:unchecked
removeback to summary
public boolean remove(T instance, V value, boolean identity)

Atomically replace the array with a new array which does not include the first occurrence of the given value, if the value is present in the array.

Parameters
instance:T

the instance holding the field

value:V

the updated value

identity:boolean

true if comparisons should be done using reference identity, or false to use the equals() method

Returns:boolean

true if the value was removed, or false if it was not present

removeback to summary
public boolean remove(T instance, V value, Comparator<? super V> comparator)

Remove a value to a sorted array. Does not check for duplicates. If there are multiple occurrences of a value, there is no guarantee as to which one is removed.

Parameters
instance:T

the instance holding the field

value:V

the value to remove

comparator:Comparator<? super V>

a comparator, or null to use natural ordering

removeAllback to summary
public int removeAll(T instance, V value, boolean identity)

Atomically replace the array with a new array which does not include any occurrences of the given value, if the value is present in the array.

Parameters
instance:T

the instance holding the field

value:V

the updated value

identity:boolean

true if comparisons should be done using reference identity, or false to use the equals() method

Returns:int

the number of values removed

setback to summary
public void set(T instance, V[] value)

Update the value of this array.

Parameters
instance:T

the instance holding the field

value:V[]

the new value

sortback to summary
public void sort(T instance, Comparator<? super V> comparator)

Sort an array.

Parameters
instance:T

the instance holding the field

comparator:Comparator<? super V>

a comparator, or null to use natural ordering