src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListSet.java
author darcy
Wed, 16 Oct 2019 16:55:52 -0700
changeset 58657 6252605fb005
parent 49433 b6671a111395
permissions -rw-r--r--
8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 8544
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
46145
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
    38
import java.lang.reflect.Field;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    39
import java.util.AbstractSet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    40
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    41
import java.util.Collections;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    42
import java.util.Comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    43
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    44
import java.util.Map;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    45
import java.util.NavigableSet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    46
import java.util.Set;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    47
import java.util.SortedSet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    48
import java.util.Spliterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A scalable concurrent {@link NavigableSet} implementation based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * a {@link ConcurrentSkipListMap}.  The elements of the set are kept
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * sorted according to their {@linkplain Comparable natural ordering},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * or by a {@link Comparator} provided at set creation time, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * on which constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>This implementation provides expected average <i>log(n)</i> time
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    58
 * cost for the {@code contains}, {@code add}, and {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * operations and their variants.  Insertion, removal, and access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * operations safely execute concurrently by multiple threads.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
    61
 *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
    62
 * <p>Iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
    63
 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
    64
 *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
    65
 * <p>Ascending ordered views and their iterators are faster than
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
    66
 * descending ones.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    68
 * <p>Beware that, unlike in most collections, the {@code size}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * method is <em>not</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * asynchronous nature of these sets, determining the current number
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    71
 * of elements requires a traversal of the elements, and so may report
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    72
 * inaccurate results if this collection is modified during traversal.
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 39725
diff changeset
    73
 *
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 39725
diff changeset
    74
 * <p>Bulk operations that add, remove, or examine multiple elements,
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 39725
diff changeset
    75
 * such as {@link #addAll}, {@link #removeIf} or {@link #forEach},
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 39725
diff changeset
    76
 * are <em>not</em> guaranteed to be performed atomically.
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 39725
diff changeset
    77
 * For example, a {@code forEach} traversal concurrent with an {@code
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 39725
diff changeset
    78
 * addAll} operation might observe only some of the added elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>This class and its iterators implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <em>optional</em> methods of the {@link Set} and {@link Iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * interfaces. Like most other concurrent collection implementations,
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    83
 * this class does not permit the use of {@code null} elements,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    84
 * because {@code null} arguments and return values cannot be reliably
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * distinguished from the absence of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
    88
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @param <E> the type of elements maintained by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
public class ConcurrentSkipListSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    implements NavigableSet<E>, Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final long serialVersionUID = -2479143111061671589L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The underlying map. Uses Boolean.TRUE as value for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * element.  This field is declared final for the sake of thread
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   104
     * safety, which entails some ugliness in clone().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 49433
diff changeset
   106
    @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private final ConcurrentNavigableMap<E,Object> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Constructs a new, empty set that orders its elements according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * their {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public ConcurrentSkipListSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        m = new ConcurrentSkipListMap<E,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Constructs a new, empty set that orders its elements according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param comparator the comparator that will be used to order this set.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   122
     *        If {@code null}, the {@linkplain Comparable natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *        ordering} of the elements will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public ConcurrentSkipListSet(Comparator<? super E> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        m = new ConcurrentSkipListMap<E,Object>(comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Constructs a new set containing the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * collection, that orders its elements according to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param c The elements that will comprise the new set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   135
     * @throws ClassCastException if the elements in {@code c} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         not {@link Comparable}, or are not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public ConcurrentSkipListSet(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        m = new ConcurrentSkipListMap<E,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Constructs a new set containing the same elements and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * same ordering as the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param s sorted set whose elements will comprise the new set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws NullPointerException if the specified sorted set or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public ConcurrentSkipListSet(SortedSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        m = new ConcurrentSkipListMap<E,Object>(s.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        addAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * For use by submaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    ConcurrentSkipListSet(ConcurrentNavigableMap<E,Object> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   166
     * Returns a shallow copy of this {@code ConcurrentSkipListSet}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * instance. (The elements themselves are not cloned.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return a shallow copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public ConcurrentSkipListSet<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   173
            @SuppressWarnings("unchecked")
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   174
            ConcurrentSkipListSet<E> clone =
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   175
                (ConcurrentSkipListSet<E>) super.clone();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   176
            clone.setMap(new ConcurrentSkipListMap<E,Object>(m));
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   177
            return clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /* ---------------- Set operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the number of elements in this set.  If this set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   187
     * contains more than {@code Integer.MAX_VALUE} elements, it
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   188
     * returns {@code Integer.MAX_VALUE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <p>Beware that, unlike in most collections, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * asynchronous nature of these sets, determining the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * number of elements requires traversing them all to count them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Additionally, it is possible for the size to change during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * execution of this method, in which case the returned result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * will be inaccurate. Thus, this method is typically not very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * useful in concurrent applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return the number of elements in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   206
     * Returns {@code true} if this set contains no elements.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   207
     * @return {@code true} if this set contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return m.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   214
     * Returns {@code true} if this set contains the specified element.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   215
     * More formally, returns {@code true} if and only if this set
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   216
     * contains an element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param o object to be checked for containment in this set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   219
     * @return {@code true} if this set contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws ClassCastException if the specified element cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *         compared with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return m.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Adds the specified element to this set if it is not already present.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   230
     * More formally, adds the specified element {@code e} to this set if
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   231
     * the set contains no element {@code e2} such that {@code e.equals(e2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * If this set already contains the element, the call leaves the set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   233
     * unchanged and returns {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param e element to be added to this set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   236
     * @return {@code true} if this set did not already contain the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *         specified element
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   238
     * @throws ClassCastException if {@code e} cannot be compared
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *         with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return m.putIfAbsent(e, Boolean.TRUE) == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Removes the specified element from this set if it is present.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   248
     * More formally, removes an element {@code e} such that
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   249
     * {@code o.equals(e)}, if this set contains such an element.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   250
     * Returns {@code true} if this set contained the element (or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * equivalently, if this set changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * (This set will not contain the element once the call returns.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param o object to be removed from this set, if present
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   255
     * @return {@code true} if this set contained the specified element
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   256
     * @throws ClassCastException if {@code o} cannot be compared
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *         with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return m.remove(o, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Removes all of the elements from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        m.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns an iterator over the elements in this set in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return an iterator over the elements in this set in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return m.navigableKeySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Returns an iterator over the elements in this set in descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return an iterator over the elements in this set in descending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return m.descendingKeySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /* ---------------- AbstractSet Overrides -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Compares the specified object with this set for equality.  Returns
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   294
     * {@code true} if the specified object is also a set, the two sets
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * have the same size, and every member of the specified set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * contained in this set (or equivalently, every member of this set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * contained in the specified set).  This definition ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * equals method works properly across different implementations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * set interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param o the object to be compared for equality with this set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   302
     * @return {@code true} if the specified object is equal to this set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Override AbstractSet version to avoid calling size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        Collection<?> c = (Collection<?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return containsAll(c) && c.containsAll(this);
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
   313
        } catch (ClassCastException | NullPointerException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Removes from this set all of its elements that are contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * the specified collection.  If the specified collection is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * a set, this operation effectively modifies this set so that its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * value is the <i>asymmetric set difference</i> of the two sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param  c collection containing elements to be removed from this set
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   325
     * @return {@code true} if this set changed as a result of the call
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   326
     * @throws ClassCastException if the class of an element of this set
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   327
     *         is incompatible with the specified collection
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   328
     * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // Override AbstractSet version to avoid unnecessary call to size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        boolean modified = false;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   335
        for (Object e : c)
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   336
            if (remove(e))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /* ---------------- Relational operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public E lower(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return m.lowerKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public E floor(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return m.floorKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public E ceiling(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return m.ceilingKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public E higher(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return m.higherKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        Map.Entry<E,Object> e = m.pollFirstEntry();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   377
        return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        Map.Entry<E,Object> e = m.pollLastEntry();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   382
        return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /* ---------------- SortedSet operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public Comparator<? super E> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   393
     * @throws java.util.NoSuchElementException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public E first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return m.firstKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   400
     * @throws java.util.NoSuchElementException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public E last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return m.lastKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws NullPointerException if {@code fromElement} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *         {@code toElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public NavigableSet<E> subSet(E fromElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                  boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                  E toElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                  boolean toInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return new ConcurrentSkipListSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            (m.subMap(fromElement, fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                      toElement,   toInclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @throws NullPointerException if {@code toElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return new ConcurrentSkipListSet<E>(m.headMap(toElement, inclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @throws NullPointerException if {@code fromElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return new ConcurrentSkipListSet<E>(m.tailMap(fromElement, inclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @throws NullPointerException if {@code fromElement} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *         {@code toElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public NavigableSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return subSet(fromElement, true, toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @throws NullPointerException if {@code toElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public NavigableSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return headSet(toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @throws NullPointerException if {@code fromElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public NavigableSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return tailSet(fromElement, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Returns a reverse order view of the elements contained in this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * The descending set is backed by this set, so changes to the set are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * reflected in the descending set, and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <p>The returned set has an ordering equivalent to
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   473
     * {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}{@code (comparator())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * The expression {@code s.descendingSet().descendingSet()} returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * view of {@code s} essentially equivalent to {@code s}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @return a reverse order view of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public NavigableSet<E> descendingSet() {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   480
        return new ConcurrentSkipListSet<E>(m.descendingMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   483
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   484
     * Returns a {@link Spliterator} over the elements in this set.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   485
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   486
     * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   487
     * {@link Spliterator#NONNULL}, {@link Spliterator#DISTINCT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   488
     * {@link Spliterator#SORTED} and {@link Spliterator#ORDERED}, with an
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   489
     * encounter order that is ascending order.  Overriding implementations
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   490
     * should document the reporting of additional characteristic values.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   491
     *
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
   492
     * <p>The {@linkplain Spliterator#getComparator() spliterator's comparator}
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
   493
     * is {@code null} if the {@linkplain #comparator() set's comparator}
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
   494
     * is {@code null}.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   495
     * Otherwise, the spliterator's comparator is the same as or imposes the
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   496
     * same total ordering as the set's comparator.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   497
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   498
     * @return a {@code Spliterator} over the elements in this set
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   499
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   500
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   501
    public Spliterator<E> spliterator() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   502
        return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   503
            ? ((ConcurrentSkipListMap<E,?>)m).keySpliterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   504
            : ((ConcurrentSkipListMap.SubMap<E,?>)m).new SubMapKeyIterator();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   505
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   506
46145
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   507
    /** Initializes map field; for use in clone. */
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7518
diff changeset
   508
    private void setMap(ConcurrentNavigableMap<E,Object> map) {
46145
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   509
        Field mapField = java.security.AccessController.doPrivileged(
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   510
            (java.security.PrivilegedAction<Field>) () -> {
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   511
                try {
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   512
                    Field f = ConcurrentSkipListSet.class
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   513
                        .getDeclaredField("m");
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   514
                    f.setAccessible(true);
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   515
                    return f;
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   516
                } catch (ReflectiveOperationException e) {
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   517
                    throw new Error(e);
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   518
                }});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        try {
46145
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   520
            mapField.set(this, map);
1a5028372b29 8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException
dl
parents: 45937
diff changeset
   521
        } catch (IllegalAccessException e) {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7518
diff changeset
   522
            throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7518
diff changeset
   523
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
}