rt/emul/compact/src/main/java/java/util/SortedSet.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 597 emul/compact/src/main/java/java/util/SortedSet.java@ee8a922f4268
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@597
     1
/*
jaroslav@597
     2
 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@597
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@597
     4
 *
jaroslav@597
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@597
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@597
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@597
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@597
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@597
    10
 *
jaroslav@597
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@597
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@597
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@597
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@597
    15
 * accompanied this code).
jaroslav@597
    16
 *
jaroslav@597
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@597
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@597
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@597
    20
 *
jaroslav@597
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@597
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@597
    23
 * questions.
jaroslav@597
    24
 */
jaroslav@597
    25
jaroslav@597
    26
package java.util;
jaroslav@597
    27
jaroslav@597
    28
/**
jaroslav@597
    29
 * A {@link Set} that further provides a <i>total ordering</i> on its elements.
jaroslav@597
    30
 * The elements are ordered using their {@linkplain Comparable natural
jaroslav@597
    31
 * ordering}, or by a {@link Comparator} typically provided at sorted
jaroslav@597
    32
 * set creation time.  The set's iterator will traverse the set in
jaroslav@597
    33
 * ascending element order. Several additional operations are provided
jaroslav@597
    34
 * to take advantage of the ordering.  (This interface is the set
jaroslav@597
    35
 * analogue of {@link SortedMap}.)
jaroslav@597
    36
 *
jaroslav@597
    37
 * <p>All elements inserted into a sorted set must implement the <tt>Comparable</tt>
jaroslav@597
    38
 * interface (or be accepted by the specified comparator).  Furthermore, all
jaroslav@597
    39
 * such elements must be <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt>
jaroslav@597
    40
 * (or <tt>comparator.compare(e1, e2)</tt>) must not throw a
jaroslav@597
    41
 * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and <tt>e2</tt> in
jaroslav@597
    42
 * the sorted set.  Attempts to violate this restriction will cause the
jaroslav@597
    43
 * offending method or constructor invocation to throw a
jaroslav@597
    44
 * <tt>ClassCastException</tt>.
jaroslav@597
    45
 *
jaroslav@597
    46
 * <p>Note that the ordering maintained by a sorted set (whether or not an
jaroslav@597
    47
 * explicit comparator is provided) must be <i>consistent with equals</i> if
jaroslav@597
    48
 * the sorted set is to correctly implement the <tt>Set</tt> interface.  (See
jaroslav@597
    49
 * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
jaroslav@597
    50
 * precise definition of <i>consistent with equals</i>.)  This is so because
jaroslav@597
    51
 * the <tt>Set</tt> interface is defined in terms of the <tt>equals</tt>
jaroslav@597
    52
 * operation, but a sorted set performs all element comparisons using its
jaroslav@597
    53
 * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two elements that are
jaroslav@597
    54
 * deemed equal by this method are, from the standpoint of the sorted set,
jaroslav@597
    55
 * equal.  The behavior of a sorted set <i>is</i> well-defined even if its
jaroslav@597
    56
 * ordering is inconsistent with equals; it just fails to obey the general
jaroslav@597
    57
 * contract of the <tt>Set</tt> interface.
jaroslav@597
    58
 *
jaroslav@597
    59
 * <p>All general-purpose sorted set implementation classes should
jaroslav@597
    60
 * provide four "standard" constructors: 1) A void (no arguments)
jaroslav@597
    61
 * constructor, which creates an empty sorted set sorted according to
jaroslav@597
    62
 * the natural ordering of its elements.  2) A constructor with a
jaroslav@597
    63
 * single argument of type <tt>Comparator</tt>, which creates an empty
jaroslav@597
    64
 * sorted set sorted according to the specified comparator.  3) A
jaroslav@597
    65
 * constructor with a single argument of type <tt>Collection</tt>,
jaroslav@597
    66
 * which creates a new sorted set with the same elements as its
jaroslav@597
    67
 * argument, sorted according to the natural ordering of the elements.
jaroslav@597
    68
 * 4) A constructor with a single argument of type <tt>SortedSet</tt>,
jaroslav@597
    69
 * which creates a new sorted set with the same elements and the same
jaroslav@597
    70
 * ordering as the input sorted set.  There is no way to enforce this
jaroslav@597
    71
 * recommendation, as interfaces cannot contain constructors.
jaroslav@597
    72
 *
jaroslav@597
    73
 * <p>Note: several methods return subsets with restricted ranges.
jaroslav@597
    74
 * Such ranges are <i>half-open</i>, that is, they include their low
jaroslav@597
    75
 * endpoint but not their high endpoint (where applicable).
jaroslav@597
    76
 * If you need a <i>closed range</i> (which includes both endpoints), and
jaroslav@597
    77
 * the element type allows for calculation of the successor of a given
jaroslav@597
    78
 * value, merely request the subrange from <tt>lowEndpoint</tt> to
jaroslav@597
    79
 * <tt>successor(highEndpoint)</tt>.  For example, suppose that <tt>s</tt>
jaroslav@597
    80
 * is a sorted set of strings.  The following idiom obtains a view
jaroslav@597
    81
 * containing all of the strings in <tt>s</tt> from <tt>low</tt> to
jaroslav@597
    82
 * <tt>high</tt>, inclusive:<pre>
jaroslav@597
    83
 *   SortedSet&lt;String&gt; sub = s.subSet(low, high+"\0");</pre>
jaroslav@597
    84
 *
jaroslav@597
    85
 * A similar technique can be used to generate an <i>open range</i> (which
jaroslav@597
    86
 * contains neither endpoint).  The following idiom obtains a view
jaroslav@597
    87
 * containing all of the Strings in <tt>s</tt> from <tt>low</tt> to
jaroslav@597
    88
 * <tt>high</tt>, exclusive:<pre>
jaroslav@597
    89
 *   SortedSet&lt;String&gt; sub = s.subSet(low+"\0", high);</pre>
jaroslav@597
    90
 *
jaroslav@597
    91
 * <p>This interface is a member of the
jaroslav@597
    92
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@597
    93
 * Java Collections Framework</a>.
jaroslav@597
    94
 *
jaroslav@597
    95
 * @param <E> the type of elements maintained by this set
jaroslav@597
    96
 *
jaroslav@597
    97
 * @author  Josh Bloch
jaroslav@597
    98
 * @see Set
jaroslav@597
    99
 * @see TreeSet
jaroslav@597
   100
 * @see SortedMap
jaroslav@597
   101
 * @see Collection
jaroslav@597
   102
 * @see Comparable
jaroslav@597
   103
 * @see Comparator
jaroslav@597
   104
 * @see ClassCastException
jaroslav@597
   105
 * @since 1.2
jaroslav@597
   106
 */
jaroslav@597
   107
jaroslav@597
   108
public interface SortedSet<E> extends Set<E> {
jaroslav@597
   109
    /**
jaroslav@597
   110
     * Returns the comparator used to order the elements in this set,
jaroslav@597
   111
     * or <tt>null</tt> if this set uses the {@linkplain Comparable
jaroslav@597
   112
     * natural ordering} of its elements.
jaroslav@597
   113
     *
jaroslav@597
   114
     * @return the comparator used to order the elements in this set,
jaroslav@597
   115
     *         or <tt>null</tt> if this set uses the natural ordering
jaroslav@597
   116
     *         of its elements
jaroslav@597
   117
     */
jaroslav@597
   118
    Comparator<? super E> comparator();
jaroslav@597
   119
jaroslav@597
   120
    /**
jaroslav@597
   121
     * Returns a view of the portion of this set whose elements range
jaroslav@597
   122
     * from <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>,
jaroslav@597
   123
     * exclusive.  (If <tt>fromElement</tt> and <tt>toElement</tt> are
jaroslav@597
   124
     * equal, the returned set is empty.)  The returned set is backed
jaroslav@597
   125
     * by this set, so changes in the returned set are reflected in
jaroslav@597
   126
     * this set, and vice-versa.  The returned set supports all
jaroslav@597
   127
     * optional set operations that this set supports.
jaroslav@597
   128
     *
jaroslav@597
   129
     * <p>The returned set will throw an <tt>IllegalArgumentException</tt>
jaroslav@597
   130
     * on an attempt to insert an element outside its range.
jaroslav@597
   131
     *
jaroslav@597
   132
     * @param fromElement low endpoint (inclusive) of the returned set
jaroslav@597
   133
     * @param toElement high endpoint (exclusive) of the returned set
jaroslav@597
   134
     * @return a view of the portion of this set whose elements range from
jaroslav@597
   135
     *         <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, exclusive
jaroslav@597
   136
     * @throws ClassCastException if <tt>fromElement</tt> and
jaroslav@597
   137
     *         <tt>toElement</tt> cannot be compared to one another using this
jaroslav@597
   138
     *         set's comparator (or, if the set has no comparator, using
jaroslav@597
   139
     *         natural ordering).  Implementations may, but are not required
jaroslav@597
   140
     *         to, throw this exception if <tt>fromElement</tt> or
jaroslav@597
   141
     *         <tt>toElement</tt> cannot be compared to elements currently in
jaroslav@597
   142
     *         the set.
jaroslav@597
   143
     * @throws NullPointerException if <tt>fromElement</tt> or
jaroslav@597
   144
     *         <tt>toElement</tt> is null and this set does not permit null
jaroslav@597
   145
     *         elements
jaroslav@597
   146
     * @throws IllegalArgumentException if <tt>fromElement</tt> is
jaroslav@597
   147
     *         greater than <tt>toElement</tt>; or if this set itself
jaroslav@597
   148
     *         has a restricted range, and <tt>fromElement</tt> or
jaroslav@597
   149
     *         <tt>toElement</tt> lies outside the bounds of the range
jaroslav@597
   150
     */
jaroslav@597
   151
    SortedSet<E> subSet(E fromElement, E toElement);
jaroslav@597
   152
jaroslav@597
   153
    /**
jaroslav@597
   154
     * Returns a view of the portion of this set whose elements are
jaroslav@597
   155
     * strictly less than <tt>toElement</tt>.  The returned set is
jaroslav@597
   156
     * backed by this set, so changes in the returned set are
jaroslav@597
   157
     * reflected in this set, and vice-versa.  The returned set
jaroslav@597
   158
     * supports all optional set operations that this set supports.
jaroslav@597
   159
     *
jaroslav@597
   160
     * <p>The returned set will throw an <tt>IllegalArgumentException</tt>
jaroslav@597
   161
     * on an attempt to insert an element outside its range.
jaroslav@597
   162
     *
jaroslav@597
   163
     * @param toElement high endpoint (exclusive) of the returned set
jaroslav@597
   164
     * @return a view of the portion of this set whose elements are strictly
jaroslav@597
   165
     *         less than <tt>toElement</tt>
jaroslav@597
   166
     * @throws ClassCastException if <tt>toElement</tt> is not compatible
jaroslav@597
   167
     *         with this set's comparator (or, if the set has no comparator,
jaroslav@597
   168
     *         if <tt>toElement</tt> does not implement {@link Comparable}).
jaroslav@597
   169
     *         Implementations may, but are not required to, throw this
jaroslav@597
   170
     *         exception if <tt>toElement</tt> cannot be compared to elements
jaroslav@597
   171
     *         currently in the set.
jaroslav@597
   172
     * @throws NullPointerException if <tt>toElement</tt> is null and
jaroslav@597
   173
     *         this set does not permit null elements
jaroslav@597
   174
     * @throws IllegalArgumentException if this set itself has a
jaroslav@597
   175
     *         restricted range, and <tt>toElement</tt> lies outside the
jaroslav@597
   176
     *         bounds of the range
jaroslav@597
   177
     */
jaroslav@597
   178
    SortedSet<E> headSet(E toElement);
jaroslav@597
   179
jaroslav@597
   180
    /**
jaroslav@597
   181
     * Returns a view of the portion of this set whose elements are
jaroslav@597
   182
     * greater than or equal to <tt>fromElement</tt>.  The returned
jaroslav@597
   183
     * set is backed by this set, so changes in the returned set are
jaroslav@597
   184
     * reflected in this set, and vice-versa.  The returned set
jaroslav@597
   185
     * supports all optional set operations that this set supports.
jaroslav@597
   186
     *
jaroslav@597
   187
     * <p>The returned set will throw an <tt>IllegalArgumentException</tt>
jaroslav@597
   188
     * on an attempt to insert an element outside its range.
jaroslav@597
   189
     *
jaroslav@597
   190
     * @param fromElement low endpoint (inclusive) of the returned set
jaroslav@597
   191
     * @return a view of the portion of this set whose elements are greater
jaroslav@597
   192
     *         than or equal to <tt>fromElement</tt>
jaroslav@597
   193
     * @throws ClassCastException if <tt>fromElement</tt> is not compatible
jaroslav@597
   194
     *         with this set's comparator (or, if the set has no comparator,
jaroslav@597
   195
     *         if <tt>fromElement</tt> does not implement {@link Comparable}).
jaroslav@597
   196
     *         Implementations may, but are not required to, throw this
jaroslav@597
   197
     *         exception if <tt>fromElement</tt> cannot be compared to elements
jaroslav@597
   198
     *         currently in the set.
jaroslav@597
   199
     * @throws NullPointerException if <tt>fromElement</tt> is null
jaroslav@597
   200
     *         and this set does not permit null elements
jaroslav@597
   201
     * @throws IllegalArgumentException if this set itself has a
jaroslav@597
   202
     *         restricted range, and <tt>fromElement</tt> lies outside the
jaroslav@597
   203
     *         bounds of the range
jaroslav@597
   204
     */
jaroslav@597
   205
    SortedSet<E> tailSet(E fromElement);
jaroslav@597
   206
jaroslav@597
   207
    /**
jaroslav@597
   208
     * Returns the first (lowest) element currently in this set.
jaroslav@597
   209
     *
jaroslav@597
   210
     * @return the first (lowest) element currently in this set
jaroslav@597
   211
     * @throws NoSuchElementException if this set is empty
jaroslav@597
   212
     */
jaroslav@597
   213
    E first();
jaroslav@597
   214
jaroslav@597
   215
    /**
jaroslav@597
   216
     * Returns the last (highest) element currently in this set.
jaroslav@597
   217
     *
jaroslav@597
   218
     * @return the last (highest) element currently in this set
jaroslav@597
   219
     * @throws NoSuchElementException if this set is empty
jaroslav@597
   220
     */
jaroslav@597
   221
    E last();
jaroslav@597
   222
}