Package com.pnfsoftware.jeb.util.collect
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>
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 Summary
ConstructorsConstructorDescriptionCreate an empty concurrent list.ConcurrentList(int initialCapacity) Create an empty concurrent list.ConcurrentList(Collection<? extends T> collection) Create a concurrent list initialized from a collection. -
Method Summary
Modifier and TypeMethodDescriptionvoidMay raiseArrayIndexOutOfBoundsExceptionif index is greater than the list's size by the time the lock is acquired.booleanbooleanaddAll(int index, Collection<? extends T> c) May raiseArrayIndexOutOfBoundsExceptionif index is greater than the list's size by the time the lock is acquired.booleanaddAll(Collection<? extends T> c) booleanSafely insert an element.voidclear()booleanbooleancontainsAll(Collection<?> c) booleanget(int index) May raiseArrayIndexOutOfBoundsExceptionif index is greater or equals than the list's size by the time the lock is acquired.Safely retrieve an element.inthashCode()intbooleanisEmpty()iterator()Returns an iterator that will iterate over a defensive copy.intReturns a list iterator that will iterate over a defensive copy.listIterator(int index) Returns a list iterator that will iterate over a defensive copy.remove(int index) May raiseArrayIndexOutOfBoundsExceptionif index is greater or equals than the list's size by the time the lock is acquired.booleanbooleanremoveAll(Collection<?> c) booleanremoveSafe(int index) Safely remove an element by index.booleanretainAll(Collection<?> c) May raiseArrayIndexOutOfBoundsExceptionif index is greater or equals than the list's size by the time the lock is acquired.booleanSafely set an element.intsize()subList(int fromIndex, int toIndex) Returns a defensive copy.Object[]toArray()<U> U[]toArray(U[] a) toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, getFirst, getLast, removeFirst, removeLast, replaceAll, reversed, sort, spliterator
-
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
Create a concurrent list initialized from a collection.- Parameters:
collection- source collection
-
-
Method Details
-
isEmpty
public boolean isEmpty() -
size
public int size() -
get
May raiseArrayIndexOutOfBoundsExceptionif index is greater or equals than the list's size by the time the lock is acquired. Safe version:getSafe(int, Object). -
getSafe
Safely retrieve an element.- Parameters:
index- element indexvalueIfPastBound- value returned if the index is past the current end- Returns:
- element at the index, or
valueIfPastBound
-
set
May raiseArrayIndexOutOfBoundsExceptionif index is greater or equals than the list's size by the time the lock is acquired. Safe version:setSafe(int, Object). -
setSafe
Safely set an element.- Parameters:
index- element indexelement- replacement element- Returns:
- true if the element was set
-
add
May raiseArrayIndexOutOfBoundsExceptionif index is greater than the list's size by the time the lock is acquired. Safe version:addSafe(int, Object). -
addSafe
Safely insert an element.- Parameters:
index- insertion indexelement- element to insert- Returns:
- true if the element was inserted
-
add
-
addAll
-
addAll
May raiseArrayIndexOutOfBoundsExceptionif index is greater than the list's size by the time the lock is acquired. -
contains
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceList<T>
-
clear
public void clear() -
remove
-
remove
May raiseArrayIndexOutOfBoundsExceptionif index is greater or equals than the list's size by the time the lock is acquired. Safe version:removeSafe(int). -
removeSafe
public boolean removeSafe(int index) Safely remove an element by index.- Parameters:
index- element index- Returns:
- true if an element was removed
-
removeAll
-
retainAll
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList<T>
-
toArray
-
toArray
public <U> U[] toArray(U[] a) -
subList
Returns a defensive copy. Modification operations will not impact the original list. -
listIterator
Returns a list iterator that will iterate over a defensive copy. Modification operations will not impact the original list.- Specified by:
listIteratorin interfaceList<T>
-
listIterator
Returns a list iterator that will iterate over a defensive copy. Modification operations will not impact the original list.- Specified by:
listIteratorin interfaceList<T>
-
iterator
Returns an iterator that will iterate over a defensive copy. Modification operations will not impact the original list. -
hashCode
public int hashCode() -
equals
-
toString
-