Class SynchronizedCollection<E>
- java.lang.Object
-
- org.apache.commons.collections4.collection.SynchronizedCollection<E>
-
- Type Parameters:
E
- the type of the elements in the collection
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
- Direct Known Subclasses:
SynchronizedBag
,SynchronizedMultiSet
,SynchronizedQueue
public class SynchronizedCollection<E> extends Object implements Collection<E>, Serializable
Decorates anotherCollection
to synchronize its behaviour for a multi-threaded environment.Iterators must be manually synchronized:
synchronized (coll) { Iterator it = coll.iterator(); // do stuff with iterator }
This class is Serializable from Commons Collections 3.1.
- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SynchronizedCollection(Collection<E> collection)
Constructor that wraps (not copies).protected
SynchronizedCollection(Collection<E> collection, Object lock)
Constructor that wraps (not copies).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E object)
boolean
addAll(Collection<? extends E> coll)
void
clear()
boolean
contains(Object object)
boolean
containsAll(Collection<?> coll)
protected Collection<E>
decorated()
Gets the collection being decorated.boolean
equals(Object object)
int
hashCode()
boolean
isEmpty()
Iterator<E>
iterator()
Iterators must be manually synchronized.boolean
remove(Object object)
boolean
removeAll(Collection<?> coll)
boolean
retainAll(Collection<?> coll)
int
size()
static <T> SynchronizedCollection<T>
synchronizedCollection(Collection<T> coll)
Factory method to create a synchronized collection.Object[]
toArray()
<T> T[]
toArray(T[] object)
String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream, toArray
-
-
-
-
Field Detail
-
lock
protected final Object lock
The object to lock on, needed for List/SortedSet views
-
-
Constructor Detail
-
SynchronizedCollection
protected SynchronizedCollection(Collection<E> collection)
Constructor that wraps (not copies).- Parameters:
collection
- the collection to decorate, must not be null- Throws:
NullPointerException
- if the collection is null
-
SynchronizedCollection
protected SynchronizedCollection(Collection<E> collection, Object lock)
Constructor that wraps (not copies).- Parameters:
collection
- the collection to decorate, must not be nulllock
- the lock object to use, must not be null- Throws:
NullPointerException
- if the collection or lock is null
-
-
Method Detail
-
synchronizedCollection
public static <T> SynchronizedCollection<T> synchronizedCollection(Collection<T> coll)
Factory method to create a synchronized collection.- Type Parameters:
T
- the type of the elements in the collection- Parameters:
coll
- the collection to decorate, must not be null- Returns:
- a new synchronized collection
- Throws:
NullPointerException
- if collection is null- Since:
- 4.0
-
decorated
protected Collection<E> decorated()
Gets the collection being decorated.- Returns:
- the decorated collection
-
add
public boolean add(E object)
- Specified by:
add
in interfaceCollection<E>
-
addAll
public boolean addAll(Collection<? extends E> coll)
- Specified by:
addAll
in interfaceCollection<E>
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection<E>
-
contains
public boolean contains(Object object)
- Specified by:
contains
in interfaceCollection<E>
-
containsAll
public boolean containsAll(Collection<?> coll)
- Specified by:
containsAll
in interfaceCollection<E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceCollection<E>
-
iterator
public Iterator<E> iterator()
Iterators must be manually synchronized.synchronized (coll) { Iterator it = coll.iterator(); // do stuff with iterator }
-
toArray
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] object)
- Specified by:
toArray
in interfaceCollection<E>
-
remove
public boolean remove(Object object)
- Specified by:
remove
in interfaceCollection<E>
-
removeAll
public boolean removeAll(Collection<?> coll)
- Specified by:
removeAll
in interfaceCollection<E>
-
retainAll
public boolean retainAll(Collection<?> coll)
- Specified by:
retainAll
in interfaceCollection<E>
-
size
public int size()
- Specified by:
size
in interfaceCollection<E>
-
equals
public boolean equals(Object object)
- Specified by:
equals
in interfaceCollection<E>
- Overrides:
equals
in classObject
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfaceCollection<E>
- Overrides:
hashCode
in classObject
-
-