Class SynchronizedCollection
java.lang.Object
org.apache.commons.collections.collection.SynchronizedCollection
- All Implemented Interfaces:
Serializable
,Iterable
,Collection
- Direct Known Subclasses:
SynchronizedBag
,SynchronizedBuffer
,SynchronizedList
,SynchronizedSet
,SynchronizedSortedSet
Decorates another
Collection
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:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Stephen Colebourne
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Collection
The collection to decorateprotected final Object
The object to lock on, needed for List/SortedSet views -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SynchronizedCollection
(Collection collection) Constructor that wraps (not copies).protected
SynchronizedCollection
(Collection collection, Object lock) Constructor that wraps (not copies). -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection coll) void
clear()
boolean
boolean
containsAll
(Collection coll) static Collection
decorate
(Collection coll) Factory method to create a synchronized collection.boolean
int
hashCode()
boolean
isEmpty()
iterator()
Iterators must be manually synchronized.boolean
boolean
removeAll
(Collection coll) boolean
retainAll
(Collection coll) int
size()
Object[]
toArray()
Object[]
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 Details
-
collection
The collection to decorate -
lock
The object to lock on, needed for List/SortedSet views
-
-
Constructor Details
-
SynchronizedCollection
Constructor that wraps (not copies).- Parameters:
collection
- the collection to decorate, must not be null- Throws:
IllegalArgumentException
- if the collection is null
-
SynchronizedCollection
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:
IllegalArgumentException
- if the collection is null
-
-
Method Details
-
decorate
Factory method to create a synchronized collection.- Parameters:
coll
- the collection to decorate, must not be null- Returns:
- a new synchronized collection
- Throws:
IllegalArgumentException
- if collection is null
-
add
- Specified by:
add
in interfaceCollection
-
addAll
- Specified by:
addAll
in interfaceCollection
-
clear
public void clear()- Specified by:
clear
in interfaceCollection
-
contains
- Specified by:
contains
in interfaceCollection
-
containsAll
- Specified by:
containsAll
in interfaceCollection
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection
-
iterator
Iterators must be manually synchronized.synchronized (coll) { Iterator it = coll.iterator(); // do stuff with iterator }
- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- Returns:
- an iterator that must be manually synchronized on the collection
-
toArray
- Specified by:
toArray
in interfaceCollection
-
toArray
- Specified by:
toArray
in interfaceCollection
-
remove
- Specified by:
remove
in interfaceCollection
-
removeAll
- Specified by:
removeAll
in interfaceCollection
-
retainAll
- Specified by:
retainAll
in interfaceCollection
-
size
public int size()- Specified by:
size
in interfaceCollection
-
equals
- Specified by:
equals
in interfaceCollection
- Overrides:
equals
in classObject
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection
- Overrides:
hashCode
in classObject
-
toString
-