SynchronizedCollection.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.collection;

  18. import java.io.Serializable;
  19. import java.util.Collection;
  20. import java.util.Iterator;
  21. import java.util.Objects;
  22. import java.util.function.Predicate;

  23. /**
  24.  * Decorates another {@link Collection} to synchronize its behavior
  25.  * for a multithreaded environment.
  26.  * <p>
  27.  * Iterators must be manually synchronized:
  28.  * </p>
  29.  * <pre>
  30.  * synchronized (coll) {
  31.  *   Iterator it = coll.iterator();
  32.  *   // do stuff with iterator
  33.  * }
  34.  * </pre>
  35.  * <p>
  36.  * This class is Serializable from Commons Collections 3.1.
  37.  * </p>
  38.  *
  39.  * @param <E> the type of the elements in the collection
  40.  * @since 3.0
  41.  */
  42. public class SynchronizedCollection<E> implements Collection<E>, Serializable {

  43.     /** Serialization version */
  44.     private static final long serialVersionUID = 2412805092710877986L;

  45.     /**
  46.      * Factory method to create a synchronized collection.
  47.      *
  48.      * @param <T> the type of the elements in the collection
  49.      * @param coll  the collection to decorate, must not be null
  50.      * @return a new synchronized collection
  51.      * @throws NullPointerException if collection is null
  52.      * @since 4.0
  53.      */
  54.     public static <T> SynchronizedCollection<T> synchronizedCollection(final Collection<T> coll) {
  55.         return new SynchronizedCollection<>(coll);
  56.     }
  57.     /** The collection to decorate */
  58.     private final Collection<E> collection;

  59.     /** The object to lock on, needed for List/SortedSet views */
  60.     protected final Object lock;

  61.     /**
  62.      * Constructor that wraps (not copies).
  63.      *
  64.      * @param collection  the collection to decorate, must not be null
  65.      * @throws NullPointerException if the collection is null
  66.      */
  67.     protected SynchronizedCollection(final Collection<E> collection) {
  68.         this.collection = Objects.requireNonNull(collection, "collection");
  69.         this.lock = this;
  70.     }

  71.     /**
  72.      * Constructor that wraps (not copies).
  73.      *
  74.      * @param collection  the collection to decorate, must not be null
  75.      * @param lock  the lock object to use, must not be null
  76.      * @throws NullPointerException if the collection or lock is null
  77.      */
  78.     protected SynchronizedCollection(final Collection<E> collection, final Object lock) {
  79.         this.collection = Objects.requireNonNull(collection, "collection");
  80.         this.lock = Objects.requireNonNull(lock, "lock");
  81.     }

  82.     @Override
  83.     public boolean add(final E object) {
  84.         synchronized (lock) {
  85.             return decorated().add(object);
  86.         }
  87.     }

  88.     @Override
  89.     public boolean addAll(final Collection<? extends E> coll) {
  90.         synchronized (lock) {
  91.             return decorated().addAll(coll);
  92.         }
  93.     }

  94.     @Override
  95.     public void clear() {
  96.         synchronized (lock) {
  97.             decorated().clear();
  98.         }
  99.     }

  100.     @Override
  101.     public boolean contains(final Object object) {
  102.         synchronized (lock) {
  103.             return decorated().contains(object);
  104.         }
  105.     }

  106.     @Override
  107.     public boolean containsAll(final Collection<?> coll) {
  108.         synchronized (lock) {
  109.             return decorated().containsAll(coll);
  110.         }
  111.     }

  112.     /**
  113.      * Gets the collection being decorated.
  114.      *
  115.      * @return the decorated collection
  116.      */
  117.     protected Collection<E> decorated() {
  118.         return collection;
  119.     }

  120.     @Override
  121.     public boolean equals(final Object object) {
  122.         synchronized (lock) {
  123.             if (object == this) {
  124.                 return true;
  125.             }
  126.             return object == this || decorated().equals(object);
  127.         }
  128.     }

  129.     @Override
  130.     public int hashCode() {
  131.         synchronized (lock) {
  132.             return decorated().hashCode();
  133.         }
  134.     }

  135.     @Override
  136.     public boolean isEmpty() {
  137.         synchronized (lock) {
  138.             return decorated().isEmpty();
  139.         }
  140.     }

  141.     /**
  142.      * Iterators must be manually synchronized.
  143.      * <pre>
  144.      * synchronized (coll) {
  145.      *   Iterator it = coll.iterator();
  146.      *   // do stuff with iterator
  147.      * }
  148.      * </pre>
  149.      *
  150.      * @return an iterator that must be manually synchronized on the collection
  151.      */
  152.     @Override
  153.     public Iterator<E> iterator() {
  154.         return decorated().iterator();
  155.     }

  156.     @Override
  157.     public boolean remove(final Object object) {
  158.         synchronized (lock) {
  159.             return decorated().remove(object);
  160.         }
  161.     }

  162.     @Override
  163.     public boolean removeAll(final Collection<?> coll) {
  164.         synchronized (lock) {
  165.             return decorated().removeAll(coll);
  166.         }
  167.     }

  168.     /**
  169.      * @since 4.4
  170.      */
  171.     @Override
  172.     public boolean removeIf(final Predicate<? super E> filter) {
  173.         synchronized (lock) {
  174.             return decorated().removeIf(filter);
  175.         }
  176.     }

  177.     @Override
  178.     public boolean retainAll(final Collection<?> coll) {
  179.         synchronized (lock) {
  180.             return decorated().retainAll(coll);
  181.         }
  182.     }

  183.     @Override
  184.     public int size() {
  185.         synchronized (lock) {
  186.             return decorated().size();
  187.         }
  188.     }

  189.     @Override
  190.     public Object[] toArray() {
  191.         synchronized (lock) {
  192.             return decorated().toArray();
  193.         }
  194.     }

  195.     @Override
  196.     public <T> T[] toArray(final T[] object) {
  197.         synchronized (lock) {
  198.             return decorated().toArray(object);
  199.         }
  200.     }

  201.     @Override
  202.     public String toString() {
  203.         synchronized (lock) {
  204.             return decorated().toString();
  205.         }
  206.     }

  207. }