Home | History | Annotate | Download | only in util
      1 /*
      2  * Written by Doug Lea and Josh Bloch with assistance from members of JCP
      3  * JSR-166 Expert Group and released to the public domain, as explained at
      4  * http://creativecommons.org/publicdomain/zero/1.0/
      5  */
      6 
      7 package java.util;
      8 
      9 // BEGIN android-note
     10 // removed link to collections framework docs
     11 // END android-note
     12 
     13 /**
     14  * A {@link SortedMap} extended with navigation methods returning the
     15  * closest matches for given search targets. Methods
     16  * {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry},
     17  * and {@code higherEntry} return {@code Map.Entry} objects
     18  * associated with keys respectively less than, less than or equal,
     19  * greater than or equal, and greater than a given key, returning
     20  * {@code null} if there is no such key.  Similarly, methods
     21  * {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and
     22  * {@code higherKey} return only the associated keys. All of these
     23  * methods are designed for locating, not traversing entries.
     24  *
     25  * <p>A {@code NavigableMap} may be accessed and traversed in either
     26  * ascending or descending key order.  The {@code descendingMap}
     27  * method returns a view of the map with the senses of all relational
     28  * and directional methods inverted. The performance of ascending
     29  * operations and views is likely to be faster than that of descending
     30  * ones.  Methods {@code subMap}, {@code headMap},
     31  * and {@code tailMap} differ from the like-named {@code
     32  * SortedMap} methods in accepting additional arguments describing
     33  * whether lower and upper bounds are inclusive versus exclusive.
     34  * Submaps of any {@code NavigableMap} must implement the {@code
     35  * NavigableMap} interface.
     36  *
     37  * <p>This interface additionally defines methods {@code firstEntry},
     38  * {@code pollFirstEntry}, {@code lastEntry}, and
     39  * {@code pollLastEntry} that return and/or remove the least and
     40  * greatest mappings, if any exist, else returning {@code null}.
     41  *
     42  * <p>Implementations of entry-returning methods are expected to
     43  * return {@code Map.Entry} pairs representing snapshots of mappings
     44  * at the time they were produced, and thus generally do <em>not</em>
     45  * support the optional {@code Entry.setValue} method. Note however
     46  * that it is possible to change mappings in the associated map using
     47  * method {@code put}.
     48  *
     49  * <p>Methods
     50  * {@link #subMap(Object, Object) subMap(K, K)},
     51  * {@link #headMap(Object) headMap(K)}, and
     52  * {@link #tailMap(Object) tailMap(K)}
     53  * are specified to return {@code SortedMap} to allow existing
     54  * implementations of {@code SortedMap} to be compatibly retrofitted to
     55  * implement {@code NavigableMap}, but extensions and implementations
     56  * of this interface are encouraged to override these methods to return
     57  * {@code NavigableMap}.  Similarly,
     58  * {@link #keySet()} can be overridden to return {@code NavigableSet}.
     59  *
     60  * @author Doug Lea
     61  * @author Josh Bloch
     62  * @param <K> the type of keys maintained by this map
     63  * @param <V> the type of mapped values
     64  * @since 1.6
     65  */
     66 public interface NavigableMap<K,V> extends SortedMap<K,V> {
     67     /**
     68      * Returns a key-value mapping associated with the greatest key
     69      * strictly less than the given key, or {@code null} if there is
     70      * no such key.
     71      *
     72      * @param key the key
     73      * @return an entry with the greatest key less than {@code key},
     74      *         or {@code null} if there is no such key
     75      * @throws ClassCastException if the specified key cannot be compared
     76      *         with the keys currently in the map
     77      * @throws NullPointerException if the specified key is null
     78      *         and this map does not permit null keys
     79      */
     80     Map.Entry<K,V> lowerEntry(K key);
     81 
     82     /**
     83      * Returns the greatest key strictly less than the given key, or
     84      * {@code null} if there is no such key.
     85      *
     86      * @param key the key
     87      * @return the greatest key less than {@code key},
     88      *         or {@code null} if there is no such key
     89      * @throws ClassCastException if the specified key cannot be compared
     90      *         with the keys currently in the map
     91      * @throws NullPointerException if the specified key is null
     92      *         and this map does not permit null keys
     93      */
     94     K lowerKey(K key);
     95 
     96     /**
     97      * Returns a key-value mapping associated with the greatest key
     98      * less than or equal to the given key, or {@code null} if there
     99      * is no such key.
    100      *
    101      * @param key the key
    102      * @return an entry with the greatest key less than or equal to
    103      *         {@code key}, or {@code null} if there is no such key
    104      * @throws ClassCastException if the specified key cannot be compared
    105      *         with the keys currently in the map
    106      * @throws NullPointerException if the specified key is null
    107      *         and this map does not permit null keys
    108      */
    109     Map.Entry<K,V> floorEntry(K key);
    110 
    111     /**
    112      * Returns the greatest key less than or equal to the given key,
    113      * or {@code null} if there is no such key.
    114      *
    115      * @param key the key
    116      * @return the greatest key less than or equal to {@code key},
    117      *         or {@code null} if there is no such key
    118      * @throws ClassCastException if the specified key cannot be compared
    119      *         with the keys currently in the map
    120      * @throws NullPointerException if the specified key is null
    121      *         and this map does not permit null keys
    122      */
    123     K floorKey(K key);
    124 
    125     /**
    126      * Returns a key-value mapping associated with the least key
    127      * greater than or equal to the given key, or {@code null} if
    128      * there is no such key.
    129      *
    130      * @param key the key
    131      * @return an entry with the least key greater than or equal to
    132      *         {@code key}, or {@code null} if there is no such key
    133      * @throws ClassCastException if the specified key cannot be compared
    134      *         with the keys currently in the map
    135      * @throws NullPointerException if the specified key is null
    136      *         and this map does not permit null keys
    137      */
    138     Map.Entry<K,V> ceilingEntry(K key);
    139 
    140     /**
    141      * Returns the least key greater than or equal to the given key,
    142      * or {@code null} if there is no such key.
    143      *
    144      * @param key the key
    145      * @return the least key greater than or equal to {@code key},
    146      *         or {@code null} if there is no such key
    147      * @throws ClassCastException if the specified key cannot be compared
    148      *         with the keys currently in the map
    149      * @throws NullPointerException if the specified key is null
    150      *         and this map does not permit null keys
    151      */
    152     K ceilingKey(K key);
    153 
    154     /**
    155      * Returns a key-value mapping associated with the least key
    156      * strictly greater than the given key, or {@code null} if there
    157      * is no such key.
    158      *
    159      * @param key the key
    160      * @return an entry with the least key greater than {@code key},
    161      *         or {@code null} if there is no such key
    162      * @throws ClassCastException if the specified key cannot be compared
    163      *         with the keys currently in the map
    164      * @throws NullPointerException if the specified key is null
    165      *         and this map does not permit null keys
    166      */
    167     Map.Entry<K,V> higherEntry(K key);
    168 
    169     /**
    170      * Returns the least key strictly greater than the given key, or
    171      * {@code null} if there is no such key.
    172      *
    173      * @param key the key
    174      * @return the least key greater than {@code key},
    175      *         or {@code null} if there is no such key
    176      * @throws ClassCastException if the specified key cannot be compared
    177      *         with the keys currently in the map
    178      * @throws NullPointerException if the specified key is null
    179      *         and this map does not permit null keys
    180      */
    181     K higherKey(K key);
    182 
    183     /**
    184      * Returns a key-value mapping associated with the least
    185      * key in this map, or {@code null} if the map is empty.
    186      */
    187     Map.Entry<K,V> firstEntry();
    188 
    189     /**
    190      * Returns a key-value mapping associated with the greatest
    191      * key in this map, or {@code null} if the map is empty.
    192      */
    193     Map.Entry<K,V> lastEntry();
    194 
    195     /**
    196      * Removes and returns a key-value mapping associated with
    197      * the least key in this map, or {@code null} if the map is empty.
    198      *
    199      * @return the removed first entry of this map,
    200      *         or {@code null} if this map is empty
    201      */
    202     Map.Entry<K,V> pollFirstEntry();
    203 
    204     /**
    205      * Removes and returns a key-value mapping associated with
    206      * the greatest key in this map, or {@code null} if the map is empty.
    207      *
    208      * @return the removed last entry of this map,
    209      *         or {@code null} if this map is empty
    210      */
    211     Map.Entry<K,V> pollLastEntry();
    212 
    213     /**
    214      * Returns a reverse order view of the mappings contained in this map.
    215      * The descending map is backed by this map, so changes to the map are
    216      * reflected in the descending map, and vice-versa.  If either map is
    217      * modified while an iteration over a collection view of either map
    218      * is in progress (except through the iterator's own {@code remove}
    219      * operation), the results of the iteration are undefined.
    220      *
    221      * <p>The returned map has an ordering equivalent to
    222      * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
    223      * The expression {@code m.descendingMap().descendingMap()} returns a
    224      * view of {@code m} essentially equivalent to {@code m}.
    225      *
    226      * @return a reverse order view of this map
    227      */
    228     NavigableMap<K,V> descendingMap();
    229 
    230     /**
    231      * Returns a {@link NavigableSet} view of the keys contained in this map.
    232      * The set's iterator returns the keys in ascending order.
    233      * The set is backed by the map, so changes to the map are reflected in
    234      * the set, and vice-versa.  If the map is modified while an iteration
    235      * over the set is in progress (except through the iterator's own {@code
    236      * remove} operation), the results of the iteration are undefined.  The
    237      * set supports element removal, which removes the corresponding mapping
    238      * from the map, via the {@code Iterator.remove}, {@code Set.remove},
    239      * {@code removeAll}, {@code retainAll}, and {@code clear} operations.
    240      * It does not support the {@code add} or {@code addAll} operations.
    241      *
    242      * @return a navigable set view of the keys in this map
    243      */
    244     NavigableSet<K> navigableKeySet();
    245 
    246     /**
    247      * Returns a reverse order {@link NavigableSet} view of the keys contained in this map.
    248      * The set's iterator returns the keys in descending order.
    249      * The set is backed by the map, so changes to the map are reflected in
    250      * the set, and vice-versa.  If the map is modified while an iteration
    251      * over the set is in progress (except through the iterator's own {@code
    252      * remove} operation), the results of the iteration are undefined.  The
    253      * set supports element removal, which removes the corresponding mapping
    254      * from the map, via the {@code Iterator.remove}, {@code Set.remove},
    255      * {@code removeAll}, {@code retainAll}, and {@code clear} operations.
    256      * It does not support the {@code add} or {@code addAll} operations.
    257      *
    258      * @return a reverse order navigable set view of the keys in this map
    259      */
    260     NavigableSet<K> descendingKeySet();
    261 
    262     /**
    263      * Returns a view of the portion of this map whose keys range from
    264      * {@code fromKey} to {@code toKey}.  If {@code fromKey} and
    265      * {@code toKey} are equal, the returned map is empty unless
    266      * {@code fromExclusive} and {@code toExclusive} are both true.  The
    267      * returned map is backed by this map, so changes in the returned map are
    268      * reflected in this map, and vice-versa.  The returned map supports all
    269      * optional map operations that this map supports.
    270      *
    271      * <p>The returned map will throw an {@code IllegalArgumentException}
    272      * on an attempt to insert a key outside of its range, or to construct a
    273      * submap either of whose endpoints lie outside its range.
    274      *
    275      * @param fromKey low endpoint of the keys in the returned map
    276      * @param fromInclusive {@code true} if the low endpoint
    277      *        is to be included in the returned view
    278      * @param toKey high endpoint of the keys in the returned map
    279      * @param toInclusive {@code true} if the high endpoint
    280      *        is to be included in the returned view
    281      * @return a view of the portion of this map whose keys range from
    282      *         {@code fromKey} to {@code toKey}
    283      * @throws ClassCastException if {@code fromKey} and {@code toKey}
    284      *         cannot be compared to one another using this map's comparator
    285      *         (or, if the map has no comparator, using natural ordering).
    286      *         Implementations may, but are not required to, throw this
    287      *         exception if {@code fromKey} or {@code toKey}
    288      *         cannot be compared to keys currently in the map.
    289      * @throws NullPointerException if {@code fromKey} or {@code toKey}
    290      *         is null and this map does not permit null keys
    291      * @throws IllegalArgumentException if {@code fromKey} is greater than
    292      *         {@code toKey}; or if this map itself has a restricted
    293      *         range, and {@code fromKey} or {@code toKey} lies
    294      *         outside the bounds of the range
    295      */
    296     NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
    297                              K toKey,   boolean toInclusive);
    298 
    299     /**
    300      * Returns a view of the portion of this map whose keys are less than (or
    301      * equal to, if {@code inclusive} is true) {@code toKey}.  The returned
    302      * map is backed by this map, so changes in the returned map are reflected
    303      * in this map, and vice-versa.  The returned map supports all optional
    304      * map operations that this map supports.
    305      *
    306      * <p>The returned map will throw an {@code IllegalArgumentException}
    307      * on an attempt to insert a key outside its range.
    308      *
    309      * @param toKey high endpoint of the keys in the returned map
    310      * @param inclusive {@code true} if the high endpoint
    311      *        is to be included in the returned view
    312      * @return a view of the portion of this map whose keys are less than
    313      *         (or equal to, if {@code inclusive} is true) {@code toKey}
    314      * @throws ClassCastException if {@code toKey} is not compatible
    315      *         with this map's comparator (or, if the map has no comparator,
    316      *         if {@code toKey} does not implement {@link Comparable}).
    317      *         Implementations may, but are not required to, throw this
    318      *         exception if {@code toKey} cannot be compared to keys
    319      *         currently in the map.
    320      * @throws NullPointerException if {@code toKey} is null
    321      *         and this map does not permit null keys
    322      * @throws IllegalArgumentException if this map itself has a
    323      *         restricted range, and {@code toKey} lies outside the
    324      *         bounds of the range
    325      */
    326     NavigableMap<K,V> headMap(K toKey, boolean inclusive);
    327 
    328     /**
    329      * Returns a view of the portion of this map whose keys are greater than (or
    330      * equal to, if {@code inclusive} is true) {@code fromKey}.  The returned
    331      * map is backed by this map, so changes in the returned map are reflected
    332      * in this map, and vice-versa.  The returned map supports all optional
    333      * map operations that this map supports.
    334      *
    335      * <p>The returned map will throw an {@code IllegalArgumentException}
    336      * on an attempt to insert a key outside its range.
    337      *
    338      * @param fromKey low endpoint of the keys in the returned map
    339      * @param inclusive {@code true} if the low endpoint
    340      *        is to be included in the returned view
    341      * @return a view of the portion of this map whose keys are greater than
    342      *         (or equal to, if {@code inclusive} is true) {@code fromKey}
    343      * @throws ClassCastException if {@code fromKey} is not compatible
    344      *         with this map's comparator (or, if the map has no comparator,
    345      *         if {@code fromKey} does not implement {@link Comparable}).
    346      *         Implementations may, but are not required to, throw this
    347      *         exception if {@code fromKey} cannot be compared to keys
    348      *         currently in the map.
    349      * @throws NullPointerException if {@code fromKey} is null
    350      *         and this map does not permit null keys
    351      * @throws IllegalArgumentException if this map itself has a
    352      *         restricted range, and {@code fromKey} lies outside the
    353      *         bounds of the range
    354      */
    355     NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
    356 
    357     /**
    358      * {@inheritDoc}
    359      *
    360      * <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
    361      *
    362      * @throws ClassCastException       {@inheritDoc}
    363      * @throws NullPointerException     {@inheritDoc}
    364      * @throws IllegalArgumentException {@inheritDoc}
    365      */
    366     SortedMap<K,V> subMap(K fromKey, K toKey);
    367 
    368     /**
    369      * {@inheritDoc}
    370      *
    371      * <p>Equivalent to {@code headMap(toKey, false)}.
    372      *
    373      * @throws ClassCastException       {@inheritDoc}
    374      * @throws NullPointerException     {@inheritDoc}
    375      * @throws IllegalArgumentException {@inheritDoc}
    376      */
    377     SortedMap<K,V> headMap(K toKey);
    378 
    379     /**
    380      * {@inheritDoc}
    381      *
    382      * <p>Equivalent to {@code tailMap(fromKey, true)}.
    383      *
    384      * @throws ClassCastException       {@inheritDoc}
    385      * @throws NullPointerException     {@inheritDoc}
    386      * @throws IllegalArgumentException {@inheritDoc}
    387      */
    388     SortedMap<K,V> tailMap(K fromKey);
    389 }
    390