Class 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


    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:
    3.0
    See Also:
    Serialized Form
    • 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 null
        lock - 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)
      • addAll

        public boolean addAll(Collection<? extends E> coll)
      • clear

        public void clear()
      • contains

        public boolean contains(Object object)
      • containsAll

        public boolean containsAll(Collection<?> coll)
      • isEmpty

        public boolean isEmpty()
      • 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()
      • toArray

        public <T> T[] toArray(T[] object)
      • remove

        public boolean remove(Object object)
      • removeAll

        public boolean removeAll(Collection<?> coll)
      • retainAll

        public boolean retainAll(Collection<?> coll)
      • size

        public int size()
      • equals

        public boolean equals(Object object)
      • hashCode

        public int hashCode()
      • toString

        public String toString()