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