jaroslav@1258: /* jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1258: * jaroslav@1258: * This code is free software; you can redistribute it and/or modify it jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as jaroslav@1258: * published by the Free Software Foundation. Oracle designates this jaroslav@1258: * particular file as subject to the "Classpath" exception as provided jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1258: * jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1258: * accompanied this code). jaroslav@1258: * jaroslav@1258: * You should have received a copy of the GNU General Public License version jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1258: * jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1258: * or visit www.oracle.com if you need additional information or have any jaroslav@1258: * questions. jaroslav@1258: */ jaroslav@1258: jaroslav@1258: /* jaroslav@1258: * This file is available under and governed by the GNU General Public jaroslav@1258: * License version 2 only, as published by the Free Software Foundation. jaroslav@1258: * However, the following notice accompanied the original version of this jaroslav@1258: * file: jaroslav@1258: * jaroslav@1258: * Written by Doug Lea and Josh Bloch with assistance from members of JCP jaroslav@1258: * JSR-166 Expert Group and released to the public domain, as explained at jaroslav@1258: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@1258: */ jaroslav@1258: jaroslav@1258: package java.util; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * A {@link SortedMap} extended with navigation methods returning the jaroslav@1258: * closest matches for given search targets. Methods jaroslav@1258: * {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry}, jaroslav@1258: * and {@code higherEntry} return {@code Map.Entry} objects jaroslav@1258: * associated with keys respectively less than, less than or equal, jaroslav@1258: * greater than or equal, and greater than a given key, returning jaroslav@1258: * {@code null} if there is no such key. Similarly, methods jaroslav@1258: * {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and jaroslav@1258: * {@code higherKey} return only the associated keys. All of these jaroslav@1258: * methods are designed for locating, not traversing entries. jaroslav@1258: * jaroslav@1258: *

A {@code NavigableMap} may be accessed and traversed in either jaroslav@1258: * ascending or descending key order. The {@code descendingMap} jaroslav@1258: * method returns a view of the map with the senses of all relational jaroslav@1258: * and directional methods inverted. The performance of ascending jaroslav@1258: * operations and views is likely to be faster than that of descending jaroslav@1258: * ones. Methods {@code subMap}, {@code headMap}, jaroslav@1258: * and {@code tailMap} differ from the like-named {@code jaroslav@1258: * SortedMap} methods in accepting additional arguments describing jaroslav@1258: * whether lower and upper bounds are inclusive versus exclusive. jaroslav@1258: * Submaps of any {@code NavigableMap} must implement the {@code jaroslav@1258: * NavigableMap} interface. jaroslav@1258: * jaroslav@1258: *

This interface additionally defines methods {@code firstEntry}, jaroslav@1258: * {@code pollFirstEntry}, {@code lastEntry}, and jaroslav@1258: * {@code pollLastEntry} that return and/or remove the least and jaroslav@1258: * greatest mappings, if any exist, else returning {@code null}. jaroslav@1258: * jaroslav@1258: *

Implementations of entry-returning methods are expected to jaroslav@1258: * return {@code Map.Entry} pairs representing snapshots of mappings jaroslav@1258: * at the time they were produced, and thus generally do not jaroslav@1258: * support the optional {@code Entry.setValue} method. Note however jaroslav@1258: * that it is possible to change mappings in the associated map using jaroslav@1258: * method {@code put}. jaroslav@1258: * jaroslav@1258: *

Methods jaroslav@1258: * {@link #subMap(Object, Object) subMap(K, K)}, jaroslav@1258: * {@link #headMap(Object) headMap(K)}, and jaroslav@1258: * {@link #tailMap(Object) tailMap(K)} jaroslav@1258: * are specified to return {@code SortedMap} to allow existing jaroslav@1258: * implementations of {@code SortedMap} to be compatibly retrofitted to jaroslav@1258: * implement {@code NavigableMap}, but extensions and implementations jaroslav@1258: * of this interface are encouraged to override these methods to return jaroslav@1258: * {@code NavigableMap}. Similarly, jaroslav@1258: * {@link #keySet()} can be overriden to return {@code NavigableSet}. jaroslav@1258: * jaroslav@1258: *

This interface is a member of the jaroslav@1258: * jaroslav@1258: * Java Collections Framework. jaroslav@1258: * jaroslav@1258: * @author Doug Lea jaroslav@1258: * @author Josh Bloch jaroslav@1258: * @param the type of keys maintained by this map jaroslav@1258: * @param the type of mapped values jaroslav@1258: * @since 1.6 jaroslav@1258: */ jaroslav@1258: public interface NavigableMap extends SortedMap { jaroslav@1258: /** jaroslav@1258: * Returns a key-value mapping associated with the greatest key jaroslav@1258: * strictly less than the given key, or {@code null} if there is jaroslav@1258: * no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return an entry with the greatest key less than {@code key}, jaroslav@1258: * or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: Map.Entry lowerEntry(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns the greatest key strictly less than the given key, or jaroslav@1258: * {@code null} if there is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return the greatest key less than {@code key}, jaroslav@1258: * or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: K lowerKey(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a key-value mapping associated with the greatest key jaroslav@1258: * less than or equal to the given key, or {@code null} if there jaroslav@1258: * is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return an entry with the greatest key less than or equal to jaroslav@1258: * {@code key}, or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: Map.Entry floorEntry(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns the greatest key less than or equal to the given key, jaroslav@1258: * or {@code null} if there is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return the greatest key less than or equal to {@code key}, jaroslav@1258: * or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: K floorKey(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a key-value mapping associated with the least key jaroslav@1258: * greater than or equal to the given key, or {@code null} if jaroslav@1258: * there is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return an entry with the least key greater than or equal to jaroslav@1258: * {@code key}, or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: Map.Entry ceilingEntry(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns the least key greater than or equal to the given key, jaroslav@1258: * or {@code null} if there is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return the least key greater than or equal to {@code key}, jaroslav@1258: * or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: K ceilingKey(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a key-value mapping associated with the least key jaroslav@1258: * strictly greater than the given key, or {@code null} if there jaroslav@1258: * is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return an entry with the least key greater than {@code key}, jaroslav@1258: * or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: Map.Entry higherEntry(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns the least key strictly greater than the given key, or jaroslav@1258: * {@code null} if there is no such key. jaroslav@1258: * jaroslav@1258: * @param key the key jaroslav@1258: * @return the least key greater than {@code key}, jaroslav@1258: * or {@code null} if there is no such key jaroslav@1258: * @throws ClassCastException if the specified key cannot be compared jaroslav@1258: * with the keys currently in the map jaroslav@1258: * @throws NullPointerException if the specified key is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: */ jaroslav@1258: K higherKey(K key); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a key-value mapping associated with the least jaroslav@1258: * key in this map, or {@code null} if the map is empty. jaroslav@1258: * jaroslav@1258: * @return an entry with the least key, jaroslav@1258: * or {@code null} if this map is empty jaroslav@1258: */ jaroslav@1258: Map.Entry firstEntry(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a key-value mapping associated with the greatest jaroslav@1258: * key in this map, or {@code null} if the map is empty. jaroslav@1258: * jaroslav@1258: * @return an entry with the greatest key, jaroslav@1258: * or {@code null} if this map is empty jaroslav@1258: */ jaroslav@1258: Map.Entry lastEntry(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Removes and returns a key-value mapping associated with jaroslav@1258: * the least key in this map, or {@code null} if the map is empty. jaroslav@1258: * jaroslav@1258: * @return the removed first entry of this map, jaroslav@1258: * or {@code null} if this map is empty jaroslav@1258: */ jaroslav@1258: Map.Entry pollFirstEntry(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Removes and returns a key-value mapping associated with jaroslav@1258: * the greatest key in this map, or {@code null} if the map is empty. jaroslav@1258: * jaroslav@1258: * @return the removed last entry of this map, jaroslav@1258: * or {@code null} if this map is empty jaroslav@1258: */ jaroslav@1258: Map.Entry pollLastEntry(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a reverse order view of the mappings contained in this map. jaroslav@1258: * The descending map is backed by this map, so changes to the map are jaroslav@1258: * reflected in the descending map, and vice-versa. If either map is jaroslav@1258: * modified while an iteration over a collection view of either map jaroslav@1258: * is in progress (except through the iterator's own {@code remove} jaroslav@1258: * operation), the results of the iteration are undefined. jaroslav@1258: * jaroslav@1258: *

The returned map has an ordering equivalent to jaroslav@1258: * {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator()). jaroslav@1258: * The expression {@code m.descendingMap().descendingMap()} returns a jaroslav@1258: * view of {@code m} essentially equivalent to {@code m}. jaroslav@1258: * jaroslav@1258: * @return a reverse order view of this map jaroslav@1258: */ jaroslav@1258: NavigableMap descendingMap(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a {@link NavigableSet} view of the keys contained in this map. jaroslav@1258: * The set's iterator returns the keys in ascending order. jaroslav@1258: * The set is backed by the map, so changes to the map are reflected in jaroslav@1258: * the set, and vice-versa. If the map is modified while an iteration jaroslav@1258: * over the set is in progress (except through the iterator's own {@code jaroslav@1258: * remove} operation), the results of the iteration are undefined. The jaroslav@1258: * set supports element removal, which removes the corresponding mapping jaroslav@1258: * from the map, via the {@code Iterator.remove}, {@code Set.remove}, jaroslav@1258: * {@code removeAll}, {@code retainAll}, and {@code clear} operations. jaroslav@1258: * It does not support the {@code add} or {@code addAll} operations. jaroslav@1258: * jaroslav@1258: * @return a navigable set view of the keys in this map jaroslav@1258: */ jaroslav@1258: NavigableSet navigableKeySet(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a reverse order {@link NavigableSet} view of the keys contained in this map. jaroslav@1258: * The set's iterator returns the keys in descending order. jaroslav@1258: * The set is backed by the map, so changes to the map are reflected in jaroslav@1258: * the set, and vice-versa. If the map is modified while an iteration jaroslav@1258: * over the set is in progress (except through the iterator's own {@code jaroslav@1258: * remove} operation), the results of the iteration are undefined. The jaroslav@1258: * set supports element removal, which removes the corresponding mapping jaroslav@1258: * from the map, via the {@code Iterator.remove}, {@code Set.remove}, jaroslav@1258: * {@code removeAll}, {@code retainAll}, and {@code clear} operations. jaroslav@1258: * It does not support the {@code add} or {@code addAll} operations. jaroslav@1258: * jaroslav@1258: * @return a reverse order navigable set view of the keys in this map jaroslav@1258: */ jaroslav@1258: NavigableSet descendingKeySet(); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a view of the portion of this map whose keys range from jaroslav@1258: * {@code fromKey} to {@code toKey}. If {@code fromKey} and jaroslav@1258: * {@code toKey} are equal, the returned map is empty unless jaroslav@1258: * {@code fromInclusive} and {@code toInclusive} are both true. The jaroslav@1258: * returned map is backed by this map, so changes in the returned map are jaroslav@1258: * reflected in this map, and vice-versa. The returned map supports all jaroslav@1258: * optional map operations that this map supports. jaroslav@1258: * jaroslav@1258: *

The returned map will throw an {@code IllegalArgumentException} jaroslav@1258: * on an attempt to insert a key outside of its range, or to construct a jaroslav@1258: * submap either of whose endpoints lie outside its range. jaroslav@1258: * jaroslav@1258: * @param fromKey low endpoint of the keys in the returned map jaroslav@1258: * @param fromInclusive {@code true} if the low endpoint jaroslav@1258: * is to be included in the returned view jaroslav@1258: * @param toKey high endpoint of the keys in the returned map jaroslav@1258: * @param toInclusive {@code true} if the high endpoint jaroslav@1258: * is to be included in the returned view jaroslav@1258: * @return a view of the portion of this map whose keys range from jaroslav@1258: * {@code fromKey} to {@code toKey} jaroslav@1258: * @throws ClassCastException if {@code fromKey} and {@code toKey} jaroslav@1258: * cannot be compared to one another using this map's comparator jaroslav@1258: * (or, if the map has no comparator, using natural ordering). jaroslav@1258: * Implementations may, but are not required to, throw this jaroslav@1258: * exception if {@code fromKey} or {@code toKey} jaroslav@1258: * cannot be compared to keys currently in the map. jaroslav@1258: * @throws NullPointerException if {@code fromKey} or {@code toKey} jaroslav@1258: * is null and this map does not permit null keys jaroslav@1258: * @throws IllegalArgumentException if {@code fromKey} is greater than jaroslav@1258: * {@code toKey}; or if this map itself has a restricted jaroslav@1258: * range, and {@code fromKey} or {@code toKey} lies jaroslav@1258: * outside the bounds of the range jaroslav@1258: */ jaroslav@1258: NavigableMap subMap(K fromKey, boolean fromInclusive, jaroslav@1258: K toKey, boolean toInclusive); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a view of the portion of this map whose keys are less than (or jaroslav@1258: * equal to, if {@code inclusive} is true) {@code toKey}. The returned jaroslav@1258: * map is backed by this map, so changes in the returned map are reflected jaroslav@1258: * in this map, and vice-versa. The returned map supports all optional jaroslav@1258: * map operations that this map supports. jaroslav@1258: * jaroslav@1258: *

The returned map will throw an {@code IllegalArgumentException} jaroslav@1258: * on an attempt to insert a key outside its range. jaroslav@1258: * jaroslav@1258: * @param toKey high endpoint of the keys in the returned map jaroslav@1258: * @param inclusive {@code true} if the high endpoint jaroslav@1258: * is to be included in the returned view jaroslav@1258: * @return a view of the portion of this map whose keys are less than jaroslav@1258: * (or equal to, if {@code inclusive} is true) {@code toKey} jaroslav@1258: * @throws ClassCastException if {@code toKey} is not compatible jaroslav@1258: * with this map's comparator (or, if the map has no comparator, jaroslav@1258: * if {@code toKey} does not implement {@link Comparable}). jaroslav@1258: * Implementations may, but are not required to, throw this jaroslav@1258: * exception if {@code toKey} cannot be compared to keys jaroslav@1258: * currently in the map. jaroslav@1258: * @throws NullPointerException if {@code toKey} is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: * @throws IllegalArgumentException if this map itself has a jaroslav@1258: * restricted range, and {@code toKey} lies outside the jaroslav@1258: * bounds of the range jaroslav@1258: */ jaroslav@1258: NavigableMap headMap(K toKey, boolean inclusive); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a view of the portion of this map whose keys are greater than (or jaroslav@1258: * equal to, if {@code inclusive} is true) {@code fromKey}. The returned jaroslav@1258: * map is backed by this map, so changes in the returned map are reflected jaroslav@1258: * in this map, and vice-versa. The returned map supports all optional jaroslav@1258: * map operations that this map supports. jaroslav@1258: * jaroslav@1258: *

The returned map will throw an {@code IllegalArgumentException} jaroslav@1258: * on an attempt to insert a key outside its range. jaroslav@1258: * jaroslav@1258: * @param fromKey low endpoint of the keys in the returned map jaroslav@1258: * @param inclusive {@code true} if the low endpoint jaroslav@1258: * is to be included in the returned view jaroslav@1258: * @return a view of the portion of this map whose keys are greater than jaroslav@1258: * (or equal to, if {@code inclusive} is true) {@code fromKey} jaroslav@1258: * @throws ClassCastException if {@code fromKey} is not compatible jaroslav@1258: * with this map's comparator (or, if the map has no comparator, jaroslav@1258: * if {@code fromKey} does not implement {@link Comparable}). jaroslav@1258: * Implementations may, but are not required to, throw this jaroslav@1258: * exception if {@code fromKey} cannot be compared to keys jaroslav@1258: * currently in the map. jaroslav@1258: * @throws NullPointerException if {@code fromKey} is null jaroslav@1258: * and this map does not permit null keys jaroslav@1258: * @throws IllegalArgumentException if this map itself has a jaroslav@1258: * restricted range, and {@code fromKey} lies outside the jaroslav@1258: * bounds of the range jaroslav@1258: */ jaroslav@1258: NavigableMap tailMap(K fromKey, boolean inclusive); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * {@inheritDoc} jaroslav@1258: * jaroslav@1258: *

Equivalent to {@code subMap(fromKey, true, toKey, false)}. jaroslav@1258: * jaroslav@1258: * @throws ClassCastException {@inheritDoc} jaroslav@1258: * @throws NullPointerException {@inheritDoc} jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc} jaroslav@1258: */ jaroslav@1258: SortedMap subMap(K fromKey, K toKey); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * {@inheritDoc} jaroslav@1258: * jaroslav@1258: *

Equivalent to {@code headMap(toKey, false)}. jaroslav@1258: * jaroslav@1258: * @throws ClassCastException {@inheritDoc} jaroslav@1258: * @throws NullPointerException {@inheritDoc} jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc} jaroslav@1258: */ jaroslav@1258: SortedMap headMap(K toKey); jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * {@inheritDoc} jaroslav@1258: * jaroslav@1258: *

Equivalent to {@code tailMap(fromKey, true)}. jaroslav@1258: * jaroslav@1258: * @throws ClassCastException {@inheritDoc} jaroslav@1258: * @throws NullPointerException {@inheritDoc} jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc} jaroslav@1258: */ jaroslav@1258: SortedMap tailMap(K fromKey); jaroslav@1258: }