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