--- jsr166/src/jsr166x/ConcurrentSkipListSet.java 2004/12/21 17:27:44 1.5 +++ jsr166/src/jsr166x/ConcurrentSkipListSet.java 2011/03/15 19:47:02 1.12 @@ -1,10 +1,10 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -package jsr166x; +package jsr166x; import java.util.*; import java.util.concurrent.*; @@ -24,7 +24,7 @@ import java.util.concurrent.*; * threads. Iterators are weakly consistent, returning elements * reflecting the state of the set at some point at or since the * creation of the iterator. They do not throw {@link - * ConcurrentModificationException}, and may procede concurrently with + * ConcurrentModificationException}, and may proceed concurrently with * other operations. * *

Beware that, unlike in most collections, the size @@ -60,11 +60,11 @@ public class ConcurrentSkipListSet * fields of underlying map, but enables this field to be declared * final, which is necessary for thread safety. */ - private final ConcurrentSkipListMap m; + private final ConcurrentSkipListMap m; /** * Constructs a new, empty set, sorted according to the elements' natural - * order. + * order. */ public ConcurrentSkipListSet() { m = new ConcurrentSkipListMap(); @@ -72,7 +72,7 @@ public class ConcurrentSkipListSet /** * Constructs a new, empty set, sorted according to the specified - * comparator. + * comparator. * * @param c the comparator that will be used to sort this set. A * null value indicates that the elements' natural @@ -119,11 +119,11 @@ public class ConcurrentSkipListSet */ public Object clone() { ConcurrentSkipListSet clone = null; - try { - clone = (ConcurrentSkipListSet) super.clone(); - } catch (CloneNotSupportedException e) { - throw new InternalError(); - } + try { + clone = (ConcurrentSkipListSet) super.clone(); + } catch (CloneNotSupportedException e) { + throw new InternalError(); + } clone.m.initialize(); clone.addAll(this); @@ -149,7 +149,7 @@ public class ConcurrentSkipListSet * @return the number of elements in this set. */ public int size() { - return m.size(); + return m.size(); } /** @@ -157,7 +157,7 @@ public class ConcurrentSkipListSet * @return true if this set contains no elements. */ public boolean isEmpty() { - return m.isEmpty(); + return m.isEmpty(); } /** @@ -167,11 +167,11 @@ public class ConcurrentSkipListSet * @return true if this set contains the specified element. * * @throws ClassCastException if the specified object cannot be compared - * with the elements currently in the set. + * with the elements currently in the set. * @throws NullPointerException if o is null. */ public boolean contains(Object o) { - return m.containsKey(o); + return m.containsKey(o); } /** @@ -182,11 +182,11 @@ public class ConcurrentSkipListSet * element. * * @throws ClassCastException if the specified object cannot be compared - * with the elements currently in the set. + * with the elements currently in the set. * @throws NullPointerException if o is null. */ public boolean add(E o) { - return m.putIfAbsent(o, Boolean.TRUE) == null; + return m.putIfAbsent(o, Boolean.TRUE) == null; } /** @@ -196,18 +196,18 @@ public class ConcurrentSkipListSet * @return true if the set contained the specified element. * * @throws ClassCastException if the specified object cannot be compared - * with the elements currently in the set. + * with the elements currently in the set. * @throws NullPointerException if o is null. */ public boolean remove(Object o) { - return m.removep(o); + return m.removep(o); } /** * Removes all of the elements from this set. */ public void clear() { - m.clear(); + m.clear(); } /** @@ -217,7 +217,7 @@ public class ConcurrentSkipListSet * @return an iterator over the elements in this set. */ public Iterator iterator() { - return m.keyIterator(); + return m.keyIterator(); } /** @@ -227,7 +227,7 @@ public class ConcurrentSkipListSet * @return an iterator over the elements in this set. */ public Iterator descendingIterator() { - return m.descendingKeyIterator(); + return m.descendingKeyIterator(); } /* ---------------- AbstractSet Overrides -------------- */ @@ -246,20 +246,20 @@ public class ConcurrentSkipListSet */ 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; + 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) { + } catch (ClassCastException unused) { return false; - } catch(NullPointerException unused) { + } 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 @@ -269,7 +269,7 @@ public class ConcurrentSkipListSet * @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 @@ -283,13 +283,13 @@ public class ConcurrentSkipListSet 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. - * + * * @param o the value to match * @return an element greater than or equal to given element, or * null if there is no such element. @@ -304,7 +304,7 @@ public class ConcurrentSkipListSet /** * 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. @@ -319,7 +319,7 @@ public class ConcurrentSkipListSet /** * 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. @@ -334,7 +334,7 @@ public class ConcurrentSkipListSet /** * 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. @@ -407,12 +407,12 @@ public class ConcurrentSkipListSet * fromElement and toElement are equal, the returned * sorted set is empty.) The returned sorted set is backed by this set, * so changes in the returned sorted set are reflected in this set, and - * vice-versa. + * vice-versa. * @param fromElement low endpoint (inclusive) of the subSet. * @param toElement high endpoint (exclusive) of the subSet. * @return a view of the portion of this set whose elements range from - * fromElement, inclusive, to toElement, - * exclusive. + * fromElement, inclusive, to toElement, + * exclusive. * @throws ClassCastException if fromElement and * toElement cannot be compared to one another using * this set's comparator (or, if the set has no comparator, @@ -420,27 +420,27 @@ public class ConcurrentSkipListSet * @throws IllegalArgumentException if fromElement is * greater than toElement. * @throws NullPointerException if fromElement or - * toElement is null. + * toElement is null. */ public NavigableSet subSet(E fromElement, E toElement) { - return new ConcurrentSkipListSubSet(m, fromElement, toElement); + return new ConcurrentSkipListSubSet(m, fromElement, toElement); } /** * Returns a view of the portion of this set whose elements are strictly * less than toElement. The returned sorted set is backed by * this set, so changes in the returned sorted set are reflected in this - * set, and vice-versa. + * set, and vice-versa. * @param toElement high endpoint (exclusive) of the headSet. * @return a view of the portion of this set whose elements are strictly - * less than toElement. + * less than toElement. * @throws ClassCastException if toElement is not compatible * with this set's comparator (or, if the set has no comparator, * if toElement does not implement Comparable). * @throws NullPointerException if toElement is null. */ public NavigableSet headSet(E toElement) { - return new ConcurrentSkipListSubSet(m, null, toElement); + return new ConcurrentSkipListSubSet(m, null, toElement); } @@ -459,7 +459,7 @@ public class ConcurrentSkipListSet * @throws NullPointerException if fromElement is null. */ public NavigableSet tailSet(E fromElement) { - return new ConcurrentSkipListSubSet(m, fromElement, null); + return new ConcurrentSkipListSubSet(m, fromElement, null); } /** @@ -473,23 +473,23 @@ public class ConcurrentSkipListSet * tailSet methods of their underlying sets. * */ - static class ConcurrentSkipListSubSet - extends AbstractSet + static class ConcurrentSkipListSubSet + extends AbstractSet implements NavigableSet, java.io.Serializable { private static final long serialVersionUID = -7647078645896651609L; /** The underlying submap */ private final ConcurrentSkipListMap.ConcurrentSkipListSubMap s; - + /** - * Creates a new submap. + * Creates a new submap. * @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 + * non-null and fromElement greater than toElement */ - ConcurrentSkipListSubSet(ConcurrentSkipListMap map, + ConcurrentSkipListSubSet(ConcurrentSkipListMap map, E fromElement, E toElement) { s = new ConcurrentSkipListMap.ConcurrentSkipListSubMap (map, fromElement, toElement); @@ -500,7 +500,7 @@ public class ConcurrentSkipListSet public NavigableSet subSet(E fromElement, E toElement) { if (!s.inOpenRange(fromElement) || !s.inOpenRange(toElement)) throw new IllegalArgumentException("element out of range"); - return new ConcurrentSkipListSubSet(s.getMap(), + return new ConcurrentSkipListSubSet(s.getMap(), fromElement, toElement); } @@ -508,15 +508,15 @@ public class ConcurrentSkipListSet E least = s.getLeast(); if (!s.inOpenRange(toElement)) throw new IllegalArgumentException("element out of range"); - return new ConcurrentSkipListSubSet(s.getMap(), + return new ConcurrentSkipListSubSet(s.getMap(), least, toElement); } - + public NavigableSet tailSet(E fromElement) { E fence = s.getFence(); if (!s.inOpenRange(fromElement)) throw new IllegalArgumentException("element out of range"); - return new ConcurrentSkipListSubSet(s.getMap(), + return new ConcurrentSkipListSubSet(s.getMap(), fromElement, fence); } @@ -539,14 +539,14 @@ public class ConcurrentSkipListSet public Iterator descendingIterator() { return s.descendingKeySet().iterator(); } - public E pollFirst() { + public E pollFirst() { Map.Entry e = s.pollFirstEntry(); - return (e == null)? null : e.getKey(); + return (e == null) ? null : e.getKey(); } public E pollLast() { Map.Entry e = s.pollLastEntry(); - return (e == null)? null : e.getKey(); + return (e == null) ? null : e.getKey(); } } -} +}