Class CopyOnWriteLongArray<V>

  • All Implemented Interfaces:
    LongArray<V>, Serializable, Cloneable, Iterable<V>

    public class CopyOnWriteLongArray<V>
    extends WrapperCollections.AbstractWrapperLongArray<V>
    A thread-safe variant of LongArray 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
    • 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 LongArray
        IllegalAccessException - if the class or its no-arg constructor is not accessible
        InstantiationException - 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.
      • CopyOnWriteLongArray

        public CopyOnWriteLongArray​(LongArray<V> array)
        Construct a CopyOnWriteLongArray, initialized with the contents of the specified LongArray.
        Parameters:
        array - the initial LongArray
    • Method Detail

      • 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.

        Specified by:
        set in interface LongArray<V>
        Overrides:
        set in class WrapperCollections.AbstractWrapperLongArray<V>
        Parameters:
        lIndex - a long index value
        oValue - the object to store at the specified index
        Returns:
        the object that was stored at the specified index, or null
      • 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.
        Specified by:
        add in interface LongArray<V>
        Overrides:
        add in class WrapperCollections.AbstractWrapperLongArray<V>
        Parameters:
        oValue - the object to add to the LongArray
        Returns:
        the long index value at which the element value was stored
      • remove

        public V remove​(long lIndex)
        Remove the specified index from the LongArray, returning its associated value.
        Specified by:
        remove in interface LongArray<V>
        Overrides:
        remove in class WrapperCollections.AbstractWrapperLongArray<V>
        Parameters:
        lIndex - the index into the LongArray
        Returns:
        the associated value (which can be null) or null if the specified index is not in the LongArray
      • 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.
        Specified by:
        iterator in interface LongArray<V>
        Overrides:
        iterator in class WrapperCollections.AbstractWrapperLongArray<V>
        Parameters:
        lIndex - the LongArray index to iterate from
        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 interface LongArray<V>
        Overrides:
        reverseIterator in class WrapperCollections.AbstractWrapperLongArray<V>
        Parameters:
        lIndex - the LongArray index to iterate from
        Returns:
        an instance of LongArray.Iterator
      • instantiateUnmodifiableIterator

        public LongArray.Iterator<V> instantiateUnmodifiableIterator​(LongArray.Iterator<V> iterator)
        Factory pattern: instantiate an UnmodifiableIterator.
        Parameters:
        iterator - the underlying iterator
        Returns:
        an UnmodifiableIterator