org.apache.commons.collections.collection
Class SynchronizedCollection<E>

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

public class SynchronizedCollection<E>
extends Object
implements Collection<E>, 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: 1.1.1.1 $ $Date: 2005/05/23 04:34:02 $
Author:
Matt Hall, John Watkinson, Stephen Colebourne
See Also:
Serialized Form

Field Summary
protected  Collection<E> collection
          The collection to decorate
protected  Object lock
          The object to lock on, needed for List/SortedSet views
 
Constructor Summary
protected SynchronizedCollection(Collection<E> collection)
          Constructor that wraps (not copies).
protected SynchronizedCollection(Collection<E> collection, Object lock)
          Constructor that wraps (not copies).
 
Method Summary
 boolean add(E object)
           
 boolean addAll(Collection<? extends E> coll)
           
 void clear()
           
 boolean contains(Object object)
           
 boolean containsAll(Collection<?> coll)
           
static
<E> Collection<E>
decorate(Collection<E> coll)
          Factory method to create a synchronized collection.
 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()
           
 Object[] toArray()
           
<T> T[]
toArray(T[] object)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

collection

protected final Collection<E> 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<E> 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<E> collection,
                                 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:
IllegalArgumentException - if the collection is null
Method Detail

decorate

public static <E> Collection<E> decorate(Collection<E> 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(E object)
Specified by:
add in interface Collection<E>

addAll

public boolean addAll(Collection<? extends E> coll)
Specified by:
addAll in interface Collection<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>

contains

public boolean contains(Object object)
Specified by:
contains in interface Collection<E>

containsAll

public boolean containsAll(Collection<?> coll)
Specified by:
containsAll in interface Collection<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>

iterator

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

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Returns:
an iterator that must be manually synchronized on the collection

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>

toArray

public <T> T[] toArray(T[] object)
Specified by:
toArray in interface Collection<E>

remove

public boolean remove(Object object)
Specified by:
remove in interface Collection<E>

removeAll

public boolean removeAll(Collection<?> coll)
Specified by:
removeAll in interface Collection<E>

retainAll

public boolean retainAll(Collection<?> coll)
Specified by:
retainAll in interface Collection<E>

size

public int size()
Specified by:
size in interface Collection<E>

equals

public boolean equals(Object object)
Specified by:
equals in interface Collection<E>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<E>
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.