jdk/src/share/classes/java/util/TreeSet.java
author psandoz
Tue, 06 Aug 2013 14:26:34 +0100
changeset 19435 9d7530ff42cb
parent 17168 b7d3500f2516
child 22078 bdec5d53e98c
permissions -rw-r--r--
8014824: Document Spliterator characteristics and binding policy of java util collection impls Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12448
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A {@link NavigableSet} implementation based on a {@link TreeMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The elements are ordered using their {@linkplain Comparable natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * ordering}, or by a {@link Comparator} provided at set creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * time, depending on which constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>This implementation provides guaranteed log(n) time cost for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * operations ({@code add}, {@code remove} and {@code contains}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>Note that the ordering maintained by a set (whether or not an explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * comparator is provided) must be <i>consistent with equals</i> if it is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * correctly implement the {@code Set} interface.  (See {@code Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * or {@code Comparator} for a precise definition of <i>consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * equals</i>.)  This is so because the {@code Set} interface is defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * terms of the {@code equals} operation, but a {@code TreeSet} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * performs all element comparisons using its {@code compareTo} (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@code compare}) method, so two elements that are deemed equal by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * are, from the standpoint of the set, equal.  The behavior of a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * just fails to obey the general contract of the {@code Set} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * If multiple threads access a tree set concurrently, and at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * of the threads modifies the set, it <i>must</i> be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * externally.  This is typically accomplished by synchronizing on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * object that naturally encapsulates the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * If no such object exists, the set should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * unsynchronized access to the set: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>The iterators returned by this class's {@code iterator} method are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <i>fail-fast</i>: if the set is modified at any time after the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * created, in any way except through the iterator's own {@code remove}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * method, the iterator will throw a {@link ConcurrentModificationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Thus, in the face of concurrent modification, the iterator fails quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * and cleanly, rather than risking arbitrary, non-deterministic behavior at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * throw {@code ConcurrentModificationException} on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exception for its correctness:   <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @param <E> the type of elements maintained by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see     Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see     HashSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see     Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see     Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class TreeSet<E> extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    implements NavigableSet<E>, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private transient NavigableMap<E,Object> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Dummy value to associate with an Object in the backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final Object PRESENT = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Constructs a set backed by the specified navigable map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    TreeSet(NavigableMap<E,Object> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.m = 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs a new, empty tree set, sorted according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * natural ordering of its elements.  All elements inserted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the set must implement the {@link Comparable} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Furthermore, all such elements must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * comparable</i>: {@code e1.compareTo(e2)} must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * {@code ClassCastException} for any elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * {@code e2} in the set.  If the user attempts to add an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * to the set that violates this constraint (for example, the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * attempts to add a string element to a set whose elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * integers), the {@code add} call will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * {@code ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public TreeSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this(new TreeMap<E,Object>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Constructs a new, empty tree set, sorted according to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * comparator.  All elements inserted into the set must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * comparable</i> by the specified comparator: {@code comparator.compare(e1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * e2)} must not throw a {@code ClassCastException} for any elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * {@code e1} and {@code e2} in the set.  If the user attempts to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * an element to the set that violates this constraint, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * {@code add} call will throw a {@code ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param comparator the comparator that will be used to order this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *        If {@code null}, the {@linkplain Comparable natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *        ordering} of the elements will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public TreeSet(Comparator<? super E> comparator) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   141
        this(new TreeMap<>(comparator));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs a new tree set containing the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * collection, sorted according to the <i>natural ordering</i> of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * elements.  All elements inserted into the set must implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * {@link Comparable} interface.  Furthermore, all such elements must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@code ClassCastException} for any elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * {@code e2} in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param c collection whose elements will comprise the new set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws ClassCastException if the elements in {@code c} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         not {@link Comparable}, or are not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public TreeSet(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Constructs a new tree set containing the same elements and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * using the same ordering as the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param s sorted set whose elements will comprise the new set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @throws NullPointerException if the specified sorted set is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public TreeSet(SortedSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this(s.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        addAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns an iterator over the elements in this set in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return an iterator over the elements in this set in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return m.navigableKeySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns an iterator over the elements in this set in descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return an iterator over the elements in this set in descending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return m.descendingKeySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public NavigableSet<E> descendingSet() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   198
        return new TreeSet<>(m.descendingMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the number of elements in this set (its cardinality).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the number of elements in this set (its cardinality)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns {@code true} if this set contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return {@code true} if this set contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return m.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns {@code true} if this set contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * More formally, returns {@code true} if and only if this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * contains an element {@code e} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param o object to be checked for containment in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return {@code true} if this set contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @throws ClassCastException if the specified object cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         with the elements currently in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return m.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Adds the specified element to this set if it is not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * More formally, adds the specified element {@code e} to this set if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * the set contains no element {@code e2} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * If this set already contains the element, the call leaves the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * unchanged and returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param e element to be added to this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return {@code true} if this set did not already contain the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *         element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @throws ClassCastException if the specified object cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *         with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return m.put(e, PRESENT)==null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Removes the specified element from this set if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * More formally, removes an element {@code e} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * if this set contains such an element.  Returns {@code true} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * this set contained the element (or equivalently, if this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * changed as a result of the call).  (This set will not contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * element once the call returns.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param o object to be removed from this set, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return {@code true} if this set contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws ClassCastException if the specified object cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *         with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return m.remove(o)==PRESENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Removes all of the elements from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * The set will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        m.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Adds all of the elements in the specified collection to this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param c collection containing elements to be added to this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return {@code true} if this set changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws ClassCastException if the elements provided cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *         with the elements currently in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws NullPointerException if the specified collection is null or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *         if any element is null and this set uses natural ordering, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *         its comparator does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public  boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // Use linear-time version if applicable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (m.size()==0 && c.size() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            c instanceof SortedSet &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            m instanceof TreeMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            SortedSet<? extends E> set = (SortedSet<? extends E>) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            TreeMap<E,Object> map = (TreeMap<E, Object>) m;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   305
            Comparator<?> cc = set.comparator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            Comparator<? super E> mc = map.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (cc==mc || (cc != null && cc.equals(mc))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                map.addAllForTreeSet(set, PRESENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return super.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws NullPointerException if {@code fromElement} or {@code toElement}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         is null and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                  E toElement,   boolean toInclusive) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   325
        return new TreeSet<>(m.subMap(fromElement, fromInclusive,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                       toElement,   toInclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws NullPointerException if {@code toElement} is null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *         this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   338
        return new TreeSet<>(m.headMap(toElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @throws NullPointerException if {@code fromElement} is null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   350
        return new TreeSet<>(m.tailMap(fromElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @throws NullPointerException if {@code fromElement} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *         {@code toElement} is null and this set uses natural ordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         or its comparator does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return subSet(fromElement, true, toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws NullPointerException if {@code toElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         and this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return headSet(toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws NullPointerException if {@code fromElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *         and this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return tailSet(fromElement, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public Comparator<? super E> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public E first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return m.firstKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public E last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return m.lastKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    // NavigableSet API methods
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 the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public E lower(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return m.lowerKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public E floor(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return m.floorKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public E ceiling(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return m.ceilingKey(e);
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 the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public E higher(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return m.higherKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        Map.Entry<E,?> e = m.pollFirstEntry();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   455
        return (e == null) ? null : e.getKey();
2
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
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        Map.Entry<E,?> e = m.pollLastEntry();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   463
        return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Returns a shallow copy of this {@code TreeSet} instance. (The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * themselves are not cloned.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return a shallow copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   472
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public Object clone() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   474
        TreeSet<E> clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            clone = (TreeSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7816
diff changeset
   478
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   481
        clone.m = new TreeMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Save the state of the {@code TreeSet} instance to a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @serialData Emits the comparator used to order this set, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *             {@code null} if it obeys its elements' natural ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *             (Object), followed by the size of the set (the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *             elements it contains) (int), followed by all of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *             elements (each an Object) in order (as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *             set's Comparator, or by the elements' natural ordering if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *             the set has no Comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // Write out any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // Write out Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        s.writeObject(m.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        s.writeInt(m.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        // Write out all elements in the proper order.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   509
        for (E e : m.keySet())
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   510
            s.writeObject(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Reconstitute the {@code TreeSet} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // Read in any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // Read in Comparator
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   523
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   524
            Comparator<? super E> c = (Comparator<? super E>) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // Create backing TreeMap
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   527
        TreeMap<E,Object> tm = new TreeMap<>(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        m = tm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // Read in size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        tm.readTreeSet(size, s, PRESENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   536
    /**
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   537
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   538
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   539
     * set.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   540
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   541
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   542
     * {@link Spliterator#DISTINCT}, {@link Spliterator#SORTED}, and
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   543
     * {@link Spliterator#ORDERED}.  Overriding implementations should document
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   544
     * the reporting of additional characteristic values.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   545
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   546
     * <p>The spliterator's comparator (see
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   547
     * {@link java.util.Spliterator#getComparator()}) is {@code null} if
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   548
     * the tree set's comparator (see {@link #comparator()}) is {@code null}.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   549
     * Otherwise, the spliterator's comparator is the same as or imposes the
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   550
     * same total ordering as the tree set's comparator.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   551
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   552
     * @return a {@code Spliterator} over the elements in this set
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   553
     * @since 1.8
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   554
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   555
    public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   556
        return TreeMap.keySpliteratorFor(m);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   557
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   558
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private static final long serialVersionUID = -2479143000061671589L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
}