66 * reference to the state of the array at the point that the iterator
67 * was created. This array never changes during the lifetime of the
68 * iterator, so interference is impossible and the iterator is
69 * guaranteed not to throw {@code ConcurrentModificationException}.
70 * The iterator will not reflect additions, removals, or changes to
71 * the list since the iterator was created. Element-changing
72 * operations on iterators themselves ({@code remove}, {@code set}, and
73 * {@code add}) are not supported. These methods throw
74 * {@code UnsupportedOperationException}.
75 *
76 * <p>All elements are permitted, including {@code null}.
77 *
78 * <p>Memory consistency effects: As with other concurrent
79 * collections, actions in a thread prior to placing an object into a
80 * {@code CopyOnWriteArrayList}
81 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
82 * actions subsequent to the access or removal of that element from
83 * the {@code CopyOnWriteArrayList} in another thread.
84 *
85 * <p>This class is a member of the
86 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
87 * Java Collections Framework</a>.
88 *
89 * @since 1.5
90 * @author Doug Lea
91 * @param <E> the type of elements held in this list
92 */
93 public class CopyOnWriteArrayList<E>
94 implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
95 private static final long serialVersionUID = 8673264195747942595L;
96
97 /**
98 * The lock protecting all mutators. (We have a mild preference
99 * for builtin monitors over ReentrantLock when either will do.)
100 */
101 final transient Object lock = new Object();
102
103 /** The array, accessed only via getArray/setArray. */
104 private transient volatile Object[] array;
105
106 /**
628 */
629 public boolean containsAll(Collection<?> c) {
630 Object[] elements = getArray();
631 int len = elements.length;
632 for (Object e : c) {
633 if (indexOf(e, elements, 0, len) < 0)
634 return false;
635 }
636 return true;
637 }
638
639 /**
640 * Removes from this list all of its elements that are contained in
641 * the specified collection. This is a particularly expensive operation
642 * in this class because of the need for an internal temporary array.
643 *
644 * @param c collection containing elements to be removed from this list
645 * @return {@code true} if this list changed as a result of the call
646 * @throws ClassCastException if the class of an element of this list
647 * is incompatible with the specified collection
648 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
649 * @throws NullPointerException if this list contains a null element and the
650 * specified collection does not permit null elements
651 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
652 * or if the specified collection is null
653 * @see #remove(Object)
654 */
655 public boolean removeAll(Collection<?> c) {
656 Objects.requireNonNull(c);
657 return bulkRemove(e -> c.contains(e));
658 }
659
660 /**
661 * Retains only the elements in this list that are contained in the
662 * specified collection. In other words, removes from this list all of
663 * its elements that are not contained in the specified collection.
664 *
665 * @param c collection containing elements to be retained in this list
666 * @return {@code true} if this list changed as a result of the call
667 * @throws ClassCastException if the class of an element of this list
668 * is incompatible with the specified collection
669 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
670 * @throws NullPointerException if this list contains a null element and the
671 * specified collection does not permit null elements
672 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
673 * or if the specified collection is null
674 * @see #remove(Object)
675 */
676 public boolean retainAll(Collection<?> c) {
677 Objects.requireNonNull(c);
678 return bulkRemove(e -> !c.contains(e));
679 }
680
681 /**
682 * Appends all of the elements in the specified collection that
683 * are not already contained in this list, to the end of
684 * this list, in the order that they are returned by the
685 * specified collection's iterator.
686 *
687 * @param c collection containing elements to be added to this list
688 * @return the number of elements added
689 * @throws NullPointerException if the specified collection is null
690 * @see #addIfAbsent(Object)
691 */
692 public int addAllAbsent(Collection<? extends E> c) {
|
66 * reference to the state of the array at the point that the iterator
67 * was created. This array never changes during the lifetime of the
68 * iterator, so interference is impossible and the iterator is
69 * guaranteed not to throw {@code ConcurrentModificationException}.
70 * The iterator will not reflect additions, removals, or changes to
71 * the list since the iterator was created. Element-changing
72 * operations on iterators themselves ({@code remove}, {@code set}, and
73 * {@code add}) are not supported. These methods throw
74 * {@code UnsupportedOperationException}.
75 *
76 * <p>All elements are permitted, including {@code null}.
77 *
78 * <p>Memory consistency effects: As with other concurrent
79 * collections, actions in a thread prior to placing an object into a
80 * {@code CopyOnWriteArrayList}
81 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
82 * actions subsequent to the access or removal of that element from
83 * the {@code CopyOnWriteArrayList} in another thread.
84 *
85 * <p>This class is a member of the
86 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
87 * Java Collections Framework</a>.
88 *
89 * @since 1.5
90 * @author Doug Lea
91 * @param <E> the type of elements held in this list
92 */
93 public class CopyOnWriteArrayList<E>
94 implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
95 private static final long serialVersionUID = 8673264195747942595L;
96
97 /**
98 * The lock protecting all mutators. (We have a mild preference
99 * for builtin monitors over ReentrantLock when either will do.)
100 */
101 final transient Object lock = new Object();
102
103 /** The array, accessed only via getArray/setArray. */
104 private transient volatile Object[] array;
105
106 /**
628 */
629 public boolean containsAll(Collection<?> c) {
630 Object[] elements = getArray();
631 int len = elements.length;
632 for (Object e : c) {
633 if (indexOf(e, elements, 0, len) < 0)
634 return false;
635 }
636 return true;
637 }
638
639 /**
640 * Removes from this list all of its elements that are contained in
641 * the specified collection. This is a particularly expensive operation
642 * in this class because of the need for an internal temporary array.
643 *
644 * @param c collection containing elements to be removed from this list
645 * @return {@code true} if this list changed as a result of the call
646 * @throws ClassCastException if the class of an element of this list
647 * is incompatible with the specified collection
648 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
649 * @throws NullPointerException if this list contains a null element and the
650 * specified collection does not permit null elements
651 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
652 * or if the specified collection is null
653 * @see #remove(Object)
654 */
655 public boolean removeAll(Collection<?> c) {
656 Objects.requireNonNull(c);
657 return bulkRemove(e -> c.contains(e));
658 }
659
660 /**
661 * Retains only the elements in this list that are contained in the
662 * specified collection. In other words, removes from this list all of
663 * its elements that are not contained in the specified collection.
664 *
665 * @param c collection containing elements to be retained in this list
666 * @return {@code true} if this list changed as a result of the call
667 * @throws ClassCastException if the class of an element of this list
668 * is incompatible with the specified collection
669 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
670 * @throws NullPointerException if this list contains a null element and the
671 * specified collection does not permit null elements
672 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
673 * or if the specified collection is null
674 * @see #remove(Object)
675 */
676 public boolean retainAll(Collection<?> c) {
677 Objects.requireNonNull(c);
678 return bulkRemove(e -> !c.contains(e));
679 }
680
681 /**
682 * Appends all of the elements in the specified collection that
683 * are not already contained in this list, to the end of
684 * this list, in the order that they are returned by the
685 * specified collection's iterator.
686 *
687 * @param c collection containing elements to be added to this list
688 * @return the number of elements added
689 * @throws NullPointerException if the specified collection is null
690 * @see #addIfAbsent(Object)
691 */
692 public int addAllAbsent(Collection<? extends E> c) {
|