jaroslav@1890: /*
jaroslav@1890: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890: *
jaroslav@1890: * This code is free software; you can redistribute it and/or modify it
jaroslav@1890: * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890: * published by the Free Software Foundation. Oracle designates this
jaroslav@1890: * particular file as subject to the "Classpath" exception as provided
jaroslav@1890: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890: *
jaroslav@1890: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@1890: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890: * accompanied this code).
jaroslav@1890: *
jaroslav@1890: * You should have received a copy of the GNU General Public License version
jaroslav@1890: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890: *
jaroslav@1890: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890: * or visit www.oracle.com if you need additional information or have any
jaroslav@1890: * questions.
jaroslav@1890: */
jaroslav@1890:
jaroslav@1890: /*
jaroslav@1890: * This file is available under and governed by the GNU General Public
jaroslav@1890: * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890: * However, the following notice accompanied the original version of this
jaroslav@1890: * file:
jaroslav@1890: *
jaroslav@1890: * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890: * Expert Group and released to the public domain, as explained at
jaroslav@1890: * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890: */
jaroslav@1890:
jaroslav@1890: package java.util.concurrent;
jaroslav@1890: import java.util.*;
jaroslav@1890: import sun.misc.Unsafe;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * A scalable concurrent {@link NavigableSet} implementation based on
jaroslav@1890: * a {@link ConcurrentSkipListMap}. The elements of the set are kept
jaroslav@1890: * sorted according to their {@linkplain Comparable natural ordering},
jaroslav@1890: * or by a {@link Comparator} provided at set creation time, depending
jaroslav@1890: * on which constructor is used.
jaroslav@1890: *
jaroslav@1890: *
This implementation provides expected average log(n) time
jaroslav@1890: * cost for the contains, add, and remove
jaroslav@1890: * operations and their variants. Insertion, removal, and access
jaroslav@1890: * operations safely execute concurrently by multiple threads.
jaroslav@1890: * Iterators are weakly consistent, returning elements
jaroslav@1890: * reflecting the state of the set at some point at or since the
jaroslav@1890: * creation of the iterator. They do not throw {@link
jaroslav@1890: * ConcurrentModificationException}, and may proceed concurrently with
jaroslav@1890: * other operations. Ascending ordered views and their iterators are
jaroslav@1890: * faster than descending ones.
jaroslav@1890: *
jaroslav@1890: *
Beware that, unlike in most collections, the size
jaroslav@1890: * method is not a constant-time operation. Because of the
jaroslav@1890: * asynchronous nature of these sets, determining the current number
jaroslav@1890: * of elements requires a traversal of the elements, and so may report
jaroslav@1890: * inaccurate results if this collection is modified during traversal.
jaroslav@1890: * Additionally, the bulk operations addAll,
jaroslav@1890: * removeAll, retainAll, containsAll,
jaroslav@1890: * equals, and toArray are not guaranteed
jaroslav@1890: * to be performed atomically. For example, an iterator operating
jaroslav@1890: * concurrently with an addAll operation might view only some
jaroslav@1890: * of the added elements.
jaroslav@1890: *
jaroslav@1890: *
This class and its iterators implement all of the
jaroslav@1890: * optional methods of the {@link Set} and {@link Iterator}
jaroslav@1890: * interfaces. Like most other concurrent collection implementations,
jaroslav@1890: * this class does not permit the use of null elements,
jaroslav@1890: * because null arguments and return values cannot be reliably
jaroslav@1890: * distinguished from the absence of elements.
jaroslav@1890: *
jaroslav@1890: *
This class is a member of the
jaroslav@1890: *
jaroslav@1890: * Java Collections Framework.
jaroslav@1890: *
jaroslav@1890: * @author Doug Lea
jaroslav@1890: * @param the type of elements maintained by this set
jaroslav@1890: * @since 1.6
jaroslav@1890: */
jaroslav@1890: public class ConcurrentSkipListSet
jaroslav@1890: extends AbstractSet
jaroslav@1890: implements NavigableSet, Cloneable, java.io.Serializable {
jaroslav@1890:
jaroslav@1890: private static final long serialVersionUID = -2479143111061671589L;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * The underlying map. Uses Boolean.TRUE as value for each
jaroslav@1890: * element. This field is declared final for the sake of thread
jaroslav@1890: * safety, which entails some ugliness in clone()
jaroslav@1890: */
jaroslav@1890: private final ConcurrentNavigableMap m;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Constructs a new, empty set that orders its elements according to
jaroslav@1890: * their {@linkplain Comparable natural ordering}.
jaroslav@1890: */
jaroslav@1890: public ConcurrentSkipListSet() {
jaroslav@1890: m = new ConcurrentSkipListMap();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Constructs a new, empty set that orders its elements according to
jaroslav@1890: * the specified comparator.
jaroslav@1890: *
jaroslav@1890: * @param comparator the comparator that will be used to order this set.
jaroslav@1890: * If null, the {@linkplain Comparable natural
jaroslav@1890: * ordering} of the elements will be used.
jaroslav@1890: */
jaroslav@1890: public ConcurrentSkipListSet(Comparator super E> comparator) {
jaroslav@1890: m = new ConcurrentSkipListMap(comparator);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Constructs a new set containing the elements in the specified
jaroslav@1890: * collection, that orders its elements according to their
jaroslav@1890: * {@linkplain Comparable natural ordering}.
jaroslav@1890: *
jaroslav@1890: * @param c The elements that will comprise the new set
jaroslav@1890: * @throws ClassCastException if the elements in c are
jaroslav@1890: * not {@link Comparable}, or are not mutually comparable
jaroslav@1890: * @throws NullPointerException if the specified collection or any
jaroslav@1890: * of its elements are null
jaroslav@1890: */
jaroslav@1890: public ConcurrentSkipListSet(Collection extends E> c) {
jaroslav@1890: m = new ConcurrentSkipListMap();
jaroslav@1890: addAll(c);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Constructs a new set containing the same elements and using the
jaroslav@1890: * same ordering as the specified sorted set.
jaroslav@1890: *
jaroslav@1890: * @param s sorted set whose elements will comprise the new set
jaroslav@1890: * @throws NullPointerException if the specified sorted set or any
jaroslav@1890: * of its elements are null
jaroslav@1890: */
jaroslav@1890: public ConcurrentSkipListSet(SortedSet s) {
jaroslav@1890: m = new ConcurrentSkipListMap(s.comparator());
jaroslav@1890: addAll(s);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * For use by submaps
jaroslav@1890: */
jaroslav@1890: ConcurrentSkipListSet(ConcurrentNavigableMap m) {
jaroslav@1890: this.m = m;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns a shallow copy of this ConcurrentSkipListSet
jaroslav@1890: * instance. (The elements themselves are not cloned.)
jaroslav@1890: *
jaroslav@1890: * @return a shallow copy of this set
jaroslav@1890: */
jaroslav@1890: public ConcurrentSkipListSet clone() {
jaroslav@1890: ConcurrentSkipListSet clone = null;
jaroslav@1890: try {
jaroslav@1890: clone = (ConcurrentSkipListSet) super.clone();
jaroslav@1890: clone.setMap(new ConcurrentSkipListMap(m));
jaroslav@1890: } catch (CloneNotSupportedException e) {
jaroslav@1890: throw new InternalError();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: return clone;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /* ---------------- Set operations -------------- */
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the number of elements in this set. If this set
jaroslav@1890: * contains more than Integer.MAX_VALUE elements, it
jaroslav@1890: * returns Integer.MAX_VALUE.
jaroslav@1890: *
jaroslav@1890: * Beware that, unlike in most collections, this method is
jaroslav@1890: * NOT a constant-time operation. Because of the
jaroslav@1890: * asynchronous nature of these sets, determining the current
jaroslav@1890: * number of elements requires traversing them all to count them.
jaroslav@1890: * Additionally, it is possible for the size to change during
jaroslav@1890: * execution of this method, in which case the returned result
jaroslav@1890: * will be inaccurate. Thus, this method is typically not very
jaroslav@1890: * useful in concurrent applications.
jaroslav@1890: *
jaroslav@1890: * @return the number of elements in this set
jaroslav@1890: */
jaroslav@1890: public int size() {
jaroslav@1890: return m.size();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns true if this set contains no elements.
jaroslav@1890: * @return true if this set contains no elements
jaroslav@1890: */
jaroslav@1890: public boolean isEmpty() {
jaroslav@1890: return m.isEmpty();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns true if this set contains the specified element.
jaroslav@1890: * More formally, returns true if and only if this set
jaroslav@1890: * contains an element e such that o.equals(e).
jaroslav@1890: *
jaroslav@1890: * @param o object to be checked for containment in this set
jaroslav@1890: * @return true if this set contains the specified element
jaroslav@1890: * @throws ClassCastException if the specified element cannot be
jaroslav@1890: * compared with the elements currently in this set
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public boolean contains(Object o) {
jaroslav@1890: return m.containsKey(o);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Adds the specified element to this set if it is not already present.
jaroslav@1890: * More formally, adds the specified element e to this set if
jaroslav@1890: * the set contains no element e2 such that e.equals(e2).
jaroslav@1890: * If this set already contains the element, the call leaves the set
jaroslav@1890: * unchanged and returns false.
jaroslav@1890: *
jaroslav@1890: * @param e element to be added to this set
jaroslav@1890: * @return true if this set did not already contain the
jaroslav@1890: * specified element
jaroslav@1890: * @throws ClassCastException if e cannot be compared
jaroslav@1890: * with the elements currently in this set
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public boolean add(E e) {
jaroslav@1890: return m.putIfAbsent(e, Boolean.TRUE) == null;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Removes the specified element from this set if it is present.
jaroslav@1890: * More formally, removes an element e such that
jaroslav@1890: * o.equals(e), if this set contains such an element.
jaroslav@1890: * Returns true if this set contained the element (or
jaroslav@1890: * equivalently, if this set changed as a result of the call).
jaroslav@1890: * (This set will not contain the element once the call returns.)
jaroslav@1890: *
jaroslav@1890: * @param o object to be removed from this set, if present
jaroslav@1890: * @return true if this set contained the specified element
jaroslav@1890: * @throws ClassCastException if o cannot be compared
jaroslav@1890: * with the elements currently in this set
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public boolean remove(Object o) {
jaroslav@1890: return m.remove(o, Boolean.TRUE);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Removes all of the elements from this set.
jaroslav@1890: */
jaroslav@1890: public void clear() {
jaroslav@1890: m.clear();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns an iterator over the elements in this set in ascending order.
jaroslav@1890: *
jaroslav@1890: * @return an iterator over the elements in this set in ascending order
jaroslav@1890: */
jaroslav@1890: public Iterator iterator() {
jaroslav@1890: return m.navigableKeySet().iterator();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns an iterator over the elements in this set in descending order.
jaroslav@1890: *
jaroslav@1890: * @return an iterator over the elements in this set in descending order
jaroslav@1890: */
jaroslav@1890: public Iterator descendingIterator() {
jaroslav@1890: return m.descendingKeySet().iterator();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890:
jaroslav@1890: /* ---------------- AbstractSet Overrides -------------- */
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Compares the specified object with this set for equality. Returns
jaroslav@1890: * true if the specified object is also a set, the two sets
jaroslav@1890: * have the same size, and every member of the specified set is
jaroslav@1890: * contained in this set (or equivalently, every member of this set is
jaroslav@1890: * contained in the specified set). This definition ensures that the
jaroslav@1890: * equals method works properly across different implementations of the
jaroslav@1890: * set interface.
jaroslav@1890: *
jaroslav@1890: * @param o the object to be compared for equality with this set
jaroslav@1890: * @return true if the specified object is equal to this set
jaroslav@1890: */
jaroslav@1890: public boolean equals(Object o) {
jaroslav@1890: // Override AbstractSet version to avoid calling size()
jaroslav@1890: if (o == this)
jaroslav@1890: return true;
jaroslav@1890: if (!(o instanceof Set))
jaroslav@1890: return false;
jaroslav@1890: Collection> c = (Collection>) o;
jaroslav@1890: try {
jaroslav@1890: return containsAll(c) && c.containsAll(this);
jaroslav@1890: } catch (ClassCastException unused) {
jaroslav@1890: return false;
jaroslav@1890: } catch (NullPointerException unused) {
jaroslav@1890: return false;
jaroslav@1890: }
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Removes from this set all of its elements that are contained in
jaroslav@1890: * the specified collection. If the specified collection is also
jaroslav@1890: * a set, this operation effectively modifies this set so that its
jaroslav@1890: * value is the asymmetric set difference of the two sets.
jaroslav@1890: *
jaroslav@1890: * @param c collection containing elements to be removed from this set
jaroslav@1890: * @return true if this set changed as a result of the call
jaroslav@1890: * @throws ClassCastException if the types of one or more elements in this
jaroslav@1890: * set are incompatible with the specified collection
jaroslav@1890: * @throws NullPointerException if the specified collection or any
jaroslav@1890: * of its elements are null
jaroslav@1890: */
jaroslav@1890: public boolean removeAll(Collection> c) {
jaroslav@1890: // Override AbstractSet version to avoid unnecessary call to size()
jaroslav@1890: boolean modified = false;
jaroslav@1890: for (Iterator> i = c.iterator(); i.hasNext(); )
jaroslav@1890: if (remove(i.next()))
jaroslav@1890: modified = true;
jaroslav@1890: return modified;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /* ---------------- Relational operations -------------- */
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public E lower(E e) {
jaroslav@1890: return m.lowerKey(e);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public E floor(E e) {
jaroslav@1890: return m.floorKey(e);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public E ceiling(E e) {
jaroslav@1890: return m.ceilingKey(e);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: */
jaroslav@1890: public E higher(E e) {
jaroslav@1890: return m.higherKey(e);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: public E pollFirst() {
jaroslav@1890: Map.Entry e = m.pollFirstEntry();
jaroslav@1890: return (e == null) ? null : e.getKey();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: public E pollLast() {
jaroslav@1890: Map.Entry e = m.pollLastEntry();
jaroslav@1890: return (e == null) ? null : e.getKey();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890:
jaroslav@1890: /* ---------------- SortedSet operations -------------- */
jaroslav@1890:
jaroslav@1890:
jaroslav@1890: public Comparator super E> comparator() {
jaroslav@1890: return m.comparator();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws NoSuchElementException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public E first() {
jaroslav@1890: return m.firstKey();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws NoSuchElementException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public E last() {
jaroslav@1890: return m.lastKey();
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if {@code fromElement} or
jaroslav@1890: * {@code toElement} is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public NavigableSet subSet(E fromElement,
jaroslav@1890: boolean fromInclusive,
jaroslav@1890: E toElement,
jaroslav@1890: boolean toInclusive) {
jaroslav@1890: return new ConcurrentSkipListSet
jaroslav@1890: (m.subMap(fromElement, fromInclusive,
jaroslav@1890: toElement, toInclusive));
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if {@code toElement} is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public NavigableSet headSet(E toElement, boolean inclusive) {
jaroslav@1890: return new ConcurrentSkipListSet(m.headMap(toElement, inclusive));
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if {@code fromElement} is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public NavigableSet tailSet(E fromElement, boolean inclusive) {
jaroslav@1890: return new ConcurrentSkipListSet(m.tailMap(fromElement, inclusive));
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if {@code fromElement} or
jaroslav@1890: * {@code toElement} is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public NavigableSet subSet(E fromElement, E toElement) {
jaroslav@1890: return subSet(fromElement, true, toElement, false);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if {@code toElement} is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public NavigableSet headSet(E toElement) {
jaroslav@1890: return headSet(toElement, false);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if {@code fromElement} is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: public NavigableSet tailSet(E fromElement) {
jaroslav@1890: return tailSet(fromElement, true);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns a reverse order view of the elements contained in this set.
jaroslav@1890: * The descending set is backed by this set, so changes to the set are
jaroslav@1890: * reflected in the descending set, and vice-versa.
jaroslav@1890: *
jaroslav@1890: * The returned set has an ordering equivalent to
jaroslav@1890: * {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator()).
jaroslav@1890: * The expression {@code s.descendingSet().descendingSet()} returns a
jaroslav@1890: * view of {@code s} essentially equivalent to {@code s}.
jaroslav@1890: *
jaroslav@1890: * @return a reverse order view of this set
jaroslav@1890: */
jaroslav@1890: public NavigableSet descendingSet() {
jaroslav@1890: return new ConcurrentSkipListSet(m.descendingMap());
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: // Support for resetting map in clone
jaroslav@1890: private void setMap(ConcurrentNavigableMap map) {
jaroslav@1890: UNSAFE.putObjectVolatile(this, mapOffset, map);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: private static final sun.misc.Unsafe UNSAFE;
jaroslav@1890: private static final long mapOffset;
jaroslav@1890: static {
jaroslav@1890: try {
jaroslav@1890: UNSAFE = sun.misc.Unsafe.getUnsafe();
jaroslav@1890: Class k = ConcurrentSkipListSet.class;
jaroslav@1890: mapOffset = UNSAFE.objectFieldOffset
jaroslav@1890: (k.getDeclaredField("m"));
jaroslav@1890: } catch (Exception e) {
jaroslav@1890: throw new Error(e);
jaroslav@1890: }
jaroslav@1890: }
jaroslav@1890: }