--- jsr166/src/jsr166x/ConcurrentSkipListSet.java 2004/09/06 17:01:54 1.2 +++ jsr166/src/jsr166x/ConcurrentSkipListSet.java 2004/10/16 14:49:45 1.4 @@ -28,15 +28,20 @@ import java.util.concurrent.*; * other operations. * *

Beware that, unlike in most collections, the size - * method is NOT a constant-time operation. Because of the + * method is not a constant-time operation. Because of the * asynchronous nature of these sets, determining the current number - * of elements requires a traversal of the elements. + * of elements requires a traversal of the elements. Additionally, the + * bulk operations addAll, removeAll, + * <retainAll, and tt>containsAll are not + * guaranteed to be performed atomically. For example, an iterator + * operating concurrently with an addAll operation might view + * only some of the added elements. * *

This class and its iterators implement all of the * optional methods of the {@link Set} and {@link Iterator} * interfaces. Like most other concurrent collection implementations, * this class does not permit the use of null elements. - * because null arguments and return values cannot be reliably + * because null arguments and return values cannot be reliably * distinguished from the absence of elements. * * @author Doug Lea @@ -85,7 +90,8 @@ public class ConcurrentSkipListSet * * @throws ClassCastException if the elements in the specified * collection are not comparable, or are not mutually comparable. - * @throws NullPointerException if the specified collection is null. + * @throws NullPointerException if the specified collection is + * null. */ public ConcurrentSkipListSet(Collection c) { m = new ConcurrentSkipListMap(); @@ -97,7 +103,8 @@ public class ConcurrentSkipListSet * sorted set, sorted according to the same ordering. * * @param s sorted set whose elements will comprise the new set. - * @throws NullPointerException if the specified sorted set is null. + * @throws NullPointerException if the specified sorted set is + * null. */ public ConcurrentSkipListSet(SortedSet s) { m = new ConcurrentSkipListMap(s.comparator()); @@ -212,16 +219,70 @@ public class ConcurrentSkipListSet public Iterator iterator() { return m.keyIterator(); } + + /* ---------------- AbstractSet Overrides -------------- */ + + /** + * Compares the specified object with this set for equality. Returns + * true if the specified object is also a set, the two sets + * 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 the specified set). This definition ensures that the + * equals method works properly across different implementations of the + * set interface. + * + * @param o Object to be compared for equality with this set. + * @return true if the specified Object is equal to this set. + */ + public boolean equals(Object o) { + // Override AbstractSet version to avoid calling size() + if (o == this) + return true; + if (!(o instanceof Set)) + return false; + Collection c = (Collection) o; + try { + return containsAll(c) && c.containsAll(this); + } catch(ClassCastException unused) { + return false; + } catch(NullPointerException unused) { + return false; + } + } + + /** + * Removes from this set all of its elements that are contained in + * the specified collection. If the specified collection is also + * a set, this operation effectively modifies this set so that its + * value is the asymmetric set difference of the two sets. + * + * @param c collection that defines which elements will be removed from + * this set. + * @return true if this set changed as a result of the call. + * + * @throws ClassCastException if the types of one or more elements in this + * set are incompatible with the specified collection + * @throws NullPointerException if the specified collection, or any + * of its elements are null. + */ + public boolean removeAll(Collection c) { + // Override AbstractSet version to avoid unnecessary call to size() + boolean modified = false; + for (Iterator i = c.iterator(); i.hasNext(); ) + if (remove(i.next())) + modified = true; + return modified; + } /* ---------------- Relational operations -------------- */ /** * Returns an element greater than or equal to the given element, or - * null if there is no such element. + * null if there is no such element. * * @param o the value to match - * @return an element greater than or equal to given element, or null - * if there is no such element. + * @return an element greater than or equal to given element, or + * null if there is no such element. * @throws ClassCastException if o cannot be compared with the elements * currently in the set. * @throws NullPointerException if o is null @@ -231,12 +292,12 @@ public class ConcurrentSkipListSet } /** - * Returns an element strictly less than the given element, or null if - * there is no such element. + * Returns an element strictly less than the given element, or + * null if there is no such element. * * @param o the value to match * @return the greatest element less than the given element, or - * null if there is no such element. + * null if there is no such element. * @throws ClassCastException if o cannot be compared with the elements * currently in the set. * @throws NullPointerException if o is null. @@ -246,12 +307,12 @@ public class ConcurrentSkipListSet } /** - * Returns an element less than or equal to the given element, or null - * if there is no such element. + * Returns an element less than or equal to the given element, or + * null if there is no such element. * * @param o the value to match * @return the greatest element less than or equal to given - * element, or null if there is no such element. + * element, or null if there is no such element. * @throws ClassCastException if o cannot be compared with the elements * currently in the set. * @throws NullPointerException if o is null. @@ -261,12 +322,12 @@ public class ConcurrentSkipListSet } /** - * Returns an element strictly greater than the given element, or null - * if there is no such element. + * Returns an element strictly greater than the given element, or + * null if there is no such element. * * @param o the value to match * @return the least element greater than the given element, or - * null if there is no such element. + * null if there is no such element. * @throws ClassCastException if o cannot be compared with the elements * currently in the set. * @throws NullPointerException if o is null. @@ -328,6 +389,8 @@ public class ConcurrentSkipListSet return m.lastKey(); } + + /** * Returns a view of the portion of this set whose elements range from * fromElement, inclusive, to toElement, exclusive. (If @@ -411,8 +474,8 @@ public class ConcurrentSkipListSet /** * Creates a new submap. - * @param fromElement inclusive least value, or null if from start - * @param toElement exclusive upper bound or null if to end + * @param fromElement inclusive least value, or null if from start + * @param toElement exclusive upper bound or null if to end * @throws IllegalArgumentException if fromElement and toElement * nonnull and fromElement greater than toElement */ @@ -520,10 +583,10 @@ public class ConcurrentSkipListSet /** * Returns an element greater than or equal to the given - * element, or null if there is no such element. + * element, or null if there is no such element. * * @param o the value to match - * @return an element greater than or equal to given element, or null + * @return an element greater than or equal to given element, or null * if there is no such element. * @throws ClassCastException if o cannot be compared with the * elements currently in the set. @@ -539,12 +602,12 @@ public class ConcurrentSkipListSet } /** - * Returns an element strictly less than the given element, or null if + * Returns an element strictly less than the given element, or null if * there is no such element. * * @param o the value to match * @return the greatest element less than the given element, or - * null if there is no such element. + * null if there is no such element. * @throws ClassCastException if o cannot be compared with the * elements currently in the set. * @throws NullPointerException if o is null. @@ -555,12 +618,12 @@ public class ConcurrentSkipListSet } /** - * Returns an element less than or equal to the given element, or null + * Returns an element less than or equal to the given element, or null * if there is no such element. * * @param o the value to match * @return the greatest element less than or equal to given - * element, or null if there is no such element. + * element, or null if there is no such element. * @throws ClassCastException if o cannot be compared with the * elements currently in the set. * @throws NullPointerException if o is null. @@ -571,12 +634,12 @@ public class ConcurrentSkipListSet } /** - * Returns an element strictly greater than the given element, or null + * Returns an element strictly greater than the given element, or null * if there is no such element. * * @param o the value to match * @return the least element greater than the given element, or - * null if there is no such element. + * null if there is no such element. * @throws ClassCastException if o cannot be compared with the * elements currently in the set. * @throws NullPointerException if o is null. @@ -734,6 +797,4 @@ public class ConcurrentSkipListSet fromElement, fence); } } - - }