提交 7bf1893e 编写于 作者: P psandoz

8011427: java.util.concurrent collection Spliterator implementations

Reviewed-by: martin
Contributed-by: NDoug Lea <dl@cs.oswego.edu>
上级 2bfa31ee
...@@ -41,17 +41,18 @@ import java.util.*; ...@@ -41,17 +41,18 @@ import java.util.*;
* for the deque to become non-empty when retrieving an element, and wait for * for the deque to become non-empty when retrieving an element, and wait for
* space to become available in the deque when storing an element. * space to become available in the deque when storing an element.
* *
* <p><tt>BlockingDeque</tt> methods come in four forms, with different ways * <p>{@code BlockingDeque} methods come in four forms, with different ways
* of handling operations that cannot be satisfied immediately, but may be * of handling operations that cannot be satisfied immediately, but may be
* satisfied at some point in the future: * satisfied at some point in the future:
* one throws an exception, the second returns a special value (either * one throws an exception, the second returns a special value (either
* <tt>null</tt> or <tt>false</tt>, depending on the operation), the third * {@code null} or {@code false}, depending on the operation), the third
* blocks the current thread indefinitely until the operation can succeed, * blocks the current thread indefinitely until the operation can succeed,
* and the fourth blocks for only a given maximum time limit before giving * and the fourth blocks for only a given maximum time limit before giving
* up. These methods are summarized in the following table: * up. These methods are summarized in the following table:
* *
* <p> * <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1> * <table BORDER CELLPADDING=3 CELLSPACING=1>
* <caption>Summary of BlockingDeque methods</caption>
* <tr> * <tr>
* <td ALIGN=CENTER COLSPAN = 5> <b>First Element (Head)</b></td> * <td ALIGN=CENTER COLSPAN = 5> <b>First Element (Head)</b></td>
* </tr> * </tr>
...@@ -116,20 +117,21 @@ import java.util.*; ...@@ -116,20 +117,21 @@ import java.util.*;
* </tr> * </tr>
* </table> * </table>
* *
* <p>Like any {@link BlockingQueue}, a <tt>BlockingDeque</tt> is thread safe, * <p>Like any {@link BlockingQueue}, a {@code BlockingDeque} is thread safe,
* does not permit null elements, and may (or may not) be * does not permit null elements, and may (or may not) be
* capacity-constrained. * capacity-constrained.
* *
* <p>A <tt>BlockingDeque</tt> implementation may be used directly as a FIFO * <p>A {@code BlockingDeque} implementation may be used directly as a FIFO
* <tt>BlockingQueue</tt>. The methods inherited from the * {@code BlockingQueue}. The methods inherited from the
* <tt>BlockingQueue</tt> interface are precisely equivalent to * {@code BlockingQueue} interface are precisely equivalent to
* <tt>BlockingDeque</tt> methods as indicated in the following table: * {@code BlockingDeque} methods as indicated in the following table:
* *
* <p> * <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1> * <table BORDER CELLPADDING=3 CELLSPACING=1>
* <caption>Comparison of BlockingQueue and BlockingDeque methods</caption>
* <tr> * <tr>
* <td ALIGN=CENTER> <b><tt>BlockingQueue</tt> Method</b></td> * <td ALIGN=CENTER> <b>{@code BlockingQueue} Method</b></td>
* <td ALIGN=CENTER> <b>Equivalent <tt>BlockingDeque</tt> Method</b></td> * <td ALIGN=CENTER> <b>Equivalent {@code BlockingDeque} Method</b></td>
* </tr> * </tr>
* <tr> * <tr>
* <td ALIGN=CENTER COLSPAN = 2> <b>Insert</b></td> * <td ALIGN=CENTER COLSPAN = 2> <b>Insert</b></td>
...@@ -208,7 +210,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -208,7 +210,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Inserts the specified element at the front of this deque if it is * Inserts the specified element at the front of this deque if it is
* possible to do so immediately without violating capacity restrictions, * possible to do so immediately without violating capacity restrictions,
* throwing an <tt>IllegalStateException</tt> if no space is currently * throwing an {@code IllegalStateException} if no space is currently
* available. When using a capacity-restricted deque, it is generally * available. When using a capacity-restricted deque, it is generally
* preferable to use {@link #offerFirst(Object) offerFirst}. * preferable to use {@link #offerFirst(Object) offerFirst}.
* *
...@@ -223,7 +225,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -223,7 +225,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Inserts the specified element at the end of this deque if it is * Inserts the specified element at the end of this deque if it is
* possible to do so immediately without violating capacity restrictions, * possible to do so immediately without violating capacity restrictions,
* throwing an <tt>IllegalStateException</tt> if no space is currently * throwing an {@code IllegalStateException} if no space is currently
* available. When using a capacity-restricted deque, it is generally * available. When using a capacity-restricted deque, it is generally
* preferable to use {@link #offerLast(Object) offerLast}. * preferable to use {@link #offerLast(Object) offerLast}.
* *
...@@ -238,7 +240,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -238,7 +240,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Inserts the specified element at the front of this deque if it is * Inserts the specified element at the front of this deque if it is
* possible to do so immediately without violating capacity restrictions, * possible to do so immediately without violating capacity restrictions,
* returning <tt>true</tt> upon success and <tt>false</tt> if no space is * returning {@code true} upon success and {@code false} if no space is
* currently available. * currently available.
* When using a capacity-restricted deque, this method is generally * When using a capacity-restricted deque, this method is generally
* preferable to the {@link #addFirst(Object) addFirst} method, which can * preferable to the {@link #addFirst(Object) addFirst} method, which can
...@@ -254,7 +256,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -254,7 +256,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Inserts the specified element at the end of this deque if it is * Inserts the specified element at the end of this deque if it is
* possible to do so immediately without violating capacity restrictions, * possible to do so immediately without violating capacity restrictions,
* returning <tt>true</tt> upon success and <tt>false</tt> if no space is * returning {@code true} upon success and {@code false} if no space is
* currently available. * currently available.
* When using a capacity-restricted deque, this method is generally * When using a capacity-restricted deque, this method is generally
* preferable to the {@link #addLast(Object) addLast} method, which can * preferable to the {@link #addLast(Object) addLast} method, which can
...@@ -302,10 +304,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -302,10 +304,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* *
* @param e the element to add * @param e the element to add
* @param timeout how long to wait before giving up, in units of * @param timeout how long to wait before giving up, in units of
* <tt>unit</tt> * {@code unit}
* @param unit a <tt>TimeUnit</tt> determining how to interpret the * @param unit a {@code TimeUnit} determining how to interpret the
* <tt>timeout</tt> parameter * {@code timeout} parameter
* @return <tt>true</tt> if successful, or <tt>false</tt> if * @return {@code true} if successful, or {@code false} if
* the specified waiting time elapses before space is available * the specified waiting time elapses before space is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
...@@ -324,10 +326,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -324,10 +326,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* *
* @param e the element to add * @param e the element to add
* @param timeout how long to wait before giving up, in units of * @param timeout how long to wait before giving up, in units of
* <tt>unit</tt> * {@code unit}
* @param unit a <tt>TimeUnit</tt> determining how to interpret the * @param unit a {@code TimeUnit} determining how to interpret the
* <tt>timeout</tt> parameter * {@code timeout} parameter
* @return <tt>true</tt> if successful, or <tt>false</tt> if * @return {@code true} if successful, or {@code false} if
* the specified waiting time elapses before space is available * the specified waiting time elapses before space is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
...@@ -363,10 +365,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -363,10 +365,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* become available. * become available.
* *
* @param timeout how long to wait before giving up, in units of * @param timeout how long to wait before giving up, in units of
* <tt>unit</tt> * {@code unit}
* @param unit a <tt>TimeUnit</tt> determining how to interpret the * @param unit a {@code TimeUnit} determining how to interpret the
* <tt>timeout</tt> parameter * {@code timeout} parameter
* @return the head of this deque, or <tt>null</tt> if the specified * @return the head of this deque, or {@code null} if the specified
* waiting time elapses before an element is available * waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
*/ */
...@@ -379,10 +381,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -379,10 +381,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* become available. * become available.
* *
* @param timeout how long to wait before giving up, in units of * @param timeout how long to wait before giving up, in units of
* <tt>unit</tt> * {@code unit}
* @param unit a <tt>TimeUnit</tt> determining how to interpret the * @param unit a {@code TimeUnit} determining how to interpret the
* <tt>timeout</tt> parameter * {@code timeout} parameter
* @return the tail of this deque, or <tt>null</tt> if the specified * @return the tail of this deque, or {@code null} if the specified
* waiting time elapses before an element is available * waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
*/ */
...@@ -392,13 +394,13 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -392,13 +394,13 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Removes the first occurrence of the specified element from this deque. * Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged. * If the deque does not contain the element, it is unchanged.
* More formally, removes the first element <tt>e</tt> such that * More formally, removes the first element {@code e} such that
* <tt>o.equals(e)</tt> (if such an element exists). * {@code o.equals(e)} (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element * Returns {@code true} if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call). * (or equivalently, if this deque changed as a result of the call).
* *
* @param o element to be removed from this deque, if present * @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call * @return {@code true} if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* is incompatible with this deque * is incompatible with this deque
* (<a href="../Collection.html#optional-restrictions">optional</a>) * (<a href="../Collection.html#optional-restrictions">optional</a>)
...@@ -410,13 +412,13 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -410,13 +412,13 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Removes the last occurrence of the specified element from this deque. * Removes the last occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged. * If the deque does not contain the element, it is unchanged.
* More formally, removes the last element <tt>e</tt> such that * More formally, removes the last element {@code e} such that
* <tt>o.equals(e)</tt> (if such an element exists). * {@code o.equals(e)} (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element * Returns {@code true} if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call). * (or equivalently, if this deque changed as a result of the call).
* *
* @param o element to be removed from this deque, if present * @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call * @return {@code true} if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* is incompatible with this deque * is incompatible with this deque
* (<a href="../Collection.html#optional-restrictions">optional</a>) * (<a href="../Collection.html#optional-restrictions">optional</a>)
...@@ -431,8 +433,8 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -431,8 +433,8 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* Inserts the specified element into the queue represented by this deque * Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque) if it is possible to do so * (in other words, at the tail of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning * immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and throwing an * {@code true} upon success and throwing an
* <tt>IllegalStateException</tt> if no space is currently available. * {@code IllegalStateException} if no space is currently available.
* When using a capacity-restricted deque, it is generally preferable to * When using a capacity-restricted deque, it is generally preferable to
* use {@link #offer(Object) offer}. * use {@link #offer(Object) offer}.
* *
...@@ -452,7 +454,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -452,7 +454,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* Inserts the specified element into the queue represented by this deque * Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque) if it is possible to do so * (in other words, at the tail of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning * immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and <tt>false</tt> if no space is currently * {@code true} upon success and {@code false} if no space is currently
* available. When using a capacity-restricted deque, this method is * available. When using a capacity-restricted deque, this method is
* generally preferable to the {@link #add} method, which can fail to * generally preferable to the {@link #add} method, which can fail to
* insert an element only by throwing an exception. * insert an element only by throwing an exception.
...@@ -494,8 +496,8 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -494,8 +496,8 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* {@link #offerLast(Object,long,TimeUnit) offerLast}. * {@link #offerLast(Object,long,TimeUnit) offerLast}.
* *
* @param e the element to add * @param e the element to add
* @return <tt>true</tt> if the element was added to this deque, else * @return {@code true} if the element was added to this deque, else
* <tt>false</tt> * {@code false}
* @throws InterruptedException {@inheritDoc} * @throws InterruptedException {@inheritDoc}
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque * prevents it from being added to this deque
...@@ -522,11 +524,11 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -522,11 +524,11 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Retrieves and removes the head of the queue represented by this deque * Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque), or returns * (in other words, the first element of this deque), or returns
* <tt>null</tt> if this deque is empty. * {@code null} if this deque is empty.
* *
* <p>This method is equivalent to {@link #pollFirst()}. * <p>This method is equivalent to {@link #pollFirst()}.
* *
* @return the head of this deque, or <tt>null</tt> if this deque is empty * @return the head of this deque, or {@code null} if this deque is empty
*/ */
E poll(); E poll();
...@@ -550,7 +552,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -550,7 +552,7 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
* <p>This method is equivalent to * <p>This method is equivalent to
* {@link #pollFirst(long,TimeUnit) pollFirst}. * {@link #pollFirst(long,TimeUnit) pollFirst}.
* *
* @return the head of this deque, or <tt>null</tt> if the * @return the head of this deque, or {@code null} if the
* specified waiting time elapses before an element is available * specified waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
*/ */
...@@ -573,27 +575,27 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -573,27 +575,27 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
/** /**
* Retrieves, but does not remove, the head of the queue represented by * Retrieves, but does not remove, the head of the queue represented by
* this deque (in other words, the first element of this deque), or * this deque (in other words, the first element of this deque), or
* returns <tt>null</tt> if this deque is empty. * returns {@code null} if this deque is empty.
* *
* <p>This method is equivalent to {@link #peekFirst() peekFirst}. * <p>This method is equivalent to {@link #peekFirst() peekFirst}.
* *
* @return the head of this deque, or <tt>null</tt> if this deque is empty * @return the head of this deque, or {@code null} if this deque is empty
*/ */
E peek(); E peek();
/** /**
* Removes the first occurrence of the specified element from this deque. * Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged. * If the deque does not contain the element, it is unchanged.
* More formally, removes the first element <tt>e</tt> such that * More formally, removes the first element {@code e} such that
* <tt>o.equals(e)</tt> (if such an element exists). * {@code o.equals(e)} (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element * Returns {@code true} if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call). * (or equivalently, if this deque changed as a result of the call).
* *
* <p>This method is equivalent to * <p>This method is equivalent to
* {@link #removeFirstOccurrence(Object) removeFirstOccurrence}. * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
* *
* @param o element to be removed from this deque, if present * @param o element to be removed from this deque, if present
* @return <tt>true</tt> if this deque changed as a result of the call * @return {@code true} if this deque changed as a result of the call
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* is incompatible with this deque * is incompatible with this deque
* (<a href="../Collection.html#optional-restrictions">optional</a>) * (<a href="../Collection.html#optional-restrictions">optional</a>)
...@@ -603,12 +605,12 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -603,12 +605,12 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
boolean remove(Object o); boolean remove(Object o);
/** /**
* Returns <tt>true</tt> if this deque contains the specified element. * Returns {@code true} if this deque contains the specified element.
* More formally, returns <tt>true</tt> if and only if this deque contains * More formally, returns {@code true} if and only if this deque contains
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. * at least one element {@code e} such that {@code o.equals(e)}.
* *
* @param o object to be checked for containment in this deque * @param o object to be checked for containment in this deque
* @return <tt>true</tt> if this deque contains the specified element * @return {@code true} if this deque contains the specified element
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* is incompatible with this deque * is incompatible with this deque
* (<a href="../Collection.html#optional-restrictions">optional</a>) * (<a href="../Collection.html#optional-restrictions">optional</a>)
...@@ -635,9 +637,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { ...@@ -635,9 +637,10 @@ public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
// *** Stack methods *** // *** Stack methods ***
/** /**
* Pushes an element onto the stack represented by this deque. In other * Pushes an element onto the stack represented by this deque (in other
* words, inserts the element at the front of this deque unless it would * words, at the head of this deque) if it is possible to do so
* violate capacity restrictions. * immediately without violating capacity restrictions, throwing an
* {@code IllegalStateException} if no space is currently available.
* *
* <p>This method is equivalent to {@link #addFirst(Object) addFirst}. * <p>This method is equivalent to {@link #addFirst(Object) addFirst}.
* *
......
...@@ -44,17 +44,18 @@ import java.util.Queue; ...@@ -44,17 +44,18 @@ import java.util.Queue;
* element, and wait for space to become available in the queue when * element, and wait for space to become available in the queue when
* storing an element. * storing an element.
* *
* <p><tt>BlockingQueue</tt> methods come in four forms, with different ways * <p>{@code BlockingQueue} methods come in four forms, with different ways
* of handling operations that cannot be satisfied immediately, but may be * of handling operations that cannot be satisfied immediately, but may be
* satisfied at some point in the future: * satisfied at some point in the future:
* one throws an exception, the second returns a special value (either * one throws an exception, the second returns a special value (either
* <tt>null</tt> or <tt>false</tt>, depending on the operation), the third * {@code null} or {@code false}, depending on the operation), the third
* blocks the current thread indefinitely until the operation can succeed, * blocks the current thread indefinitely until the operation can succeed,
* and the fourth blocks for only a given maximum time limit before giving * and the fourth blocks for only a given maximum time limit before giving
* up. These methods are summarized in the following table: * up. These methods are summarized in the following table:
* *
* <p> * <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1> * <table BORDER CELLPADDING=3 CELLSPACING=1>
* <caption>Summary of BlockingQueue methods</caption>
* <tr> * <tr>
* <td></td> * <td></td>
* <td ALIGN=CENTER><em>Throws exception</em></td> * <td ALIGN=CENTER><em>Throws exception</em></td>
...@@ -85,37 +86,37 @@ import java.util.Queue; ...@@ -85,37 +86,37 @@ import java.util.Queue;
* </tr> * </tr>
* </table> * </table>
* *
* <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements. * <p>A {@code BlockingQueue} does not accept {@code null} elements.
* Implementations throw <tt>NullPointerException</tt> on attempts * Implementations throw {@code NullPointerException} on attempts
* to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A * to {@code add}, {@code put} or {@code offer} a {@code null}. A
* <tt>null</tt> is used as a sentinel value to indicate failure of * {@code null} is used as a sentinel value to indicate failure of
* <tt>poll</tt> operations. * {@code poll} operations.
* *
* <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given * <p>A {@code BlockingQueue} may be capacity bounded. At any given
* time it may have a <tt>remainingCapacity</tt> beyond which no * time it may have a {@code remainingCapacity} beyond which no
* additional elements can be <tt>put</tt> without blocking. * additional elements can be {@code put} without blocking.
* A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always * A {@code BlockingQueue} without any intrinsic capacity constraints always
* reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>. * reports a remaining capacity of {@code Integer.MAX_VALUE}.
* *
* <p> <tt>BlockingQueue</tt> implementations are designed to be used * <p>{@code BlockingQueue} implementations are designed to be used
* primarily for producer-consumer queues, but additionally support * primarily for producer-consumer queues, but additionally support
* the {@link java.util.Collection} interface. So, for example, it is * the {@link java.util.Collection} interface. So, for example, it is
* possible to remove an arbitrary element from a queue using * possible to remove an arbitrary element from a queue using
* <tt>remove(x)</tt>. However, such operations are in general * {@code remove(x)}. However, such operations are in general
* <em>not</em> performed very efficiently, and are intended for only * <em>not</em> performed very efficiently, and are intended for only
* occasional use, such as when a queued message is cancelled. * occasional use, such as when a queued message is cancelled.
* *
* <p> <tt>BlockingQueue</tt> implementations are thread-safe. All * <p>{@code BlockingQueue} implementations are thread-safe. All
* queuing methods achieve their effects atomically using internal * queuing methods achieve their effects atomically using internal
* locks or other forms of concurrency control. However, the * locks or other forms of concurrency control. However, the
* <em>bulk</em> Collection operations <tt>addAll</tt>, * <em>bulk</em> Collection operations {@code addAll},
* <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are * {@code containsAll}, {@code retainAll} and {@code removeAll} are
* <em>not</em> necessarily performed atomically unless specified * <em>not</em> necessarily performed atomically unless specified
* otherwise in an implementation. So it is possible, for example, for * otherwise in an implementation. So it is possible, for example, for
* <tt>addAll(c)</tt> to fail (throwing an exception) after adding * {@code addAll(c)} to fail (throwing an exception) after adding
* only some of the elements in <tt>c</tt>. * only some of the elements in {@code c}.
* *
* <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support * <p>A {@code BlockingQueue} does <em>not</em> intrinsically support
* any kind of &quot;close&quot; or &quot;shutdown&quot; operation to * any kind of &quot;close&quot; or &quot;shutdown&quot; operation to
* indicate that no more items will be added. The needs and usage of * indicate that no more items will be added. The needs and usage of
* such features tend to be implementation-dependent. For example, a * such features tend to be implementation-dependent. For example, a
...@@ -125,7 +126,7 @@ import java.util.Queue; ...@@ -125,7 +126,7 @@ import java.util.Queue;
* *
* <p> * <p>
* Usage example, based on a typical producer-consumer scenario. * Usage example, based on a typical producer-consumer scenario.
* Note that a <tt>BlockingQueue</tt> can safely be used with multiple * Note that a {@code BlockingQueue} can safely be used with multiple
* producers and multiple consumers. * producers and multiple consumers.
* <pre> {@code * <pre> {@code
* class Producer implements Runnable { * class Producer implements Runnable {
...@@ -181,13 +182,13 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -181,13 +182,13 @@ public interface BlockingQueue<E> extends Queue<E> {
/** /**
* Inserts the specified element into this queue if it is possible to do * Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions, returning * so immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and throwing an * {@code true} upon success and throwing an
* <tt>IllegalStateException</tt> if no space is currently available. * {@code IllegalStateException} if no space is currently available.
* When using a capacity-restricted queue, it is generally preferable to * When using a capacity-restricted queue, it is generally preferable to
* use {@link #offer(Object) offer}. * use {@link #offer(Object) offer}.
* *
* @param e the element to add * @param e the element to add
* @return <tt>true</tt> (as specified by {@link Collection#add}) * @return {@code true} (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this * @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions * time due to capacity restrictions
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
...@@ -201,14 +202,14 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -201,14 +202,14 @@ public interface BlockingQueue<E> extends Queue<E> {
/** /**
* Inserts the specified element into this queue if it is possible to do * Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions, returning * so immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and <tt>false</tt> if no space is currently * {@code true} upon success and {@code false} if no space is currently
* available. When using a capacity-restricted queue, this method is * available. When using a capacity-restricted queue, this method is
* generally preferable to {@link #add}, which can fail to insert an * generally preferable to {@link #add}, which can fail to insert an
* element only by throwing an exception. * element only by throwing an exception.
* *
* @param e the element to add * @param e the element to add
* @return <tt>true</tt> if the element was added to this queue, else * @return {@code true} if the element was added to this queue, else
* <tt>false</tt> * {@code false}
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue * prevents it from being added to this queue
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
...@@ -237,10 +238,10 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -237,10 +238,10 @@ public interface BlockingQueue<E> extends Queue<E> {
* *
* @param e the element to add * @param e the element to add
* @param timeout how long to wait before giving up, in units of * @param timeout how long to wait before giving up, in units of
* <tt>unit</tt> * {@code unit}
* @param unit a <tt>TimeUnit</tt> determining how to interpret the * @param unit a {@code TimeUnit} determining how to interpret the
* <tt>timeout</tt> parameter * {@code timeout} parameter
* @return <tt>true</tt> if successful, or <tt>false</tt> if * @return {@code true} if successful, or {@code false} if
* the specified waiting time elapses before space is available * the specified waiting time elapses before space is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
...@@ -266,10 +267,10 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -266,10 +267,10 @@ public interface BlockingQueue<E> extends Queue<E> {
* specified wait time if necessary for an element to become available. * specified wait time if necessary for an element to become available.
* *
* @param timeout how long to wait before giving up, in units of * @param timeout how long to wait before giving up, in units of
* <tt>unit</tt> * {@code unit}
* @param unit a <tt>TimeUnit</tt> determining how to interpret the * @param unit a {@code TimeUnit} determining how to interpret the
* <tt>timeout</tt> parameter * {@code timeout} parameter
* @return the head of this queue, or <tt>null</tt> if the * @return the head of this queue, or {@code null} if the
* specified waiting time elapses before an element is available * specified waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting * @throws InterruptedException if interrupted while waiting
*/ */
...@@ -279,11 +280,11 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -279,11 +280,11 @@ public interface BlockingQueue<E> extends Queue<E> {
/** /**
* Returns the number of additional elements that this queue can ideally * Returns the number of additional elements that this queue can ideally
* (in the absence of memory or resource constraints) accept without * (in the absence of memory or resource constraints) accept without
* blocking, or <tt>Integer.MAX_VALUE</tt> if there is no intrinsic * blocking, or {@code Integer.MAX_VALUE} if there is no intrinsic
* limit. * limit.
* *
* <p>Note that you <em>cannot</em> always tell if an attempt to insert * <p>Note that you <em>cannot</em> always tell if an attempt to insert
* an element will succeed by inspecting <tt>remainingCapacity</tt> * an element will succeed by inspecting {@code remainingCapacity}
* because it may be the case that another thread is about to * because it may be the case that another thread is about to
* insert or remove an element. * insert or remove an element.
* *
...@@ -293,14 +294,14 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -293,14 +294,14 @@ public interface BlockingQueue<E> extends Queue<E> {
/** /**
* Removes a single instance of the specified element from this queue, * Removes a single instance of the specified element from this queue,
* if it is present. More formally, removes an element <tt>e</tt> such * if it is present. More formally, removes an element {@code e} such
* that <tt>o.equals(e)</tt>, if this queue contains one or more such * that {@code o.equals(e)}, if this queue contains one or more such
* elements. * elements.
* Returns <tt>true</tt> if this queue contained the specified element * Returns {@code true} if this queue contained the specified element
* (or equivalently, if this queue changed as a result of the call). * (or equivalently, if this queue changed as a result of the call).
* *
* @param o element to be removed from this queue, if present * @param o element to be removed from this queue, if present
* @return <tt>true</tt> if this queue changed as a result of the call * @return {@code true} if this queue changed as a result of the call
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* is incompatible with this queue * is incompatible with this queue
* (<a href="../Collection.html#optional-restrictions">optional</a>) * (<a href="../Collection.html#optional-restrictions">optional</a>)
...@@ -310,12 +311,12 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -310,12 +311,12 @@ public interface BlockingQueue<E> extends Queue<E> {
boolean remove(Object o); boolean remove(Object o);
/** /**
* Returns <tt>true</tt> if this queue contains the specified element. * Returns {@code true} if this queue contains the specified element.
* More formally, returns <tt>true</tt> if and only if this queue contains * More formally, returns {@code true} if and only if this queue contains
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. * at least one element {@code e} such that {@code o.equals(e)}.
* *
* @param o object to be checked for containment in this queue * @param o object to be checked for containment in this queue
* @return <tt>true</tt> if this queue contains the specified element * @return {@code true} if this queue contains the specified element
* @throws ClassCastException if the class of the specified element * @throws ClassCastException if the class of the specified element
* is incompatible with this queue * is incompatible with this queue
* (<a href="../Collection.html#optional-restrictions">optional</a>) * (<a href="../Collection.html#optional-restrictions">optional</a>)
...@@ -329,10 +330,10 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -329,10 +330,10 @@ public interface BlockingQueue<E> extends Queue<E> {
* to the given collection. This operation may be more * to the given collection. This operation may be more
* efficient than repeatedly polling this queue. A failure * efficient than repeatedly polling this queue. A failure
* encountered while attempting to add elements to * encountered while attempting to add elements to
* collection <tt>c</tt> may result in elements being in neither, * collection {@code c} may result in elements being in neither,
* either or both collections when the associated exception is * either or both collections when the associated exception is
* thrown. Attempts to drain a queue to itself result in * thrown. Attempts to drain a queue to itself result in
* <tt>IllegalArgumentException</tt>. Further, the behavior of * {@code IllegalArgumentException}. Further, the behavior of
* this operation is undefined if the specified collection is * this operation is undefined if the specified collection is
* modified while the operation is in progress. * modified while the operation is in progress.
* *
...@@ -353,10 +354,10 @@ public interface BlockingQueue<E> extends Queue<E> { ...@@ -353,10 +354,10 @@ public interface BlockingQueue<E> extends Queue<E> {
* Removes at most the given number of available elements from * Removes at most the given number of available elements from
* this queue and adds them to the given collection. A failure * this queue and adds them to the given collection. A failure
* encountered while attempting to add elements to * encountered while attempting to add elements to
* collection <tt>c</tt> may result in elements being in neither, * collection {@code c} may result in elements being in neither,
* either or both collections when the associated exception is * either or both collections when the associated exception is
* thrown. Attempts to drain a queue to itself result in * thrown. Attempts to drain a queue to itself result in
* <tt>IllegalArgumentException</tt>. Further, the behavior of * {@code IllegalArgumentException}. Further, the behavior of
* this operation is undefined if the specified collection is * this operation is undefined if the specified collection is
* modified while the operation is in progress. * modified while the operation is in progress.
* *
......
...@@ -42,6 +42,9 @@ import java.util.Deque; ...@@ -42,6 +42,9 @@ import java.util.Deque;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Queue; import java.util.Queue;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
/** /**
* An unbounded concurrent {@linkplain Deque deque} based on linked nodes. * An unbounded concurrent {@linkplain Deque deque} based on linked nodes.
...@@ -816,7 +819,7 @@ public class ConcurrentLinkedDeque<E> ...@@ -816,7 +819,7 @@ public class ConcurrentLinkedDeque<E>
* Creates an array list and fills it with elements of this list. * Creates an array list and fills it with elements of this list.
* Used by toArray. * Used by toArray.
* *
* @return the arrayList * @return the array list
*/ */
private ArrayList<E> toArrayList() { private ArrayList<E> toArrayList() {
ArrayList<E> list = new ArrayList<E>(); ArrayList<E> list = new ArrayList<E>();
...@@ -1024,11 +1027,27 @@ public class ConcurrentLinkedDeque<E> ...@@ -1024,11 +1027,27 @@ public class ConcurrentLinkedDeque<E>
} }
public E poll() { return pollFirst(); } public E poll() { return pollFirst(); }
public E remove() { return removeFirst(); }
public E peek() { return peekFirst(); } public E peek() { return peekFirst(); }
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public E remove() { return removeFirst(); }
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public E pop() { return removeFirst(); }
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public E element() { return getFirst(); } public E element() { return getFirst(); }
/**
* @throws NullPointerException {@inheritDoc}
*/
public void push(E e) { addFirst(e); } public void push(E e) { addFirst(e); }
public E pop() { return removeFirst(); }
/** /**
* Removes the first element {@code e} such that * Removes the first element {@code e} such that
...@@ -1385,6 +1404,99 @@ public class ConcurrentLinkedDeque<E> ...@@ -1385,6 +1404,99 @@ public class ConcurrentLinkedDeque<E>
Node<E> nextNode(Node<E> p) { return pred(p); } Node<E> nextNode(Node<E> p) { return pred(p); }
} }
/** A customized variant of Spliterators.IteratorSpliterator */
static final class CLDSpliterator<E> implements Spliterator<E> {
static final int MAX_BATCH = 1 << 25; // max batch array size;
final ConcurrentLinkedDeque<E> queue;
Node<E> current; // current node; null until initialized
int batch; // batch size for splits
boolean exhausted; // true when no more nodes
CLDSpliterator(ConcurrentLinkedDeque<E> queue) {
this.queue = queue;
}
public Spliterator<E> trySplit() {
Node<E> p;
final ConcurrentLinkedDeque<E> q = this.queue;
int b = batch;
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
if (p.item == null && p == (p = p.next))
current = p = q.first();
if (p != null && p.next != null) {
Object[] a = new Object[n];
int i = 0;
do {
if ((a[i] = p.item) != null)
++i;
if (p == (p = p.next))
p = q.first();
} while (p != null && i < n);
if ((current = p) == null)
exhausted = true;
if (i > 0) {
batch = i;
return Spliterators.spliterator
(a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
Spliterator.CONCURRENT);
}
}
}
return null;
}
public void forEachRemaining(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
final ConcurrentLinkedDeque<E> q = this.queue;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
exhausted = true;
do {
E e = p.item;
if (p == (p = p.next))
p = q.first();
if (e != null)
action.accept(e);
} while (p != null);
}
}
public boolean tryAdvance(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
final ConcurrentLinkedDeque<E> q = this.queue;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
E e;
do {
e = p.item;
if (p == (p = p.next))
p = q.first();
} while (e == null && p != null);
if ((current = p) == null)
exhausted = true;
if (e != null) {
action.accept(e);
return true;
}
}
return false;
}
public long estimateSize() { return Long.MAX_VALUE; }
public int characteristics() {
return Spliterator.ORDERED | Spliterator.NONNULL |
Spliterator.CONCURRENT;
}
}
public Spliterator<E> spliterator() {
return new CLDSpliterator<E>(this);
}
/** /**
* Saves this deque to a stream (that is, serializes it). * Saves this deque to a stream (that is, serializes it).
* *
......
...@@ -41,6 +41,9 @@ import java.util.Collection; ...@@ -41,6 +41,9 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Queue; import java.util.Queue;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
/** /**
* An unbounded thread-safe {@linkplain Queue queue} based on linked nodes. * An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
...@@ -56,7 +59,7 @@ import java.util.Queue; ...@@ -56,7 +59,7 @@ import java.util.Queue;
* Like most other concurrent collection implementations, this class * Like most other concurrent collection implementations, this class
* does not permit the use of {@code null} elements. * does not permit the use of {@code null} elements.
* *
* <p>This implementation employs an efficient &quot;wait-free&quot; * <p>This implementation employs an efficient <em>non-blocking</em>
* algorithm based on one described in <a * algorithm based on one described in <a
* href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple, * href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple,
* Fast, and Practical Non-Blocking and Blocking Concurrent Queue * Fast, and Practical Non-Blocking and Blocking Concurrent Queue
...@@ -295,7 +298,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> ...@@ -295,7 +298,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
} }
/** /**
* Try to CAS head to p. If successful, repoint old head to itself * Tries to CAS head to p. If successful, repoint old head to itself
* as sentinel for succ(), below. * as sentinel for succ(), below.
*/ */
final void updateHead(Node<E> h, Node<E> p) { final void updateHead(Node<E> h, Node<E> p) {
...@@ -792,6 +795,96 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> ...@@ -792,6 +795,96 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
tail = t; tail = t;
} }
/** A customized variant of Spliterators.IteratorSpliterator */
static final class CLQSpliterator<E> implements Spliterator<E> {
static final int MAX_BATCH = 1 << 25; // max batch array size;
final ConcurrentLinkedQueue<E> queue;
Node<E> current; // current node; null until initialized
int batch; // batch size for splits
boolean exhausted; // true when no more nodes
CLQSpliterator(ConcurrentLinkedQueue<E> queue) {
this.queue = queue;
}
public Spliterator<E> trySplit() {
Node<E> p;
final ConcurrentLinkedQueue<E> q = this.queue;
int b = batch;
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null) &&
p.next != null) {
Object[] a = new Object[n];
int i = 0;
do {
if ((a[i] = p.item) != null)
++i;
if (p == (p = p.next))
p = q.first();
} while (p != null && i < n);
if ((current = p) == null)
exhausted = true;
if (i > 0) {
batch = i;
return Spliterators.spliterator
(a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
Spliterator.CONCURRENT);
}
}
return null;
}
public void forEachRemaining(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
final ConcurrentLinkedQueue<E> q = this.queue;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
exhausted = true;
do {
E e = p.item;
if (p == (p = p.next))
p = q.first();
if (e != null)
action.accept(e);
} while (p != null);
}
}
public boolean tryAdvance(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
final ConcurrentLinkedQueue<E> q = this.queue;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
E e;
do {
e = p.item;
if (p == (p = p.next))
p = q.first();
} while (e == null && p != null);
if ((current = p) == null)
exhausted = true;
if (e != null) {
action.accept(e);
return true;
}
}
return false;
}
public long estimateSize() { return Long.MAX_VALUE; }
public int characteristics() {
return Spliterator.ORDERED | Spliterator.NONNULL |
Spliterator.CONCURRENT;
}
}
public Spliterator<E> spliterator() {
return new CLQSpliterator<E>(this);
}
/** /**
* Throws NullPointerException if argument is null. * Throws NullPointerException if argument is null.
* *
......
...@@ -34,7 +34,17 @@ ...@@ -34,7 +34,17 @@
*/ */
package java.util.concurrent; package java.util.concurrent;
import java.util.*; import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.Spliterator;
/** /**
* A scalable concurrent {@link NavigableSet} implementation based on * A scalable concurrent {@link NavigableSet} implementation based on
...@@ -44,33 +54,33 @@ import java.util.*; ...@@ -44,33 +54,33 @@ import java.util.*;
* on which constructor is used. * on which constructor is used.
* *
* <p>This implementation provides expected average <i>log(n)</i> time * <p>This implementation provides expected average <i>log(n)</i> time
* cost for the <tt>contains</tt>, <tt>add</tt>, and <tt>remove</tt> * cost for the {@code contains}, {@code add}, and {@code remove}
* operations and their variants. Insertion, removal, and access * operations and their variants. Insertion, removal, and access
* operations safely execute concurrently by multiple threads. * operations safely execute concurrently by multiple threads.
* Iterators are <i>weakly consistent</i>, returning elements * Iterators are <i>weakly consistent</i>, returning elements
* reflecting the state of the set at some point at or since the * reflecting the state of the set at some point at or since the
* creation of the iterator. They do <em>not</em> throw {@link * creation of the iterator. They do <em>not</em> throw {@link
* ConcurrentModificationException}, and may proceed concurrently with * java.util.ConcurrentModificationException}, and may proceed
* other operations. Ascending ordered views and their iterators are * concurrently with other operations. Ascending ordered views and
* faster than descending ones. * their iterators are faster than descending ones.
* *
* <p>Beware that, unlike in most collections, the <tt>size</tt> * <p>Beware that, unlike in most collections, the {@code size}
* method is <em>not</em> a constant-time operation. Because of the * method is <em>not</em> a constant-time operation. Because of the
* asynchronous nature of these sets, determining the current number * asynchronous nature of these sets, determining the current number
* of elements requires a traversal of the elements, and so may report * of elements requires a traversal of the elements, and so may report
* inaccurate results if this collection is modified during traversal. * inaccurate results if this collection is modified during traversal.
* Additionally, the bulk operations <tt>addAll</tt>, * Additionally, the bulk operations {@code addAll},
* <tt>removeAll</tt>, <tt>retainAll</tt>, <tt>containsAll</tt>, * {@code removeAll}, {@code retainAll}, {@code containsAll},
* <tt>equals</tt>, and <tt>toArray</tt> are <em>not</em> guaranteed * {@code equals}, and {@code toArray} are <em>not</em> guaranteed
* to be performed atomically. For example, an iterator operating * to be performed atomically. For example, an iterator operating
* concurrently with an <tt>addAll</tt> operation might view only some * concurrently with an {@code addAll} operation might view only some
* of the added elements. * of the added elements.
* *
* <p>This class and its iterators implement all of the * <p>This class and its iterators implement all of the
* <em>optional</em> methods of the {@link Set} and {@link Iterator} * <em>optional</em> methods of the {@link Set} and {@link Iterator}
* interfaces. Like most other concurrent collection implementations, * interfaces. Like most other concurrent collection implementations,
* this class does not permit the use of <tt>null</tt> elements, * this class does not permit the use of {@code null} elements,
* because <tt>null</tt> arguments and return values cannot be reliably * because {@code null} arguments and return values cannot be reliably
* distinguished from the absence of elements. * distinguished from the absence of elements.
* *
* <p>This class is a member of the * <p>This class is a member of the
...@@ -90,7 +100,7 @@ public class ConcurrentSkipListSet<E> ...@@ -90,7 +100,7 @@ public class ConcurrentSkipListSet<E>
/** /**
* The underlying map. Uses Boolean.TRUE as value for each * The underlying map. Uses Boolean.TRUE as value for each
* element. This field is declared final for the sake of thread * element. This field is declared final for the sake of thread
* safety, which entails some ugliness in clone() * safety, which entails some ugliness in clone().
*/ */
private final ConcurrentNavigableMap<E,Object> m; private final ConcurrentNavigableMap<E,Object> m;
...@@ -107,7 +117,7 @@ public class ConcurrentSkipListSet<E> ...@@ -107,7 +117,7 @@ public class ConcurrentSkipListSet<E>
* the specified comparator. * the specified comparator.
* *
* @param comparator the comparator that will be used to order this set. * @param comparator the comparator that will be used to order this set.
* If <tt>null</tt>, the {@linkplain Comparable natural * If {@code null}, the {@linkplain Comparable natural
* ordering} of the elements will be used. * ordering} of the elements will be used.
*/ */
public ConcurrentSkipListSet(Comparator<? super E> comparator) { public ConcurrentSkipListSet(Comparator<? super E> comparator) {
...@@ -120,7 +130,7 @@ public class ConcurrentSkipListSet<E> ...@@ -120,7 +130,7 @@ public class ConcurrentSkipListSet<E>
* {@linkplain Comparable natural ordering}. * {@linkplain Comparable natural ordering}.
* *
* @param c The elements that will comprise the new set * @param c The elements that will comprise the new set
* @throws ClassCastException if the elements in <tt>c</tt> are * @throws ClassCastException if the elements in {@code c} are
* not {@link Comparable}, or are not mutually comparable * not {@link Comparable}, or are not mutually comparable
* @throws NullPointerException if the specified collection or any * @throws NullPointerException if the specified collection or any
* of its elements are null * of its elements are null
...@@ -151,7 +161,7 @@ public class ConcurrentSkipListSet<E> ...@@ -151,7 +161,7 @@ public class ConcurrentSkipListSet<E>
} }
/** /**
* Returns a shallow copy of this <tt>ConcurrentSkipListSet</tt> * Returns a shallow copy of this {@code ConcurrentSkipListSet}
* instance. (The elements themselves are not cloned.) * instance. (The elements themselves are not cloned.)
* *
* @return a shallow copy of this set * @return a shallow copy of this set
...@@ -172,8 +182,8 @@ public class ConcurrentSkipListSet<E> ...@@ -172,8 +182,8 @@ public class ConcurrentSkipListSet<E>
/** /**
* Returns the number of elements in this set. If this set * Returns the number of elements in this set. If this set
* contains more than <tt>Integer.MAX_VALUE</tt> elements, it * contains more than {@code Integer.MAX_VALUE} elements, it
* returns <tt>Integer.MAX_VALUE</tt>. * returns {@code Integer.MAX_VALUE}.
* *
* <p>Beware that, unlike in most collections, this method is * <p>Beware that, unlike in most collections, this method is
* <em>NOT</em> a constant-time operation. Because of the * <em>NOT</em> a constant-time operation. Because of the
...@@ -191,20 +201,20 @@ public class ConcurrentSkipListSet<E> ...@@ -191,20 +201,20 @@ public class ConcurrentSkipListSet<E>
} }
/** /**
* 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() { public boolean isEmpty() {
return m.isEmpty(); return m.isEmpty();
} }
/** /**
* Returns <tt>true</tt> if this set contains the specified element. * Returns {@code true} if this set contains the specified element.
* More formally, returns <tt>true</tt> if and only if this set * More formally, returns {@code true} if and only if this set
* contains an element <tt>e</tt> such that <tt>o.equals(e)</tt>. * contains an element {@code e} such that {@code o.equals(e)}.
* *
* @param o object to be checked for containment in this set * @param o object to be checked for containment in this set
* @return <tt>true</tt> if this set contains the specified element * @return {@code true} if this set contains the specified element
* @throws ClassCastException if the specified element cannot be * @throws ClassCastException if the specified element cannot be
* compared with the elements currently in this set * compared with the elements currently in this set
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
...@@ -215,15 +225,15 @@ public class ConcurrentSkipListSet<E> ...@@ -215,15 +225,15 @@ public class ConcurrentSkipListSet<E>
/** /**
* Adds the specified element to this set if it is not already present. * 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 * More formally, adds the specified element {@code e} to this set if
* the set contains no element <tt>e2</tt> such that <tt>e.equals(e2)</tt>. * the set contains no element {@code e2} such that {@code e.equals(e2)}.
* If this set already contains the element, the call leaves the set * 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 * @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the * @return {@code true} if this set did not already contain the
* specified element * specified element
* @throws ClassCastException if <tt>e</tt> cannot be compared * @throws ClassCastException if {@code e} cannot be compared
* with the elements currently in this set * with the elements currently in this set
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
...@@ -233,15 +243,15 @@ public class ConcurrentSkipListSet<E> ...@@ -233,15 +243,15 @@ public class ConcurrentSkipListSet<E>
/** /**
* Removes the specified element from this set if it is present. * 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.equals(e)</tt>, if this set contains such an element. * {@code o.equals(e)}, if this set contains such an element.
* Returns <tt>true</tt> if this set contained the element (or * Returns {@code true} if this set contained the element (or
* equivalently, if this set changed as a result of the call). * equivalently, if this set changed as a result of the call).
* (This set will not contain the element once the call returns.) * (This set will not contain the element once the call returns.)
* *
* @param o object to be removed from this set, if present * @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
* @throws ClassCastException if <tt>o</tt> cannot be compared * @throws ClassCastException if {@code o} cannot be compared
* with the elements currently in this set * with the elements currently in this set
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
...@@ -279,7 +289,7 @@ public class ConcurrentSkipListSet<E> ...@@ -279,7 +289,7 @@ public class ConcurrentSkipListSet<E>
/** /**
* Compares the specified object with this set for equality. Returns * Compares the specified object with this set for equality. Returns
* <tt>true</tt> if the specified object is also a set, the two sets * {@code true} if the specified object is also a set, the two sets
* have the same size, and every member of the specified set is * have the same size, and every member of the specified set is
* contained in this set (or equivalently, every member of this set is * contained in this set (or equivalently, every member of this set is
* contained in the specified set). This definition ensures that the * contained in the specified set). This definition ensures that the
...@@ -287,7 +297,7 @@ public class ConcurrentSkipListSet<E> ...@@ -287,7 +297,7 @@ public class ConcurrentSkipListSet<E>
* set interface. * set interface.
* *
* @param o the object to be compared for equality with this set * @param o the object to be compared for equality with this set
* @return <tt>true</tt> if the specified object is equal to this set * @return {@code true} if the specified object is equal to this set
*/ */
public boolean equals(Object o) { public boolean equals(Object o) {
// Override AbstractSet version to avoid calling size() // Override AbstractSet version to avoid calling size()
...@@ -312,7 +322,7 @@ public class ConcurrentSkipListSet<E> ...@@ -312,7 +322,7 @@ public class ConcurrentSkipListSet<E>
* value is the <i>asymmetric set difference</i> of the two sets. * value is the <i>asymmetric set difference</i> of the two sets.
* *
* @param c collection containing elements to be removed from this set * @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 types of one or more elements in this * @throws ClassCastException if the types of one or more elements in this
* set are incompatible with the specified collection * set are incompatible with the specified collection
* @throws NullPointerException if the specified collection or any * @throws NullPointerException if the specified collection or any
...@@ -380,14 +390,14 @@ public class ConcurrentSkipListSet<E> ...@@ -380,14 +390,14 @@ public class ConcurrentSkipListSet<E>
} }
/** /**
* @throws NoSuchElementException {@inheritDoc} * @throws java.util.NoSuchElementException {@inheritDoc}
*/ */
public E first() { public E first() {
return m.firstKey(); return m.firstKey();
} }
/** /**
* @throws NoSuchElementException {@inheritDoc} * @throws java.util.NoSuchElementException {@inheritDoc}
*/ */
public E last() { public E last() {
return m.lastKey(); return m.lastKey();
...@@ -460,7 +470,7 @@ public class ConcurrentSkipListSet<E> ...@@ -460,7 +470,7 @@ public class ConcurrentSkipListSet<E>
* reflected in the descending set, and vice-versa. * reflected in the descending set, and vice-versa.
* *
* <p>The returned set has an ordering equivalent to * <p>The returned set has an ordering equivalent to
* <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>. * {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}{@code (comparator())}.
* The expression {@code s.descendingSet().descendingSet()} returns a * The expression {@code s.descendingSet().descendingSet()} returns a
* view of {@code s} essentially equivalent to {@code s}. * view of {@code s} essentially equivalent to {@code s}.
* *
...@@ -470,6 +480,14 @@ public class ConcurrentSkipListSet<E> ...@@ -470,6 +480,14 @@ public class ConcurrentSkipListSet<E>
return new ConcurrentSkipListSet<E>(m.descendingMap()); return new ConcurrentSkipListSet<E>(m.descendingMap());
} }
@SuppressWarnings("unchecked")
public Spliterator<E> spliterator() {
if (m instanceof ConcurrentSkipListMap)
return ((ConcurrentSkipListMap<E,?>)m).keySpliterator();
else
return (Spliterator<E>)((ConcurrentSkipListMap.SubMap<E,?>)m).keyIterator();
}
// Support for resetting map in clone // Support for resetting map in clone
private void setMap(ConcurrentNavigableMap<E,Object> map) { private void setMap(ConcurrentNavigableMap<E,Object> map) {
UNSAFE.putObjectVolatile(this, mapOffset, map); UNSAFE.putObjectVolatile(this, mapOffset, map);
......
...@@ -34,7 +34,14 @@ ...@@ -34,7 +34,14 @@
*/ */
package java.util.concurrent; 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} * A {@link java.util.Set} that uses an internal {@link CopyOnWriteArrayList}
...@@ -45,17 +52,17 @@ import java.util.*; ...@@ -45,17 +52,17 @@ import java.util.*;
* vastly outnumber mutative operations, and you need * vastly outnumber mutative operations, and you need
* to prevent interference among threads during traversal. * to prevent interference among threads during traversal.
* <li>It is thread-safe. * <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 * are expensive since they usually entail copying the entire underlying
* array. * 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 * <li>Traversal via iterators is fast and cannot encounter
* interference from other threads. Iterators rely on * interference from other threads. Iterators rely on
* unchanging snapshots of the array at the time the iterators were * unchanging snapshots of the array at the time the iterators were
* constructed. * constructed.
* </ul> * </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 * copy-on-write set to maintain a set of Handler objects that
* perform some action upon state updates. * perform some action upon state updates.
* *
...@@ -73,7 +80,7 @@ import java.util.*; ...@@ -73,7 +80,7 @@ import java.util.*;
* public void update() { * public void update() {
* changeState(); * changeState();
* for (Handler handler : handlers) * for (Handler handler : handlers)
* handler.handle(); * handler.handle();
* } * }
* }}</pre> * }}</pre>
* *
...@@ -107,8 +114,15 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -107,8 +114,15 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* @throws NullPointerException if the specified collection is null * @throws NullPointerException if the specified collection is null
*/ */
public CopyOnWriteArraySet(Collection<? extends E> c) { public CopyOnWriteArraySet(Collection<? extends E> c) {
al = new CopyOnWriteArrayList<E>(); if (c.getClass() == CopyOnWriteArraySet.class) {
al.addAllAbsent(c); @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 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -121,22 +135,22 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
} }
/** /**
* 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() { public boolean isEmpty() {
return al.isEmpty(); return al.isEmpty();
} }
/** /**
* Returns <tt>true</tt> if this set contains the specified element. * Returns {@code true} if this set contains the specified element.
* More formally, returns <tt>true</tt> if and only if this set * More formally, returns {@code true} if and only if this set
* contains an element <tt>e</tt> such that * contains an element {@code e} such that
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>. * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
* *
* @param o element whose presence in this set is to be tested * @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) { public boolean contains(Object o) {
return al.contains(o); return al.contains(o);
...@@ -172,7 +186,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -172,7 +186,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* <p>If this set fits in the specified array with room to spare * <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 * (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 * 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 * set <i>only</i> if the caller knows that this set does not contain
* any null elements.) * any null elements.)
* *
...@@ -185,14 +199,14 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -185,14 +199,14 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* precise control over the runtime type of the output array, and may, * precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs. * 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 * 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> * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
* *
* Note that <tt>toArray(new Object[0])</tt> is identical in function to * Note that {@code toArray(new Object[0])} is identical in function to
* <tt>toArray()</tt>. * {@code toArray()}.
* *
* @param a the array into which the elements of this set are to be * @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 * stored, if it is big enough; otherwise, a new array of the same
...@@ -217,15 +231,15 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -217,15 +231,15 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
/** /**
* Removes the specified element from this set if it is present. * 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&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;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 * this set contained the element (or equivalently, if this set
* changed as a result of the call). (This set will not contain the * changed as a result of the call). (This set will not contain the
* element once the call returns.) * element once the call returns.)
* *
* @param o object to be removed from this set, if present * @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) { public boolean remove(Object o) {
return al.remove(o); return al.remove(o);
...@@ -233,14 +247,14 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -233,14 +247,14 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
/** /**
* Adds the specified element to this set if it is not already present. * 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 * More formally, adds the specified element {@code e} to this set if
* the set contains no element <tt>e2</tt> such that * the set contains no element {@code e2} such that
* <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>. * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set * 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 * @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 * element
*/ */
public boolean add(E e) { public boolean add(E e) {
...@@ -248,12 +262,12 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -248,12 +262,12 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
} }
/** /**
* 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 * 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 * @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 * specified collection
* @throws NullPointerException if the specified collection is null * @throws NullPointerException if the specified collection is null
* @see #contains(Object) * @see #contains(Object)
...@@ -265,13 +279,13 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -265,13 +279,13 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
/** /**
* Adds all of the elements in the specified collection to this set if * 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 * 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 * 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 * this operation is undefined if the specified collection is modified
* while the operation is in progress. * while the operation is in progress.
* *
* @param c collection containing elements to be added to this set * @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 * @throws NullPointerException if the specified collection is null
* @see #add(Object) * @see #add(Object)
*/ */
...@@ -286,7 +300,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -286,7 +300,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* <i>asymmetric set difference</i> of the two sets. * <i>asymmetric set difference</i> of the two sets.
* *
* @param c collection containing elements to be removed from this set * @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 * @throws ClassCastException if the class of an element of this set
* is incompatible with the specified collection (optional) * is incompatible with the specified collection (optional)
* @throws NullPointerException if this set contains a null element and the * @throws NullPointerException if this set contains a null element and the
...@@ -307,7 +321,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -307,7 +321,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* two sets. * two sets.
* *
* @param c collection containing elements to be retained in this set * @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 * @throws ClassCastException if the class of an element of this set
* is incompatible with the specified collection (optional) * is incompatible with the specified collection (optional)
* @throws NullPointerException if this set contains a null element and the * @throws NullPointerException if this set contains a null element and the
...@@ -326,7 +340,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -326,7 +340,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* <p>The returned iterator provides a snapshot of the state of the set * <p>The returned iterator provides a snapshot of the state of the set
* when the iterator was constructed. No synchronization is needed while * when the iterator was constructed. No synchronization is needed while
* traversing the iterator. The iterator does <em>NOT</em> support the * 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 * @return an iterator over the elements in this set
*/ */
...@@ -338,7 +352,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -338,7 +352,7 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
* Compares the specified object with this set for equality. * Compares the specified object with this set for equality.
* Returns {@code true} if the specified object is the same object * 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 * 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 * specified set are the same as the elements returned by an
* iterator over this set. More formally, the two iterators are * iterator over this set. More formally, the two iterators are
* considered to return the same elements if they return the same * considered to return the same elements if they return the same
...@@ -382,6 +396,19 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E> ...@@ -382,6 +396,19 @@ public class CopyOnWriteArraySet<E> extends AbstractSet<E>
return k == len; 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. * Tests for equality, coping with nulls.
*/ */
......
...@@ -40,8 +40,8 @@ package java.util.concurrent; ...@@ -40,8 +40,8 @@ package java.util.concurrent;
* acted upon after a given delay. * acted upon after a given delay.
* *
* <p>An implementation of this interface must define a * <p>An implementation of this interface must define a
* <tt>compareTo</tt> method that provides an ordering consistent with * {@code compareTo} method that provides an ordering consistent with
* its <tt>getDelay</tt> method. * its {@code getDelay} method.
* *
* @since 1.5 * @since 1.5
* @author Doug Lea * @author Doug Lea
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册