Class CopyOnWriteLongArray<V>
- java.lang.Object
-
- com.tangosol.util.WrapperCollections.AbstractWrapperLongArray<V>
-
- com.tangosol.util.CopyOnWriteLongArray<V>
-
- All Implemented Interfaces:
LongArray<V>
,Serializable
,Cloneable
,Iterable<V>
public class CopyOnWriteLongArray<V> extends WrapperCollections.AbstractWrapperLongArray<V>
A thread-safe variant ofLongArray
in which all mutating operations (e.g. add, set) are implemented by making a fresh copy of the underlying array.Iterators over this LongArray are guaranteed to produce a safe-iteration and not to throw ConcurrentModificationException. The iterator will not reflect concurrent additions, removals, or changes to this array. Mutating operations on iterators themselves (e.g. remove, setValue) are not supported (and will throw UnsupportedOperationException).
Note: mutations on this LongArray are costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations. All mutating operations are synchronized, so concurrent mutation can be prevented by holding synchronization on this object.
This data-structure is not suitable to frequent updates, or updates on large data-sets see
ReadHeavyLongArray
for a more suitable alternative for such cases.- Since:
- Coherence 3.7
- Author:
- rhl 2010.09.09
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CopyOnWriteLongArray.UnmodifiableIterator<V>
Unmodifiable view of a LongArray.Iterator.-
Nested classes/interfaces inherited from interface com.tangosol.util.LongArray
LongArray.Iterator<V>
-
-
Field Summary
Fields Modifier and Type Field Description protected static LongArray
EMPTY_ARRAY
An empty placeholder array.
-
Constructor Summary
Constructors Constructor Description CopyOnWriteLongArray()
Default constructor.CopyOnWriteLongArray(LongArray<V> array)
Construct a CopyOnWriteLongArray, initialized with the contents of the specified LongArray.CopyOnWriteLongArray(Class<? extends LongArray<V>> clazz)
Construct a CopyOnWriteLongArray with an underlying array of the specified type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
add(V oValue)
Add the passed element value to the LongArray and return the index at which the element value was stored.void
clear()
Remove all nodes from the LongArray.CopyOnWriteLongArray<V>
clone()
Make a clone of the LongArray.LongArray<V>
copyArray(LongArray<V> array)
Return a shallow copy of the specified LongArray.protected LongArray<V>
delegate()
Return the internal LongArray.LongArray.Iterator<V>
instantiateUnmodifiableIterator(LongArray.Iterator<V> iterator)
Factory pattern: instantiate an UnmodifiableIterator.LongArray.Iterator<V>
iterator()
Obtain a LongArray.Iterator of the contents of the LongArray in order of increasing indices.LongArray.Iterator<V>
iterator(long lIndex)
Obtain a LongArray.Iterator of the contents of the LongArray in order of increasing indices, starting at a particular index such that the first call to next will set the location of the iterator at the first existent index that is greater than or equal to the specified index, or will throw a NoSuchElementException if there is no such existent index.V
remove(long lIndex)
Remove the specified index from the LongArray, returning its associated value.void
remove(long lIndexFrom, long lIndexTo)
Remove all nodes in the specified range.LongArray.Iterator<V>
reverseIterator()
Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices).LongArray.Iterator<V>
reverseIterator(long lIndex)
Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices), starting at a particular index such that the first call to next will set the location of the iterator at the first existent index that is less than or equal to the specified index, or will throw a NoSuchElementException if there is no such existent index.V
set(long lIndex, V oValue)
Add the passed item to the LongArray at the specified index.protected void
setDelegate(LongArray<V> array)
Set the internal LongArray.-
Methods inherited from class com.tangosol.util.WrapperCollections.AbstractWrapperLongArray
ceiling, ceilingIndex, contains, exists, floor, floorIndex, get, getFirstIndex, getLastIndex, getSize, indexOf, indexOf, isEmpty, lastIndexOf, lastIndexOf
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
EMPTY_ARRAY
protected static final LongArray EMPTY_ARRAY
An empty placeholder array. This array should not be mutated or exposed.
-
-
Constructor Detail
-
CopyOnWriteLongArray
public CopyOnWriteLongArray()
Default constructor.
-
CopyOnWriteLongArray
public CopyOnWriteLongArray(Class<? extends LongArray<V>> clazz) throws IllegalAccessException, InstantiationException
Construct a CopyOnWriteLongArray with an underlying array of the specified type.- Parameters:
clazz
- the type of the internal array; must implement LongArray and have a public no-arg constructor- Throws:
ClassCastException
- if the specified type does not implement LongArrayIllegalAccessException
- if the class or its no-arg constructor is not accessibleInstantiationException
- if the specified Class represents an abstract class or interface; or if the class does not have a no-arg constructor; or if the instantiation fails for some other reason.
-
-
Method Detail
-
delegate
protected LongArray<V> delegate()
Return the internal LongArray.- Specified by:
delegate
in classWrapperCollections.AbstractWrapperLongArray<V>
- Returns:
- the internal LongArray
-
setDelegate
protected void setDelegate(LongArray<V> array)
Set the internal LongArray.- Parameters:
array
- the new internal LongArray
-
copyArray
public LongArray<V> copyArray(LongArray<V> array)
Return a shallow copy of the specified LongArray.- Parameters:
array
- the array to be copied- Returns:
- a shallow copy of the specified LongArray
-
set
public V set(long lIndex, V oValue)
Add the passed item to the LongArray at the specified index.If the index is already used, the passed value will replace the current value stored with the key, and the replaced value will be returned.
It is expected that LongArray implementations will "grow" as necessary to support the specified index.
-
add
public long add(V oValue)
Add the passed element value to the LongArray and return the index at which the element value was stored.
-
remove
public V remove(long lIndex)
Remove the specified index from the LongArray, returning its associated value.
-
remove
public void remove(long lIndexFrom, long lIndexTo)
Remove all nodes in the specified range.
-
clear
public void clear()
Remove all nodes from the LongArray.
-
iterator
public LongArray.Iterator<V> iterator()
Obtain a LongArray.Iterator of the contents of the LongArray in order of increasing indices.
-
iterator
public LongArray.Iterator<V> iterator(long lIndex)
Obtain a LongArray.Iterator of the contents of the LongArray in order of increasing indices, starting at a particular index such that the first call to next will set the location of the iterator at the first existent index that is greater than or equal to the specified index, or will throw a NoSuchElementException if there is no such existent index.
-
reverseIterator
public LongArray.Iterator<V> reverseIterator()
Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices).- Specified by:
reverseIterator
in interfaceLongArray<V>
- Overrides:
reverseIterator
in classWrapperCollections.AbstractWrapperLongArray<V>
- Returns:
- an instance of LongArray.Iterator
-
reverseIterator
public LongArray.Iterator<V> reverseIterator(long lIndex)
Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices), starting at a particular index such that the first call to next will set the location of the iterator at the first existent index that is less than or equal to the specified index, or will throw a NoSuchElementException if there is no such existent index.- Specified by:
reverseIterator
in interfaceLongArray<V>
- Overrides:
reverseIterator
in classWrapperCollections.AbstractWrapperLongArray<V>
- Parameters:
lIndex
- the LongArray index to iterate from- Returns:
- an instance of LongArray.Iterator
-
clone
public CopyOnWriteLongArray<V> clone()
Make a clone of the LongArray. The element values are not deep-cloned.
-
instantiateUnmodifiableIterator
public LongArray.Iterator<V> instantiateUnmodifiableIterator(LongArray.Iterator<V> iterator)
Factory pattern: instantiate an UnmodifiableIterator.- Parameters:
iterator
- the underlying iterator- Returns:
- an UnmodifiableIterator
-
-