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
public class SynchronizedCollection extends Object implements Collection, 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:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Stephen Colebourne
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Collection
collection
The collection to decorateprotected Object
lock
The object to lock on, needed for List/SortedSet views
-
Constructor Summary
Constructors Modifier Constructor Description protected
SynchronizedCollection(Collection collection)
Constructor that wraps (not copies).protected
SynchronizedCollection(Collection 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(Object object)
boolean
addAll(Collection coll)
void
clear()
boolean
contains(Object object)
boolean
containsAll(Collection coll)
static Collection
decorate(Collection coll)
Factory method to create a synchronized collection.boolean
equals(Object object)
int
hashCode()
boolean
isEmpty()
Iterator
iterator()
Iterators must be manually synchronized.boolean
remove(Object object)
boolean
removeAll(Collection coll)
boolean
retainAll(Collection coll)
int
size()
Object[]
toArray()
Object[]
toArray(Object[] 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
-
collection
protected final Collection collection
The collection to decorate
-
lock
protected final Object lock
The object to lock on, needed for List/SortedSet views
-
-
Constructor Detail
-
SynchronizedCollection
protected SynchronizedCollection(Collection collection)
Constructor that wraps (not copies).- Parameters:
collection
- the collection to decorate, must not be null- Throws:
IllegalArgumentException
- if the collection is null
-
SynchronizedCollection
protected SynchronizedCollection(Collection 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:
IllegalArgumentException
- if the collection is null
-
-
Method Detail
-
decorate
public static Collection decorate(Collection coll)
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
public boolean add(Object object)
- Specified by:
add
in interfaceCollection
-
addAll
public boolean addAll(Collection coll)
- Specified by:
addAll
in interfaceCollection
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection
-
contains
public boolean contains(Object object)
- Specified by:
contains
in interfaceCollection
-
containsAll
public boolean containsAll(Collection coll)
- Specified by:
containsAll
in interfaceCollection
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceCollection
-
iterator
public Iterator 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
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection
-
toArray
public Object[] toArray(Object[] object)
- Specified by:
toArray
in interfaceCollection
-
remove
public boolean remove(Object object)
- Specified by:
remove
in interfaceCollection
-
removeAll
public boolean removeAll(Collection coll)
- Specified by:
removeAll
in interfaceCollection
-
retainAll
public boolean retainAll(Collection coll)
- Specified by:
retainAll
in interfaceCollection
-
size
public int size()
- Specified by:
size
in interfaceCollection
-
equals
public boolean equals(Object object)
- Specified by:
equals
in interfaceCollection
- Overrides:
equals
in classObject
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfaceCollection
- Overrides:
hashCode
in classObject
-
-