org.apache.commons.collections.collection
Class SynchronizedCollection

java.lang.Object
  extended by org.apache.commons.collections.collection.SynchronizedCollection
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable, java.util.Collection
Direct Known Subclasses:
SynchronizedBag, SynchronizedBuffer, SynchronizedList, SynchronizedSet, SynchronizedSortedSet

public class SynchronizedCollection
extends java.lang.Object
implements java.util.Collection, java.io.Serializable

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 13:33:15 +0100 (Thu, 10 Apr 2008) $
Author:
Stephen Colebourne
See Also:
Serialized Form

Field Summary
protected  java.util.Collection collection
          The collection to decorate
protected  java.lang.Object lock
          The object to lock on, needed for List/SortedSet views
 
Constructor Summary
protected SynchronizedCollection(java.util.Collection collection)
          Constructor that wraps (not copies).
protected SynchronizedCollection(java.util.Collection collection, java.lang.Object lock)
          Constructor that wraps (not copies).
 
Method Summary
 boolean add(java.lang.Object object)
           
 boolean addAll(java.util.Collection coll)
           
 void clear()
           
 boolean contains(java.lang.Object object)
           
 boolean containsAll(java.util.Collection coll)
           
static java.util.Collection decorate(java.util.Collection coll)
          Factory method to create a synchronized collection.
 boolean equals(java.lang.Object object)
           
 int hashCode()
           
 boolean isEmpty()
           
 java.util.Iterator iterator()
          Iterators must be manually synchronized.
 boolean remove(java.lang.Object object)
           
 boolean removeAll(java.util.Collection coll)
           
 boolean retainAll(java.util.Collection coll)
           
 int size()
           
 java.lang.Object[] toArray()
           
 java.lang.Object[] toArray(java.lang.Object[] object)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

collection

protected final java.util.Collection collection
The collection to decorate


lock

protected final java.lang.Object lock
The object to lock on, needed for List/SortedSet views

Constructor Detail

SynchronizedCollection

protected SynchronizedCollection(java.util.Collection collection)
Constructor that wraps (not copies).

Parameters:
collection - the collection to decorate, must not be null
Throws:
java.lang.IllegalArgumentException - if the collection is null

SynchronizedCollection

protected SynchronizedCollection(java.util.Collection collection,
                                 java.lang.Object lock)
Constructor that wraps (not copies).

Parameters:
collection - the collection to decorate, must not be null
lock - the lock object to use, must not be null
Throws:
java.lang.IllegalArgumentException - if the collection is null
Method Detail

decorate

public static java.util.Collection decorate(java.util.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:
java.lang.IllegalArgumentException - if collection is null

add

public boolean add(java.lang.Object object)
Specified by:
add in interface java.util.Collection

addAll

public boolean addAll(java.util.Collection coll)
Specified by:
addAll in interface java.util.Collection

clear

public void clear()
Specified by:
clear in interface java.util.Collection

contains

public boolean contains(java.lang.Object object)
Specified by:
contains in interface java.util.Collection

containsAll

public boolean containsAll(java.util.Collection coll)
Specified by:
containsAll in interface java.util.Collection

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection

iterator

public java.util.Iterator iterator()
Iterators must be manually synchronized.
 synchronized (coll) {
   Iterator it = coll.iterator();
   // do stuff with iterator
 }

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Returns:
an iterator that must be manually synchronized on the collection

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection

toArray

public java.lang.Object[] toArray(java.lang.Object[] object)
Specified by:
toArray in interface java.util.Collection

remove

public boolean remove(java.lang.Object object)
Specified by:
remove in interface java.util.Collection

removeAll

public boolean removeAll(java.util.Collection coll)
Specified by:
removeAll in interface java.util.Collection

retainAll

public boolean retainAll(java.util.Collection coll)
Specified by:
retainAll in interface java.util.Collection

size

public int size()
Specified by:
size in interface java.util.Collection

equals

public boolean equals(java.lang.Object object)
Specified by:
equals in interface java.util.Collection
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Collection
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2001-2008 The Apache Software Foundation. All Rights Reserved.