rt/emul/compact/src/main/java/java/util/concurrent/ConcurrentSkipListSet.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
child 1895 bfaf3300b7ba
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent;
jaroslav@1890
    37
import java.util.*;
jaroslav@1890
    38
import sun.misc.Unsafe;
jaroslav@1890
    39
jaroslav@1890
    40
/**
jaroslav@1890
    41
 * A scalable concurrent {@link NavigableSet} implementation based on
jaroslav@1890
    42
 * a {@link ConcurrentSkipListMap}.  The elements of the set are kept
jaroslav@1890
    43
 * sorted according to their {@linkplain Comparable natural ordering},
jaroslav@1890
    44
 * or by a {@link Comparator} provided at set creation time, depending
jaroslav@1890
    45
 * on which constructor is used.
jaroslav@1890
    46
 *
jaroslav@1890
    47
 * <p>This implementation provides expected average <i>log(n)</i> time
jaroslav@1890
    48
 * cost for the <tt>contains</tt>, <tt>add</tt>, and <tt>remove</tt>
jaroslav@1890
    49
 * operations and their variants.  Insertion, removal, and access
jaroslav@1890
    50
 * operations safely execute concurrently by multiple threads.
jaroslav@1890
    51
 * Iterators are <i>weakly consistent</i>, returning elements
jaroslav@1890
    52
 * reflecting the state of the set at some point at or since the
jaroslav@1890
    53
 * creation of the iterator.  They do <em>not</em> throw {@link
jaroslav@1890
    54
 * ConcurrentModificationException}, and may proceed concurrently with
jaroslav@1890
    55
 * other operations.  Ascending ordered views and their iterators are
jaroslav@1890
    56
 * faster than descending ones.
jaroslav@1890
    57
 *
jaroslav@1890
    58
 * <p>Beware that, unlike in most collections, the <tt>size</tt>
jaroslav@1890
    59
 * method is <em>not</em> a constant-time operation. Because of the
jaroslav@1890
    60
 * asynchronous nature of these sets, determining the current number
jaroslav@1890
    61
 * of elements requires a traversal of the elements, and so may report
jaroslav@1890
    62
 * inaccurate results if this collection is modified during traversal.
jaroslav@1890
    63
 * Additionally, the bulk operations <tt>addAll</tt>,
jaroslav@1890
    64
 * <tt>removeAll</tt>, <tt>retainAll</tt>, <tt>containsAll</tt>,
jaroslav@1890
    65
 * <tt>equals</tt>, and <tt>toArray</tt> are <em>not</em> guaranteed
jaroslav@1890
    66
 * to be performed atomically. For example, an iterator operating
jaroslav@1890
    67
 * concurrently with an <tt>addAll</tt> operation might view only some
jaroslav@1890
    68
 * of the added elements.
jaroslav@1890
    69
 *
jaroslav@1890
    70
 * <p>This class and its iterators implement all of the
jaroslav@1890
    71
 * <em>optional</em> methods of the {@link Set} and {@link Iterator}
jaroslav@1890
    72
 * interfaces. Like most other concurrent collection implementations,
jaroslav@1890
    73
 * this class does not permit the use of <tt>null</tt> elements,
jaroslav@1890
    74
 * because <tt>null</tt> arguments and return values cannot be reliably
jaroslav@1890
    75
 * distinguished from the absence of elements.
jaroslav@1890
    76
 *
jaroslav@1890
    77
 * <p>This class is a member of the
jaroslav@1890
    78
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@1890
    79
 * Java Collections Framework</a>.
jaroslav@1890
    80
 *
jaroslav@1890
    81
 * @author Doug Lea
jaroslav@1890
    82
 * @param <E> the type of elements maintained by this set
jaroslav@1890
    83
 * @since 1.6
jaroslav@1890
    84
 */
jaroslav@1890
    85
public class ConcurrentSkipListSet<E>
jaroslav@1890
    86
    extends AbstractSet<E>
jaroslav@1890
    87
    implements NavigableSet<E>, Cloneable, java.io.Serializable {
jaroslav@1890
    88
jaroslav@1890
    89
    private static final long serialVersionUID = -2479143111061671589L;
jaroslav@1890
    90
jaroslav@1890
    91
    /**
jaroslav@1890
    92
     * The underlying map. Uses Boolean.TRUE as value for each
jaroslav@1890
    93
     * element.  This field is declared final for the sake of thread
jaroslav@1890
    94
     * safety, which entails some ugliness in clone()
jaroslav@1890
    95
     */
jaroslav@1890
    96
    private final ConcurrentNavigableMap<E,Object> m;
jaroslav@1890
    97
jaroslav@1890
    98
    /**
jaroslav@1890
    99
     * Constructs a new, empty set that orders its elements according to
jaroslav@1890
   100
     * their {@linkplain Comparable natural ordering}.
jaroslav@1890
   101
     */
jaroslav@1890
   102
    public ConcurrentSkipListSet() {
jaroslav@1890
   103
        m = new ConcurrentSkipListMap<E,Object>();
jaroslav@1890
   104
    }
jaroslav@1890
   105
jaroslav@1890
   106
    /**
jaroslav@1890
   107
     * Constructs a new, empty set that orders its elements according to
jaroslav@1890
   108
     * the specified comparator.
jaroslav@1890
   109
     *
jaroslav@1890
   110
     * @param comparator the comparator that will be used to order this set.
jaroslav@1890
   111
     *        If <tt>null</tt>, the {@linkplain Comparable natural
jaroslav@1890
   112
     *        ordering} of the elements will be used.
jaroslav@1890
   113
     */
jaroslav@1890
   114
    public ConcurrentSkipListSet(Comparator<? super E> comparator) {
jaroslav@1890
   115
        m = new ConcurrentSkipListMap<E,Object>(comparator);
jaroslav@1890
   116
    }
jaroslav@1890
   117
jaroslav@1890
   118
    /**
jaroslav@1890
   119
     * Constructs a new set containing the elements in the specified
jaroslav@1890
   120
     * collection, that orders its elements according to their
jaroslav@1890
   121
     * {@linkplain Comparable natural ordering}.
jaroslav@1890
   122
     *
jaroslav@1890
   123
     * @param c The elements that will comprise the new set
jaroslav@1890
   124
     * @throws ClassCastException if the elements in <tt>c</tt> are
jaroslav@1890
   125
     *         not {@link Comparable}, or are not mutually comparable
jaroslav@1890
   126
     * @throws NullPointerException if the specified collection or any
jaroslav@1890
   127
     *         of its elements are null
jaroslav@1890
   128
     */
jaroslav@1890
   129
    public ConcurrentSkipListSet(Collection<? extends E> c) {
jaroslav@1890
   130
        m = new ConcurrentSkipListMap<E,Object>();
jaroslav@1890
   131
        addAll(c);
jaroslav@1890
   132
    }
jaroslav@1890
   133
jaroslav@1890
   134
    /**
jaroslav@1890
   135
     * Constructs a new set containing the same elements and using the
jaroslav@1890
   136
     * same ordering as the specified sorted set.
jaroslav@1890
   137
     *
jaroslav@1890
   138
     * @param s sorted set whose elements will comprise the new set
jaroslav@1890
   139
     * @throws NullPointerException if the specified sorted set or any
jaroslav@1890
   140
     *         of its elements are null
jaroslav@1890
   141
     */
jaroslav@1890
   142
    public ConcurrentSkipListSet(SortedSet<E> s) {
jaroslav@1890
   143
        m = new ConcurrentSkipListMap<E,Object>(s.comparator());
jaroslav@1890
   144
        addAll(s);
jaroslav@1890
   145
    }
jaroslav@1890
   146
jaroslav@1890
   147
    /**
jaroslav@1890
   148
     * For use by submaps
jaroslav@1890
   149
     */
jaroslav@1890
   150
    ConcurrentSkipListSet(ConcurrentNavigableMap<E,Object> m) {
jaroslav@1890
   151
        this.m = m;
jaroslav@1890
   152
    }
jaroslav@1890
   153
jaroslav@1890
   154
    /**
jaroslav@1890
   155
     * Returns a shallow copy of this <tt>ConcurrentSkipListSet</tt>
jaroslav@1890
   156
     * instance. (The elements themselves are not cloned.)
jaroslav@1890
   157
     *
jaroslav@1890
   158
     * @return a shallow copy of this set
jaroslav@1890
   159
     */
jaroslav@1890
   160
    public ConcurrentSkipListSet<E> clone() {
jaroslav@1890
   161
        ConcurrentSkipListSet<E> clone = null;
jaroslav@1890
   162
        try {
jaroslav@1890
   163
            clone = (ConcurrentSkipListSet<E>) super.clone();
jaroslav@1890
   164
            clone.setMap(new ConcurrentSkipListMap(m));
jaroslav@1890
   165
        } catch (CloneNotSupportedException e) {
jaroslav@1890
   166
            throw new InternalError();
jaroslav@1890
   167
        }
jaroslav@1890
   168
jaroslav@1890
   169
        return clone;
jaroslav@1890
   170
    }
jaroslav@1890
   171
jaroslav@1890
   172
    /* ---------------- Set operations -------------- */
jaroslav@1890
   173
jaroslav@1890
   174
    /**
jaroslav@1890
   175
     * Returns the number of elements in this set.  If this set
jaroslav@1890
   176
     * contains more than <tt>Integer.MAX_VALUE</tt> elements, it
jaroslav@1890
   177
     * returns <tt>Integer.MAX_VALUE</tt>.
jaroslav@1890
   178
     *
jaroslav@1890
   179
     * <p>Beware that, unlike in most collections, this method is
jaroslav@1890
   180
     * <em>NOT</em> a constant-time operation. Because of the
jaroslav@1890
   181
     * asynchronous nature of these sets, determining the current
jaroslav@1890
   182
     * number of elements requires traversing them all to count them.
jaroslav@1890
   183
     * Additionally, it is possible for the size to change during
jaroslav@1890
   184
     * execution of this method, in which case the returned result
jaroslav@1890
   185
     * will be inaccurate. Thus, this method is typically not very
jaroslav@1890
   186
     * useful in concurrent applications.
jaroslav@1890
   187
     *
jaroslav@1890
   188
     * @return the number of elements in this set
jaroslav@1890
   189
     */
jaroslav@1890
   190
    public int size() {
jaroslav@1890
   191
        return m.size();
jaroslav@1890
   192
    }
jaroslav@1890
   193
jaroslav@1890
   194
    /**
jaroslav@1890
   195
     * Returns <tt>true</tt> if this set contains no elements.
jaroslav@1890
   196
     * @return <tt>true</tt> if this set contains no elements
jaroslav@1890
   197
     */
jaroslav@1890
   198
    public boolean isEmpty() {
jaroslav@1890
   199
        return m.isEmpty();
jaroslav@1890
   200
    }
jaroslav@1890
   201
jaroslav@1890
   202
    /**
jaroslav@1890
   203
     * Returns <tt>true</tt> if this set contains the specified element.
jaroslav@1890
   204
     * More formally, returns <tt>true</tt> if and only if this set
jaroslav@1890
   205
     * contains an element <tt>e</tt> such that <tt>o.equals(e)</tt>.
jaroslav@1890
   206
     *
jaroslav@1890
   207
     * @param o object to be checked for containment in this set
jaroslav@1890
   208
     * @return <tt>true</tt> if this set contains the specified element
jaroslav@1890
   209
     * @throws ClassCastException if the specified element cannot be
jaroslav@1890
   210
     *         compared with the elements currently in this set
jaroslav@1890
   211
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   212
     */
jaroslav@1890
   213
    public boolean contains(Object o) {
jaroslav@1890
   214
        return m.containsKey(o);
jaroslav@1890
   215
    }
jaroslav@1890
   216
jaroslav@1890
   217
    /**
jaroslav@1890
   218
     * Adds the specified element to this set if it is not already present.
jaroslav@1890
   219
     * More formally, adds the specified element <tt>e</tt> to this set if
jaroslav@1890
   220
     * the set contains no element <tt>e2</tt> such that <tt>e.equals(e2)</tt>.
jaroslav@1890
   221
     * If this set already contains the element, the call leaves the set
jaroslav@1890
   222
     * unchanged and returns <tt>false</tt>.
jaroslav@1890
   223
     *
jaroslav@1890
   224
     * @param e element to be added to this set
jaroslav@1890
   225
     * @return <tt>true</tt> if this set did not already contain the
jaroslav@1890
   226
     *         specified element
jaroslav@1890
   227
     * @throws ClassCastException if <tt>e</tt> cannot be compared
jaroslav@1890
   228
     *         with the elements currently in this set
jaroslav@1890
   229
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   230
     */
jaroslav@1890
   231
    public boolean add(E e) {
jaroslav@1890
   232
        return m.putIfAbsent(e, Boolean.TRUE) == null;
jaroslav@1890
   233
    }
jaroslav@1890
   234
jaroslav@1890
   235
    /**
jaroslav@1890
   236
     * Removes the specified element from this set if it is present.
jaroslav@1890
   237
     * More formally, removes an element <tt>e</tt> such that
jaroslav@1890
   238
     * <tt>o.equals(e)</tt>, if this set contains such an element.
jaroslav@1890
   239
     * Returns <tt>true</tt> if this set contained the element (or
jaroslav@1890
   240
     * equivalently, if this set changed as a result of the call).
jaroslav@1890
   241
     * (This set will not contain the element once the call returns.)
jaroslav@1890
   242
     *
jaroslav@1890
   243
     * @param o object to be removed from this set, if present
jaroslav@1890
   244
     * @return <tt>true</tt> if this set contained the specified element
jaroslav@1890
   245
     * @throws ClassCastException if <tt>o</tt> cannot be compared
jaroslav@1890
   246
     *         with the elements currently in this set
jaroslav@1890
   247
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   248
     */
jaroslav@1890
   249
    public boolean remove(Object o) {
jaroslav@1890
   250
        return m.remove(o, Boolean.TRUE);
jaroslav@1890
   251
    }
jaroslav@1890
   252
jaroslav@1890
   253
    /**
jaroslav@1890
   254
     * Removes all of the elements from this set.
jaroslav@1890
   255
     */
jaroslav@1890
   256
    public void clear() {
jaroslav@1890
   257
        m.clear();
jaroslav@1890
   258
    }
jaroslav@1890
   259
jaroslav@1890
   260
    /**
jaroslav@1890
   261
     * Returns an iterator over the elements in this set in ascending order.
jaroslav@1890
   262
     *
jaroslav@1890
   263
     * @return an iterator over the elements in this set in ascending order
jaroslav@1890
   264
     */
jaroslav@1890
   265
    public Iterator<E> iterator() {
jaroslav@1890
   266
        return m.navigableKeySet().iterator();
jaroslav@1890
   267
    }
jaroslav@1890
   268
jaroslav@1890
   269
    /**
jaroslav@1890
   270
     * Returns an iterator over the elements in this set in descending order.
jaroslav@1890
   271
     *
jaroslav@1890
   272
     * @return an iterator over the elements in this set in descending order
jaroslav@1890
   273
     */
jaroslav@1890
   274
    public Iterator<E> descendingIterator() {
jaroslav@1890
   275
        return m.descendingKeySet().iterator();
jaroslav@1890
   276
    }
jaroslav@1890
   277
jaroslav@1890
   278
jaroslav@1890
   279
    /* ---------------- AbstractSet Overrides -------------- */
jaroslav@1890
   280
jaroslav@1890
   281
    /**
jaroslav@1890
   282
     * Compares the specified object with this set for equality.  Returns
jaroslav@1890
   283
     * <tt>true</tt> if the specified object is also a set, the two sets
jaroslav@1890
   284
     * have the same size, and every member of the specified set is
jaroslav@1890
   285
     * contained in this set (or equivalently, every member of this set is
jaroslav@1890
   286
     * contained in the specified set).  This definition ensures that the
jaroslav@1890
   287
     * equals method works properly across different implementations of the
jaroslav@1890
   288
     * set interface.
jaroslav@1890
   289
     *
jaroslav@1890
   290
     * @param o the object to be compared for equality with this set
jaroslav@1890
   291
     * @return <tt>true</tt> if the specified object is equal to this set
jaroslav@1890
   292
     */
jaroslav@1890
   293
    public boolean equals(Object o) {
jaroslav@1890
   294
        // Override AbstractSet version to avoid calling size()
jaroslav@1890
   295
        if (o == this)
jaroslav@1890
   296
            return true;
jaroslav@1890
   297
        if (!(o instanceof Set))
jaroslav@1890
   298
            return false;
jaroslav@1890
   299
        Collection<?> c = (Collection<?>) o;
jaroslav@1890
   300
        try {
jaroslav@1890
   301
            return containsAll(c) && c.containsAll(this);
jaroslav@1890
   302
        } catch (ClassCastException unused)   {
jaroslav@1890
   303
            return false;
jaroslav@1890
   304
        } catch (NullPointerException unused) {
jaroslav@1890
   305
            return false;
jaroslav@1890
   306
        }
jaroslav@1890
   307
    }
jaroslav@1890
   308
jaroslav@1890
   309
    /**
jaroslav@1890
   310
     * Removes from this set all of its elements that are contained in
jaroslav@1890
   311
     * the specified collection.  If the specified collection is also
jaroslav@1890
   312
     * a set, this operation effectively modifies this set so that its
jaroslav@1890
   313
     * value is the <i>asymmetric set difference</i> of the two sets.
jaroslav@1890
   314
     *
jaroslav@1890
   315
     * @param  c collection containing elements to be removed from this set
jaroslav@1890
   316
     * @return <tt>true</tt> if this set changed as a result of the call
jaroslav@1890
   317
     * @throws ClassCastException if the types of one or more elements in this
jaroslav@1890
   318
     *         set are incompatible with the specified collection
jaroslav@1890
   319
     * @throws NullPointerException if the specified collection or any
jaroslav@1890
   320
     *         of its elements are null
jaroslav@1890
   321
     */
jaroslav@1890
   322
    public boolean removeAll(Collection<?> c) {
jaroslav@1890
   323
        // Override AbstractSet version to avoid unnecessary call to size()
jaroslav@1890
   324
        boolean modified = false;
jaroslav@1890
   325
        for (Iterator<?> i = c.iterator(); i.hasNext(); )
jaroslav@1890
   326
            if (remove(i.next()))
jaroslav@1890
   327
                modified = true;
jaroslav@1890
   328
        return modified;
jaroslav@1890
   329
    }
jaroslav@1890
   330
jaroslav@1890
   331
    /* ---------------- Relational operations -------------- */
jaroslav@1890
   332
jaroslav@1890
   333
    /**
jaroslav@1890
   334
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   335
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   336
     */
jaroslav@1890
   337
    public E lower(E e) {
jaroslav@1890
   338
        return m.lowerKey(e);
jaroslav@1890
   339
    }
jaroslav@1890
   340
jaroslav@1890
   341
    /**
jaroslav@1890
   342
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   343
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   344
     */
jaroslav@1890
   345
    public E floor(E e) {
jaroslav@1890
   346
        return m.floorKey(e);
jaroslav@1890
   347
    }
jaroslav@1890
   348
jaroslav@1890
   349
    /**
jaroslav@1890
   350
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   351
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   352
     */
jaroslav@1890
   353
    public E ceiling(E e) {
jaroslav@1890
   354
        return m.ceilingKey(e);
jaroslav@1890
   355
    }
jaroslav@1890
   356
jaroslav@1890
   357
    /**
jaroslav@1890
   358
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   359
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   360
     */
jaroslav@1890
   361
    public E higher(E e) {
jaroslav@1890
   362
        return m.higherKey(e);
jaroslav@1890
   363
    }
jaroslav@1890
   364
jaroslav@1890
   365
    public E pollFirst() {
jaroslav@1890
   366
        Map.Entry<E,Object> e = m.pollFirstEntry();
jaroslav@1890
   367
        return (e == null) ? null : e.getKey();
jaroslav@1890
   368
    }
jaroslav@1890
   369
jaroslav@1890
   370
    public E pollLast() {
jaroslav@1890
   371
        Map.Entry<E,Object> e = m.pollLastEntry();
jaroslav@1890
   372
        return (e == null) ? null : e.getKey();
jaroslav@1890
   373
    }
jaroslav@1890
   374
jaroslav@1890
   375
jaroslav@1890
   376
    /* ---------------- SortedSet operations -------------- */
jaroslav@1890
   377
jaroslav@1890
   378
jaroslav@1890
   379
    public Comparator<? super E> comparator() {
jaroslav@1890
   380
        return m.comparator();
jaroslav@1890
   381
    }
jaroslav@1890
   382
jaroslav@1890
   383
    /**
jaroslav@1890
   384
     * @throws NoSuchElementException {@inheritDoc}
jaroslav@1890
   385
     */
jaroslav@1890
   386
    public E first() {
jaroslav@1890
   387
        return m.firstKey();
jaroslav@1890
   388
    }
jaroslav@1890
   389
jaroslav@1890
   390
    /**
jaroslav@1890
   391
     * @throws NoSuchElementException {@inheritDoc}
jaroslav@1890
   392
     */
jaroslav@1890
   393
    public E last() {
jaroslav@1890
   394
        return m.lastKey();
jaroslav@1890
   395
    }
jaroslav@1890
   396
jaroslav@1890
   397
    /**
jaroslav@1890
   398
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   399
     * @throws NullPointerException if {@code fromElement} or
jaroslav@1890
   400
     *         {@code toElement} is null
jaroslav@1890
   401
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   402
     */
jaroslav@1890
   403
    public NavigableSet<E> subSet(E fromElement,
jaroslav@1890
   404
                                  boolean fromInclusive,
jaroslav@1890
   405
                                  E toElement,
jaroslav@1890
   406
                                  boolean toInclusive) {
jaroslav@1890
   407
        return new ConcurrentSkipListSet<E>
jaroslav@1890
   408
            (m.subMap(fromElement, fromInclusive,
jaroslav@1890
   409
                      toElement,   toInclusive));
jaroslav@1890
   410
    }
jaroslav@1890
   411
jaroslav@1890
   412
    /**
jaroslav@1890
   413
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   414
     * @throws NullPointerException if {@code toElement} is null
jaroslav@1890
   415
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   416
     */
jaroslav@1890
   417
    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
jaroslav@1890
   418
        return new ConcurrentSkipListSet<E>(m.headMap(toElement, inclusive));
jaroslav@1890
   419
    }
jaroslav@1890
   420
jaroslav@1890
   421
    /**
jaroslav@1890
   422
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   423
     * @throws NullPointerException if {@code fromElement} is null
jaroslav@1890
   424
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   425
     */
jaroslav@1890
   426
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
jaroslav@1890
   427
        return new ConcurrentSkipListSet<E>(m.tailMap(fromElement, inclusive));
jaroslav@1890
   428
    }
jaroslav@1890
   429
jaroslav@1890
   430
    /**
jaroslav@1890
   431
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   432
     * @throws NullPointerException if {@code fromElement} or
jaroslav@1890
   433
     *         {@code toElement} is null
jaroslav@1890
   434
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   435
     */
jaroslav@1890
   436
    public NavigableSet<E> subSet(E fromElement, E toElement) {
jaroslav@1890
   437
        return subSet(fromElement, true, toElement, false);
jaroslav@1890
   438
    }
jaroslav@1890
   439
jaroslav@1890
   440
    /**
jaroslav@1890
   441
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   442
     * @throws NullPointerException if {@code toElement} is null
jaroslav@1890
   443
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   444
     */
jaroslav@1890
   445
    public NavigableSet<E> headSet(E toElement) {
jaroslav@1890
   446
        return headSet(toElement, false);
jaroslav@1890
   447
    }
jaroslav@1890
   448
jaroslav@1890
   449
    /**
jaroslav@1890
   450
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   451
     * @throws NullPointerException if {@code fromElement} is null
jaroslav@1890
   452
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   453
     */
jaroslav@1890
   454
    public NavigableSet<E> tailSet(E fromElement) {
jaroslav@1890
   455
        return tailSet(fromElement, true);
jaroslav@1890
   456
    }
jaroslav@1890
   457
jaroslav@1890
   458
    /**
jaroslav@1890
   459
     * Returns a reverse order view of the elements contained in this set.
jaroslav@1890
   460
     * The descending set is backed by this set, so changes to the set are
jaroslav@1890
   461
     * reflected in the descending set, and vice-versa.
jaroslav@1890
   462
     *
jaroslav@1890
   463
     * <p>The returned set has an ordering equivalent to
jaroslav@1890
   464
     * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
jaroslav@1890
   465
     * The expression {@code s.descendingSet().descendingSet()} returns a
jaroslav@1890
   466
     * view of {@code s} essentially equivalent to {@code s}.
jaroslav@1890
   467
     *
jaroslav@1890
   468
     * @return a reverse order view of this set
jaroslav@1890
   469
     */
jaroslav@1890
   470
    public NavigableSet<E> descendingSet() {
jaroslav@1890
   471
        return new ConcurrentSkipListSet(m.descendingMap());
jaroslav@1890
   472
    }
jaroslav@1890
   473
jaroslav@1890
   474
    // Support for resetting map in clone
jaroslav@1890
   475
    private void setMap(ConcurrentNavigableMap<E,Object> map) {
jaroslav@1890
   476
        UNSAFE.putObjectVolatile(this, mapOffset, map);
jaroslav@1890
   477
    }
jaroslav@1890
   478
jaroslav@1890
   479
    private static final sun.misc.Unsafe UNSAFE;
jaroslav@1890
   480
    private static final long mapOffset;
jaroslav@1890
   481
    static {
jaroslav@1890
   482
        try {
jaroslav@1890
   483
            UNSAFE = sun.misc.Unsafe.getUnsafe();
jaroslav@1890
   484
            Class k = ConcurrentSkipListSet.class;
jaroslav@1890
   485
            mapOffset = UNSAFE.objectFieldOffset
jaroslav@1890
   486
                (k.getDeclaredField("m"));
jaroslav@1890
   487
        } catch (Exception e) {
jaroslav@1890
   488
            throw new Error(e);
jaroslav@1890
   489
        }
jaroslav@1890
   490
    }
jaroslav@1890
   491
}