emul/compact/src/main/java/java/util/NavigableSet.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
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     3
 *
jaroslav@1258
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
     9
 *
jaroslav@1258
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    14
 * accompanied this code).
jaroslav@1258
    15
 *
jaroslav@1258
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    19
 *
jaroslav@1258
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    22
 * questions.
jaroslav@1258
    23
 */
jaroslav@1258
    24
jaroslav@1258
    25
/*
jaroslav@1258
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1258
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1258
    28
 * However, the following notice accompanied the original version of this
jaroslav@1258
    29
 * file:
jaroslav@1258
    30
 *
jaroslav@1258
    31
 * Written by Doug Lea and Josh Bloch with assistance from members of JCP
jaroslav@1258
    32
 * JSR-166 Expert Group and released to the public domain, as explained at
jaroslav@1258
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1258
    34
 */
jaroslav@1258
    35
jaroslav@1258
    36
package java.util;
jaroslav@1258
    37
jaroslav@1258
    38
/**
jaroslav@1258
    39
 * A {@link SortedSet} extended with navigation methods reporting
jaroslav@1258
    40
 * closest matches for given search targets. Methods {@code lower},
jaroslav@1258
    41
 * {@code floor}, {@code ceiling}, and {@code higher} return elements
jaroslav@1258
    42
 * respectively less than, less than or equal, greater than or equal,
jaroslav@1258
    43
 * and greater than a given element, returning {@code null} if there
jaroslav@1258
    44
 * is no such element.  A {@code NavigableSet} may be accessed and
jaroslav@1258
    45
 * traversed in either ascending or descending order.  The {@code
jaroslav@1258
    46
 * descendingSet} method returns a view of the set with the senses of
jaroslav@1258
    47
 * all relational and directional methods inverted. The performance of
jaroslav@1258
    48
 * ascending operations and views is likely to be faster than that of
jaroslav@1258
    49
 * descending ones.  This interface additionally defines methods
jaroslav@1258
    50
 * {@code pollFirst} and {@code pollLast} that return and remove the
jaroslav@1258
    51
 * lowest and highest element, if one exists, else returning {@code
jaroslav@1258
    52
 * null}.  Methods {@code subSet}, {@code headSet},
jaroslav@1258
    53
 * and {@code tailSet} differ from the like-named {@code
jaroslav@1258
    54
 * SortedSet} methods in accepting additional arguments describing
jaroslav@1258
    55
 * whether lower and upper bounds are inclusive versus exclusive.
jaroslav@1258
    56
 * Subsets of any {@code NavigableSet} must implement the {@code
jaroslav@1258
    57
 * NavigableSet} interface.
jaroslav@1258
    58
 *
jaroslav@1258
    59
 * <p> The return values of navigation methods may be ambiguous in
jaroslav@1258
    60
 * implementations that permit {@code null} elements. However, even
jaroslav@1258
    61
 * in this case the result can be disambiguated by checking
jaroslav@1258
    62
 * {@code contains(null)}. To avoid such issues, implementations of
jaroslav@1258
    63
 * this interface are encouraged to <em>not</em> permit insertion of
jaroslav@1258
    64
 * {@code null} elements. (Note that sorted sets of {@link
jaroslav@1258
    65
 * Comparable} elements intrinsically do not permit {@code null}.)
jaroslav@1258
    66
 *
jaroslav@1258
    67
 * <p>Methods
jaroslav@1258
    68
 * {@link #subSet(Object, Object) subSet(E, E)},
jaroslav@1258
    69
 * {@link #headSet(Object) headSet(E)}, and
jaroslav@1258
    70
 * {@link #tailSet(Object) tailSet(E)}
jaroslav@1258
    71
 * are specified to return {@code SortedSet} to allow existing
jaroslav@1258
    72
 * implementations of {@code SortedSet} to be compatibly retrofitted to
jaroslav@1258
    73
 * implement {@code NavigableSet}, but extensions and implementations
jaroslav@1258
    74
 * of this interface are encouraged to override these methods to return
jaroslav@1258
    75
 * {@code NavigableSet}.
jaroslav@1258
    76
 *
jaroslav@1258
    77
 * <p>This interface is a member of the
jaroslav@1258
    78
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@1258
    79
 * Java Collections Framework</a>.
jaroslav@1258
    80
 *
jaroslav@1258
    81
 * @author Doug Lea
jaroslav@1258
    82
 * @author Josh Bloch
jaroslav@1258
    83
 * @param <E> the type of elements maintained by this set
jaroslav@1258
    84
 * @since 1.6
jaroslav@1258
    85
 */
jaroslav@1258
    86
public interface NavigableSet<E> extends SortedSet<E> {
jaroslav@1258
    87
    /**
jaroslav@1258
    88
     * Returns the greatest element in this set strictly less than the
jaroslav@1258
    89
     * given element, or {@code null} if there is no such element.
jaroslav@1258
    90
     *
jaroslav@1258
    91
     * @param e the value to match
jaroslav@1258
    92
     * @return the greatest element less than {@code e},
jaroslav@1258
    93
     *         or {@code null} if there is no such element
jaroslav@1258
    94
     * @throws ClassCastException if the specified element cannot be
jaroslav@1258
    95
     *         compared with the elements currently in the set
jaroslav@1258
    96
     * @throws NullPointerException if the specified element is null
jaroslav@1258
    97
     *         and this set does not permit null elements
jaroslav@1258
    98
     */
jaroslav@1258
    99
    E lower(E e);
jaroslav@1258
   100
jaroslav@1258
   101
    /**
jaroslav@1258
   102
     * Returns the greatest element in this set less than or equal to
jaroslav@1258
   103
     * the given element, or {@code null} if there is no such element.
jaroslav@1258
   104
     *
jaroslav@1258
   105
     * @param e the value to match
jaroslav@1258
   106
     * @return the greatest element less than or equal to {@code e},
jaroslav@1258
   107
     *         or {@code null} if there is no such element
jaroslav@1258
   108
     * @throws ClassCastException if the specified element cannot be
jaroslav@1258
   109
     *         compared with the elements currently in the set
jaroslav@1258
   110
     * @throws NullPointerException if the specified element is null
jaroslav@1258
   111
     *         and this set does not permit null elements
jaroslav@1258
   112
     */
jaroslav@1258
   113
    E floor(E e);
jaroslav@1258
   114
jaroslav@1258
   115
    /**
jaroslav@1258
   116
     * Returns the least element in this set greater than or equal to
jaroslav@1258
   117
     * the given element, or {@code null} if there is no such element.
jaroslav@1258
   118
     *
jaroslav@1258
   119
     * @param e the value to match
jaroslav@1258
   120
     * @return the least element greater than or equal to {@code e},
jaroslav@1258
   121
     *         or {@code null} if there is no such element
jaroslav@1258
   122
     * @throws ClassCastException if the specified element cannot be
jaroslav@1258
   123
     *         compared with the elements currently in the set
jaroslav@1258
   124
     * @throws NullPointerException if the specified element is null
jaroslav@1258
   125
     *         and this set does not permit null elements
jaroslav@1258
   126
     */
jaroslav@1258
   127
    E ceiling(E e);
jaroslav@1258
   128
jaroslav@1258
   129
    /**
jaroslav@1258
   130
     * Returns the least element in this set strictly greater than the
jaroslav@1258
   131
     * given element, or {@code null} if there is no such element.
jaroslav@1258
   132
     *
jaroslav@1258
   133
     * @param e the value to match
jaroslav@1258
   134
     * @return the least element greater than {@code e},
jaroslav@1258
   135
     *         or {@code null} if there is no such element
jaroslav@1258
   136
     * @throws ClassCastException if the specified element cannot be
jaroslav@1258
   137
     *         compared with the elements currently in the set
jaroslav@1258
   138
     * @throws NullPointerException if the specified element is null
jaroslav@1258
   139
     *         and this set does not permit null elements
jaroslav@1258
   140
     */
jaroslav@1258
   141
    E higher(E e);
jaroslav@1258
   142
jaroslav@1258
   143
    /**
jaroslav@1258
   144
     * Retrieves and removes the first (lowest) element,
jaroslav@1258
   145
     * or returns {@code null} if this set is empty.
jaroslav@1258
   146
     *
jaroslav@1258
   147
     * @return the first element, or {@code null} if this set is empty
jaroslav@1258
   148
     */
jaroslav@1258
   149
    E pollFirst();
jaroslav@1258
   150
jaroslav@1258
   151
    /**
jaroslav@1258
   152
     * Retrieves and removes the last (highest) element,
jaroslav@1258
   153
     * or returns {@code null} if this set is empty.
jaroslav@1258
   154
     *
jaroslav@1258
   155
     * @return the last element, or {@code null} if this set is empty
jaroslav@1258
   156
     */
jaroslav@1258
   157
    E pollLast();
jaroslav@1258
   158
jaroslav@1258
   159
    /**
jaroslav@1258
   160
     * Returns an iterator over the elements in this set, in ascending order.
jaroslav@1258
   161
     *
jaroslav@1258
   162
     * @return an iterator over the elements in this set, in ascending order
jaroslav@1258
   163
     */
jaroslav@1258
   164
    Iterator<E> iterator();
jaroslav@1258
   165
jaroslav@1258
   166
    /**
jaroslav@1258
   167
     * Returns a reverse order view of the elements contained in this set.
jaroslav@1258
   168
     * The descending set is backed by this set, so changes to the set are
jaroslav@1258
   169
     * reflected in the descending set, and vice-versa.  If either set is
jaroslav@1258
   170
     * modified while an iteration over either set is in progress (except
jaroslav@1258
   171
     * through the iterator's own {@code remove} operation), the results of
jaroslav@1258
   172
     * the iteration are undefined.
jaroslav@1258
   173
     *
jaroslav@1258
   174
     * <p>The returned set has an ordering equivalent to
jaroslav@1258
   175
     * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
jaroslav@1258
   176
     * The expression {@code s.descendingSet().descendingSet()} returns a
jaroslav@1258
   177
     * view of {@code s} essentially equivalent to {@code s}.
jaroslav@1258
   178
     *
jaroslav@1258
   179
     * @return a reverse order view of this set
jaroslav@1258
   180
     */
jaroslav@1258
   181
    NavigableSet<E> descendingSet();
jaroslav@1258
   182
jaroslav@1258
   183
    /**
jaroslav@1258
   184
     * Returns an iterator over the elements in this set, in descending order.
jaroslav@1258
   185
     * Equivalent in effect to {@code descendingSet().iterator()}.
jaroslav@1258
   186
     *
jaroslav@1258
   187
     * @return an iterator over the elements in this set, in descending order
jaroslav@1258
   188
     */
jaroslav@1258
   189
    Iterator<E> descendingIterator();
jaroslav@1258
   190
jaroslav@1258
   191
    /**
jaroslav@1258
   192
     * Returns a view of the portion of this set whose elements range from
jaroslav@1258
   193
     * {@code fromElement} to {@code toElement}.  If {@code fromElement} and
jaroslav@1258
   194
     * {@code toElement} are equal, the returned set is empty unless {@code
jaroslav@1258
   195
     * fromInclusive} and {@code toInclusive} are both true.  The returned set
jaroslav@1258
   196
     * is backed by this set, so changes in the returned set are reflected in
jaroslav@1258
   197
     * this set, and vice-versa.  The returned set supports all optional set
jaroslav@1258
   198
     * operations that this set supports.
jaroslav@1258
   199
     *
jaroslav@1258
   200
     * <p>The returned set will throw an {@code IllegalArgumentException}
jaroslav@1258
   201
     * on an attempt to insert an element outside its range.
jaroslav@1258
   202
     *
jaroslav@1258
   203
     * @param fromElement low endpoint of the returned set
jaroslav@1258
   204
     * @param fromInclusive {@code true} if the low endpoint
jaroslav@1258
   205
     *        is to be included in the returned view
jaroslav@1258
   206
     * @param toElement high endpoint of the returned set
jaroslav@1258
   207
     * @param toInclusive {@code true} if the high endpoint
jaroslav@1258
   208
     *        is to be included in the returned view
jaroslav@1258
   209
     * @return a view of the portion of this set whose elements range from
jaroslav@1258
   210
     *         {@code fromElement}, inclusive, to {@code toElement}, exclusive
jaroslav@1258
   211
     * @throws ClassCastException if {@code fromElement} and
jaroslav@1258
   212
     *         {@code toElement} cannot be compared to one another using this
jaroslav@1258
   213
     *         set's comparator (or, if the set has no comparator, using
jaroslav@1258
   214
     *         natural ordering).  Implementations may, but are not required
jaroslav@1258
   215
     *         to, throw this exception if {@code fromElement} or
jaroslav@1258
   216
     *         {@code toElement} cannot be compared to elements currently in
jaroslav@1258
   217
     *         the set.
jaroslav@1258
   218
     * @throws NullPointerException if {@code fromElement} or
jaroslav@1258
   219
     *         {@code toElement} is null and this set does
jaroslav@1258
   220
     *         not permit null elements
jaroslav@1258
   221
     * @throws IllegalArgumentException if {@code fromElement} is
jaroslav@1258
   222
     *         greater than {@code toElement}; or if this set itself
jaroslav@1258
   223
     *         has a restricted range, and {@code fromElement} or
jaroslav@1258
   224
     *         {@code toElement} lies outside the bounds of the range.
jaroslav@1258
   225
     */
jaroslav@1258
   226
    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
jaroslav@1258
   227
                           E toElement,   boolean toInclusive);
jaroslav@1258
   228
jaroslav@1258
   229
    /**
jaroslav@1258
   230
     * Returns a view of the portion of this set whose elements are less than
jaroslav@1258
   231
     * (or equal to, if {@code inclusive} is true) {@code toElement}.  The
jaroslav@1258
   232
     * returned set is backed by this set, so changes in the returned set are
jaroslav@1258
   233
     * reflected in this set, and vice-versa.  The returned set supports all
jaroslav@1258
   234
     * optional set operations that this set supports.
jaroslav@1258
   235
     *
jaroslav@1258
   236
     * <p>The returned set will throw an {@code IllegalArgumentException}
jaroslav@1258
   237
     * on an attempt to insert an element outside its range.
jaroslav@1258
   238
     *
jaroslav@1258
   239
     * @param toElement high endpoint of the returned set
jaroslav@1258
   240
     * @param inclusive {@code true} if the high endpoint
jaroslav@1258
   241
     *        is to be included in the returned view
jaroslav@1258
   242
     * @return a view of the portion of this set whose elements are less than
jaroslav@1258
   243
     *         (or equal to, if {@code inclusive} is true) {@code toElement}
jaroslav@1258
   244
     * @throws ClassCastException if {@code toElement} is not compatible
jaroslav@1258
   245
     *         with this set's comparator (or, if the set has no comparator,
jaroslav@1258
   246
     *         if {@code toElement} does not implement {@link Comparable}).
jaroslav@1258
   247
     *         Implementations may, but are not required to, throw this
jaroslav@1258
   248
     *         exception if {@code toElement} cannot be compared to elements
jaroslav@1258
   249
     *         currently in the set.
jaroslav@1258
   250
     * @throws NullPointerException if {@code toElement} is null and
jaroslav@1258
   251
     *         this set does not permit null elements
jaroslav@1258
   252
     * @throws IllegalArgumentException if this set itself has a
jaroslav@1258
   253
     *         restricted range, and {@code toElement} lies outside the
jaroslav@1258
   254
     *         bounds of the range
jaroslav@1258
   255
     */
jaroslav@1258
   256
    NavigableSet<E> headSet(E toElement, boolean inclusive);
jaroslav@1258
   257
jaroslav@1258
   258
    /**
jaroslav@1258
   259
     * Returns a view of the portion of this set whose elements are greater
jaroslav@1258
   260
     * than (or equal to, if {@code inclusive} is true) {@code fromElement}.
jaroslav@1258
   261
     * The returned set is backed by this set, so changes in the returned set
jaroslav@1258
   262
     * are reflected in this set, and vice-versa.  The returned set supports
jaroslav@1258
   263
     * all optional set operations that this set supports.
jaroslav@1258
   264
     *
jaroslav@1258
   265
     * <p>The returned set will throw an {@code IllegalArgumentException}
jaroslav@1258
   266
     * on an attempt to insert an element outside its range.
jaroslav@1258
   267
     *
jaroslav@1258
   268
     * @param fromElement low endpoint of the returned set
jaroslav@1258
   269
     * @param inclusive {@code true} if the low endpoint
jaroslav@1258
   270
     *        is to be included in the returned view
jaroslav@1258
   271
     * @return a view of the portion of this set whose elements are greater
jaroslav@1258
   272
     *         than or equal to {@code fromElement}
jaroslav@1258
   273
     * @throws ClassCastException if {@code fromElement} is not compatible
jaroslav@1258
   274
     *         with this set's comparator (or, if the set has no comparator,
jaroslav@1258
   275
     *         if {@code fromElement} does not implement {@link Comparable}).
jaroslav@1258
   276
     *         Implementations may, but are not required to, throw this
jaroslav@1258
   277
     *         exception if {@code fromElement} cannot be compared to elements
jaroslav@1258
   278
     *         currently in the set.
jaroslav@1258
   279
     * @throws NullPointerException if {@code fromElement} is null
jaroslav@1258
   280
     *         and this set does not permit null elements
jaroslav@1258
   281
     * @throws IllegalArgumentException if this set itself has a
jaroslav@1258
   282
     *         restricted range, and {@code fromElement} lies outside the
jaroslav@1258
   283
     *         bounds of the range
jaroslav@1258
   284
     */
jaroslav@1258
   285
    NavigableSet<E> tailSet(E fromElement, boolean inclusive);
jaroslav@1258
   286
jaroslav@1258
   287
    /**
jaroslav@1258
   288
     * {@inheritDoc}
jaroslav@1258
   289
     *
jaroslav@1258
   290
     * <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.
jaroslav@1258
   291
     *
jaroslav@1258
   292
     * @throws ClassCastException       {@inheritDoc}
jaroslav@1258
   293
     * @throws NullPointerException     {@inheritDoc}
jaroslav@1258
   294
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258
   295
     */
jaroslav@1258
   296
    SortedSet<E> subSet(E fromElement, E toElement);
jaroslav@1258
   297
jaroslav@1258
   298
    /**
jaroslav@1258
   299
     * {@inheritDoc}
jaroslav@1258
   300
     *
jaroslav@1258
   301
     * <p>Equivalent to {@code headSet(toElement, false)}.
jaroslav@1258
   302
     *
jaroslav@1258
   303
     * @throws ClassCastException       {@inheritDoc}
jaroslav@1258
   304
     * @throws NullPointerException     {@inheritDoc}
jaroslav@1258
   305
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258
   306
na     */
jaroslav@1258
   307
    SortedSet<E> headSet(E toElement);
jaroslav@1258
   308
jaroslav@1258
   309
    /**
jaroslav@1258
   310
     * {@inheritDoc}
jaroslav@1258
   311
     *
jaroslav@1258
   312
     * <p>Equivalent to {@code tailSet(fromElement, true)}.
jaroslav@1258
   313
     *
jaroslav@1258
   314
     * @throws ClassCastException       {@inheritDoc}
jaroslav@1258
   315
     * @throws NullPointerException     {@inheritDoc}
jaroslav@1258
   316
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258
   317
     */
jaroslav@1258
   318
    SortedSet<E> tailSet(E fromElement);
jaroslav@1258
   319
}