Class ConcurrentList<T>

java.lang.Object
com.pnfsoftware.jeb.util.collect.ConcurrentList<T>
Type Parameters:
T - element type
All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>, SequencedCollection<T>

@Ser public class ConcurrentList<T> extends Object implements List<T>
A concurrent version of ArrayList. Unlike CopyOnWriteArrayList, only iterator-returning operations return defensive copies. Other write operations (addition, insertion, deletion) do not generate defensive copies. Also contains safe versions of add/set/get for convenience. Performing much better than CopyOnWriteArrayList if write operations are frequent; get() operations are slightly slower than CopyOnWriteArrayList's, so if they make up the majority of the invocations, CopyOnWriteArrayList remains the best option.
  • Constructor Details

    • ConcurrentList

      public ConcurrentList()
      Create an empty concurrent list.
    • ConcurrentList

      public ConcurrentList(int initialCapacity)
      Create an empty concurrent list.
      Parameters:
      initialCapacity - initial capacity
    • ConcurrentList

      public ConcurrentList(Collection<? extends T> collection)
      Create a concurrent list initialized from a collection.
      Parameters:
      collection - source collection
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface List<T>
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface List<T>
    • get

      public T get(int index)
      May raise ArrayIndexOutOfBoundsException if index is greater or equals than the list's size by the time the lock is acquired. Safe version: getSafe(int, Object).
      Specified by:
      get in interface List<T>
    • getSafe

      public T getSafe(int index, T valueIfPastBound)
      Safely retrieve an element.
      Parameters:
      index - element index
      valueIfPastBound - value returned if the index is past the current end
      Returns:
      element at the index, or valueIfPastBound
    • set

      public T set(int index, T element)
      May raise ArrayIndexOutOfBoundsException if index is greater or equals than the list's size by the time the lock is acquired. Safe version: setSafe(int, Object).
      Specified by:
      set in interface List<T>
    • setSafe

      public boolean setSafe(int index, T element)
      Safely set an element.
      Parameters:
      index - element index
      element - replacement element
      Returns:
      true if the element was set
    • add

      public void add(int index, T element)
      May raise ArrayIndexOutOfBoundsException if index is greater than the list's size by the time the lock is acquired. Safe version: addSafe(int, Object).
      Specified by:
      add in interface List<T>
    • addSafe

      public boolean addSafe(int index, T element)
      Safely insert an element.
      Parameters:
      index - insertion index
      element - element to insert
      Returns:
      true if the element was inserted
    • add

      public boolean add(T element)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface List<T>
    • addAll

      public boolean addAll(Collection<? extends T> c)
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface List<T>
    • addAll

      public boolean addAll(int index, Collection<? extends T> c)
      May raise ArrayIndexOutOfBoundsException if index is greater than the list's size by the time the lock is acquired.
      Specified by:
      addAll in interface List<T>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface List<T>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<T>
      Specified by:
      containsAll in interface List<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface List<T>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface List<T>
    • remove

      public T remove(int index)
      May raise ArrayIndexOutOfBoundsException if index is greater or equals than the list's size by the time the lock is acquired. Safe version: removeSafe(int).
      Specified by:
      remove in interface List<T>
    • removeSafe

      public boolean removeSafe(int index)
      Safely remove an element by index.
      Parameters:
      index - element index
      Returns:
      true if an element was removed
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface List<T>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<T>
      Specified by:
      retainAll in interface List<T>
    • indexOf

      public int indexOf(Object o)
      Specified by:
      indexOf in interface List<T>
    • lastIndexOf

      public int lastIndexOf(Object o)
      Specified by:
      lastIndexOf in interface List<T>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
    • toArray

      public <U> U[] toArray(U[] a)
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
    • subList

      public List<T> subList(int fromIndex, int toIndex)
      Returns a defensive copy. Modification operations will not impact the original list.
      Specified by:
      subList in interface List<T>
    • listIterator

      public ListIterator<T> listIterator()
      Returns a list iterator that will iterate over a defensive copy. Modification operations will not impact the original list.
      Specified by:
      listIterator in interface List<T>
    • listIterator

      public ListIterator<T> listIterator(int index)
      Returns a list iterator that will iterate over a defensive copy. Modification operations will not impact the original list.
      Specified by:
      listIterator in interface List<T>
    • iterator

      public Iterator<T> iterator()
      Returns an iterator that will iterate over a defensive copy. Modification operations will not impact the original list.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface List<T>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface List<T>
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Collection<T>
      Specified by:
      equals in interface List<T>
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object