ConcurrentCollection
Thread-safe implementation of the Collection interface. Implements all collection operations, but performance is optimized for iterator-based access and sequential addition (to, both ends of the collection) as it is used as part of the entities and scene logic in Artenus.
Java's standard LinkedList implementation is not thread-safe and hence it is programmed to throw exceptions on concurrent modification and access scenarios. There are some alternatives in the standard library, but Artenus uses this minimalistic collection implementation for its internal use. You can use this class if you need a synchronized sequential-access collection.
The size, isEmpty, iterator, add, prepend, getFirst, and getLast operations are guaranteed to run in constant time. However, no assumptions should be made about the running time of all the other operations, as this implementation does not guarantee optimized behavior for them.
Unlike implementations like LinkedList, iterators returned by this class's iterator and methods are not fail-fast: if the list is structurally modified after the iterator is created, the iterator will not throw any exceptions, and will adapt to the new state of the collection.
public class ConcurrentCollection implements Collection<T>
Constructor Summary
Modifier and Type | Constructor and Description |
---|---|
public | ConcurrentCollection() |
Method Summary
Modifier and Type | Method and Description |
---|---|
public boolean | add(T object) Adds an element to the back of the collection. |
public boolean | addAll(Collection<? extends T> collection) Appends all of the elements in the specified collection to the end of this collection, in the
order that they are returned by the specified collection's Iterator. |
public void | clear() Removes all of the elements from this collection. |
public boolean | contains(Object object) Returns true if this list contains the specified element. |
public boolean | containsAll(Collection<?> collection) Returns true if this collection contains all of the elements in the specified collection. |
public T | getFirst() Gets the first element in this collection (the element at the front of the collection). |
public T | getLast() Gets the last element in this collection (the element at the back of the collection). |
public boolean | isEmpty() Indicates whether this collection contains no elements. |
public Iterator<T> | iterator() Returns an iterator over the elements in this list in proper sequence. |
public void | prepend(T object) Inserts an element to the front of the collection. |
public boolean | remove(Object object) Removes the first occurrence of the specified element from this collection, if it is present. |
public boolean | removeAll(Collection<?> collection) Removes from this collection all of its elements that are contained in the specified
collection. |
public boolean | retainAll(Collection<?> collection) Retains only the elements in this list that are contained in the specified collection. |
public int | size() Returns the number of elements in this collection. |
public Object | toArray() Returns an array containing all of the elements in this collection in proper sequence (from
first to last element). |
public T1 | toArray(T1[] array) This method is not implemented by this class, and should not be called. |
Methods inherited from java.lang.Object
clone
, equals
, finalize
, getClass
, hashCode
, notify
, notifyAll
, toString
, wait
, wait
, wait
Method Detail
add | ||
---|---|---|
public boolean add( T object ) Adds an element to the back of the collection. Parameters:
Specified By:
add in Collection |
addAll | ||
---|---|---|
public boolean addAll( Collection<? extends T> collection ) Appends all of the elements in the specified collection to the end of this collection, in the
order that they are returned by the specified collection's Iterator. The behavior of this
operation is undefined if the specified collection is modified while the operation is in
progress. (This implies that the behavior of this call is undefined if the specified
collection is this list, and this list is nonempty.) Parameters:
Specified By:
addAll in Collection |
clear |
---|
public void clear() Removes all of the elements from this collection. The collection will be empty after this
call returns. However, existing iterators would not notice this change and would function as
they would before this call. Specified By:
clear in Collection |
contains | ||
---|---|---|
public boolean contains( Object object ) Returns true if this list contains the specified element. More formally, returns true
if and only if this list contains at least one element e such that (o==null ? e==null :
o.equals(e)). Parameters:
Specified By:
contains in Collection |
containsAll | ||
---|---|---|
public boolean containsAll( Collection<?> collection ) Returns true if this collection contains all of the elements in the specified collection. This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained true is returned, otherwise false. Parameters:
Specified By:
containsAll in Collection |
getFirst |
---|
public T getFirst() Gets the first element in this collection (the element at the front of the collection). Returns:
The first element of this collection |
getLast |
---|
public T getLast() Gets the last element in this collection (the element at the back of the collection). Returns:
The last element of this collection |
isEmpty |
---|
public boolean isEmpty() Indicates whether this collection contains no elements. Specified By:
isEmpty in Collection |
iterator |
---|
public Iterator<T> iterator() Returns an iterator over the elements in this list in proper sequence. The returned iterator
supports removal, and will not fail in case of concurrent modification in the collection. Specified By:
iterator in Collection |
prepend | ||
---|---|---|
public void prepend( T object ) Inserts an element to the front of the collection. Parameters:
|
remove | ||
---|---|---|
public boolean remove( Object object ) Removes the first occurrence of the specified element from this collection, if it is present.
If the list does not contain the element, it is unchanged. More formally, removes the element
with the lowest distance from the front of the collection such that (o==null ? get(i)==null :
o.equals(get(i))) (if such an element exists). Returns true if this list contained the
specified element (or equivalently, if this list changed as a result of the call). Parameters:
Specified By:
remove in Collection |
removeAll | ||
---|---|---|
public boolean removeAll( Collection<?> collection ) Removes from this collection all of its elements that are contained in the specified
collection. Parameters:
Specified By:
removeAll in Collection |
retainAll | ||
---|---|---|
public boolean retainAll( Collection<?> collection ) Retains only the elements in this list that are contained in the specified collection. In
other words, removes from this list all of its elements that are not contained in the
specified collection. Parameters:
Specified By:
retainAll in Collection |
size |
---|
public int size() Returns the number of elements in this collection. Specified By:
size in Collection |
toArray |
---|
public Object toArray() Returns an array containing all of the elements in this collection in proper sequence (from
first to last element). The returned array will be "safe" in that no references to it are
maintained by this collection. (In other words, this method must allocate a new array). The
caller is thus free to modify the returned array. This method acts as bridge between
array-based and collection-based APIs. Specified By:
toArray in Collection |
toArray | ||
---|---|---|
public T1 toArray( T1[] array ) This method is not implemented by this class, and should not be called. Parameters:
Specified By:
toArray in Collection Throws:
UnsupportedOperationException |