--- a/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java Tue Jul 02 15:58:09 2013 -0700
+++ b/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java Wed Jul 03 11:58:09 2013 +0200
@@ -34,7 +34,14 @@
*/
package java.util.concurrent;
-import java.util.*;
+import java.util.Collection;
+import java.util.Set;
+import java.util.AbstractSet;
+import java.util.Iterator;
+import java.util.Spliterator;
+import java.util.Spliterators;
+import java.util.function.Predicate;
+import java.util.function.Consumer;
/**
* A {@link java.util.Set} that uses an internal {@link CopyOnWriteArrayList}
@@ -45,17 +52,17 @@
* vastly outnumber mutative operations, and you need
* to prevent interference among threads during traversal.
* <li>It is thread-safe.
- * <li>Mutative operations (<tt>add</tt>, <tt>set</tt>, <tt>remove</tt>, etc.)
+ * <li>Mutative operations ({@code add}, {@code set}, {@code remove}, etc.)
* are expensive since they usually entail copying the entire underlying
* array.
- * <li>Iterators do not support the mutative <tt>remove</tt> operation.
+ * <li>Iterators do not support the mutative {@code remove} operation.
* <li>Traversal via iterators is fast and cannot encounter
* interference from other threads. Iterators rely on
* unchanging snapshots of the array at the time the iterators were
* constructed.
* </ul>
*
- * <p> <b>Sample Usage.</b> The following code sketch uses a
+ * <p><b>Sample Usage.</b> The following code sketch uses a
* copy-on-write set to maintain a set of Handler objects that
* perform some action upon state updates.
*
@@ -73,7 +80,7 @@
* public void update() {
* changeState();
* for (Handler handler : handlers)
- * handler.handle();
+ * handler.handle();
* }
* }}</pre>
*
@@ -107,8 +114,15 @@
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArraySet(Collection<? extends E> c) {
- al = new CopyOnWriteArrayList<E>();
- al.addAllAbsent(c);
+ if (c.getClass() == CopyOnWriteArraySet.class) {
+ @SuppressWarnings("unchecked") CopyOnWriteArraySet<E> cc =
+ (CopyOnWriteArraySet<E>)c;
+ al = new CopyOnWriteArrayList<E>(cc.al);
+ }
+ else {
+ al = new CopyOnWriteArrayList<E>();
+ al.addAllAbsent(c);
+ }
}
/**
@@ -121,22 +135,22 @@
}
/**
- * Returns <tt>true</tt> if this set contains no elements.
+ * Returns {@code true} if this set contains no elements.
*
- * @return <tt>true</tt> if this set contains no elements
+ * @return {@code true} if this set contains no elements
*/
public boolean isEmpty() {
return al.isEmpty();
}
/**
- * Returns <tt>true</tt> if this set contains the specified element.
- * More formally, returns <tt>true</tt> if and only if this set
- * contains an element <tt>e</tt> such that
+ * Returns {@code true} if this set contains the specified element.
+ * More formally, returns {@code true} if and only if this set
+ * contains an element {@code e} such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this set is to be tested
- * @return <tt>true</tt> if this set contains the specified element
+ * @return {@code true} if this set contains the specified element
*/
public boolean contains(Object o) {
return al.contains(o);
@@ -172,7 +186,7 @@
* <p>If this set fits in the specified array with room to spare
* (i.e., the array has more elements than this set), the element in
* the array immediately following the end of the set is set to
- * <tt>null</tt>. (This is useful in determining the length of this
+ * {@code null}. (This is useful in determining the length of this
* set <i>only</i> if the caller knows that this set does not contain
* any null elements.)
*
@@ -185,14 +199,14 @@
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
- * <p>Suppose <tt>x</tt> is a set known to contain only strings.
+ * <p>Suppose {@code x} is a set known to contain only strings.
* The following code can be used to dump the set into a newly allocated
- * array of <tt>String</tt>:
+ * array of {@code String}:
*
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
*
- * Note that <tt>toArray(new Object[0])</tt> is identical in function to
- * <tt>toArray()</tt>.
+ * Note that {@code toArray(new Object[0])} is identical in function to
+ * {@code toArray()}.
*
* @param a the array into which the elements of this set are to be
* stored, if it is big enough; otherwise, a new array of the same
@@ -217,15 +231,15 @@
/**
* Removes the specified element from this set if it is present.
- * More formally, removes an element <tt>e</tt> such that
+ * More formally, removes an element {@code e} such that
* <tt>(o==null ? e==null : o.equals(e))</tt>,
- * if this set contains such an element. Returns <tt>true</tt> if
+ * if this set contains such an element. Returns {@code true} if
* this set contained the element (or equivalently, if this set
* changed as a result of the call). (This set will not contain the
* element once the call returns.)
*
* @param o object to be removed from this set, if present
- * @return <tt>true</tt> if this set contained the specified element
+ * @return {@code true} if this set contained the specified element
*/
public boolean remove(Object o) {
return al.remove(o);
@@ -233,14 +247,14 @@
/**
* Adds the specified element to this set if it is not already present.
- * More formally, adds the specified element <tt>e</tt> to this set if
- * the set contains no element <tt>e2</tt> such that
+ * More formally, adds the specified element {@code e} to this set if
+ * the set contains no element {@code e2} such that
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
- * unchanged and returns <tt>false</tt>.
+ * unchanged and returns {@code false}.
*
* @param e element to be added to this set
- * @return <tt>true</tt> if this set did not already contain the specified
+ * @return {@code true} if this set did not already contain the specified
* element
*/
public boolean add(E e) {
@@ -248,12 +262,12 @@
}
/**
- * Returns <tt>true</tt> if this set contains all of the elements of the
+ * Returns {@code true} if this set contains all of the elements of the
* specified collection. If the specified collection is also a set, this
- * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
+ * method returns {@code true} if it is a <i>subset</i> of this set.
*
* @param c collection to be checked for containment in this set
- * @return <tt>true</tt> if this set contains all of the elements of the
+ * @return {@code true} if this set contains all of the elements of the
* specified collection
* @throws NullPointerException if the specified collection is null
* @see #contains(Object)
@@ -265,13 +279,13 @@
/**
* Adds all of the elements in the specified collection to this set if
* they're not already present. If the specified collection is also a
- * set, the <tt>addAll</tt> operation effectively modifies this set so
+ * set, the {@code addAll} operation effectively modifies this set so
* that its value is the <i>union</i> of the two sets. The behavior of
* this operation is undefined if the specified collection is modified
* while the operation is in progress.
*
* @param c collection containing elements to be added to this set
- * @return <tt>true</tt> if this set changed as a result of the call
+ * @return {@code true} if this set changed as a result of the call
* @throws NullPointerException if the specified collection is null
* @see #add(Object)
*/
@@ -286,7 +300,7 @@
* <i>asymmetric set difference</i> of the two sets.
*
* @param c collection containing elements to be removed from this set
- * @return <tt>true</tt> if this set changed as a result of the call
+ * @return {@code true} if this set changed as a result of the call
* @throws ClassCastException if the class of an element of this set
* is incompatible with the specified collection (optional)
* @throws NullPointerException if this set contains a null element and the
@@ -307,7 +321,7 @@
* two sets.
*
* @param c collection containing elements to be retained in this set
- * @return <tt>true</tt> if this set changed as a result of the call
+ * @return {@code true} if this set changed as a result of the call
* @throws ClassCastException if the class of an element of this set
* is incompatible with the specified collection (optional)
* @throws NullPointerException if this set contains a null element and the
@@ -326,7 +340,7 @@
* <p>The returned iterator provides a snapshot of the state of the set
* when the iterator was constructed. No synchronization is needed while
* traversing the iterator. The iterator does <em>NOT</em> support the
- * <tt>remove</tt> method.
+ * {@code remove} method.
*
* @return an iterator over the elements in this set
*/
@@ -338,7 +352,7 @@
* Compares the specified object with this set for equality.
* Returns {@code true} if the specified object is the same object
* as this object, or if it is also a {@link Set} and the elements
- * returned by an {@linkplain List#iterator() iterator} over the
+ * returned by an {@linkplain Set#iterator() iterator} over the
* specified set are the same as the elements returned by an
* iterator over this set. More formally, the two iterators are
* considered to return the same elements if they return the same
@@ -382,6 +396,19 @@
return k == len;
}
+ public boolean removeIf(Predicate<? super E> filter) {
+ return al.removeIf(filter);
+ }
+
+ public void forEach(Consumer<? super E> action) {
+ al.forEach(action);
+ }
+
+ public Spliterator<E> spliterator() {
+ return Spliterators.spliterator
+ (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT);
+ }
+
/**
* Tests for equality, coping with nulls.
*/