jaroslav@597: /* jaroslav@597: * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * A {@link Map} that further provides a total ordering on its keys. jaroslav@597: * The map is ordered according to the {@linkplain Comparable natural jaroslav@597: * ordering} of its keys, or by a {@link Comparator} typically jaroslav@597: * provided at sorted map creation time. This order is reflected when jaroslav@597: * iterating over the sorted map's collection views (returned by the jaroslav@597: * {@code entrySet}, {@code keySet} and {@code values} methods). jaroslav@597: * Several additional operations are provided to take advantage of the jaroslav@597: * ordering. (This interface is the map analogue of {@link SortedSet}.) jaroslav@597: * jaroslav@597: *
All keys inserted into a sorted map must implement the {@code Comparable} jaroslav@597: * interface (or be accepted by the specified comparator). Furthermore, all jaroslav@597: * such keys must be mutually comparable: {@code k1.compareTo(k2)} (or jaroslav@597: * {@code comparator.compare(k1, k2)}) must not throw a jaroslav@597: * {@code ClassCastException} for any keys {@code k1} and {@code k2} in jaroslav@597: * the sorted map. Attempts to violate this restriction will cause the jaroslav@597: * offending method or constructor invocation to throw a jaroslav@597: * {@code ClassCastException}. jaroslav@597: * jaroslav@597: *
Note that the ordering maintained by a sorted map (whether or not an jaroslav@597: * explicit comparator is provided) must be consistent with equals if jaroslav@597: * the sorted map is to correctly implement the {@code Map} interface. (See jaroslav@597: * the {@code Comparable} interface or {@code Comparator} interface for a jaroslav@597: * precise definition of consistent with equals.) This is so because jaroslav@597: * the {@code Map} interface is defined in terms of the {@code equals} jaroslav@597: * operation, but a sorted map performs all key comparisons using its jaroslav@597: * {@code compareTo} (or {@code compare}) method, so two keys that are jaroslav@597: * deemed equal by this method are, from the standpoint of the sorted map, jaroslav@597: * equal. The behavior of a tree map is well-defined even if its jaroslav@597: * ordering is inconsistent with equals; it just fails to obey the general jaroslav@597: * contract of the {@code Map} interface. jaroslav@597: * jaroslav@597: *
All general-purpose sorted map implementation classes should provide four jaroslav@597: * "standard" constructors. It is not possible to enforce this recommendation jaroslav@597: * though as required constructors cannot be specified by interfaces. The jaroslav@597: * expected "standard" constructors for all sorted map implementations are: jaroslav@597: *
Note: several methods return submaps with restricted key jaroslav@597: * ranges. Such ranges are half-open, that is, they include their low jaroslav@597: * endpoint but not their high endpoint (where applicable). If you need a jaroslav@597: * closed range (which includes both endpoints), and the key type jaroslav@597: * allows for calculation of the successor of a given key, merely request jaroslav@597: * the subrange from {@code lowEndpoint} to jaroslav@597: * {@code successor(highEndpoint)}. For example, suppose that {@code m} jaroslav@597: * is a map whose keys are strings. The following idiom obtains a view jaroslav@597: * containing all of the key-value mappings in {@code m} whose keys are jaroslav@597: * between {@code low} and {@code high}, inclusive:
jaroslav@597: * SortedMap<String, V> sub = m.subMap(low, high+"\0");jaroslav@597: * jaroslav@597: * A similar technique can be used to generate an open range jaroslav@597: * (which contains neither endpoint). The following idiom obtains a jaroslav@597: * view containing all of the key-value mappings in {@code m} whose keys jaroslav@597: * are between {@code low} and {@code high}, exclusive:
jaroslav@597: * SortedMap<String, V> sub = m.subMap(low+"\0", high);jaroslav@597: * jaroslav@597: *
This interface is a member of the
jaroslav@597: *
jaroslav@597: * Java Collections Framework.
jaroslav@597: *
jaroslav@597: * @param The returned map will throw an {@code IllegalArgumentException}
jaroslav@597: * on an attempt to insert a key outside its range.
jaroslav@597: *
jaroslav@597: * @param fromKey low endpoint (inclusive) of the keys in the returned map
jaroslav@597: * @param toKey high endpoint (exclusive) of the keys in the returned map
jaroslav@597: * @return a view of the portion of this map whose keys range from
jaroslav@597: * {@code fromKey}, inclusive, to {@code toKey}, exclusive
jaroslav@597: * @throws ClassCastException if {@code fromKey} and {@code toKey}
jaroslav@597: * cannot be compared to one another using this map's comparator
jaroslav@597: * (or, if the map has no comparator, using natural ordering).
jaroslav@597: * Implementations may, but are not required to, throw this
jaroslav@597: * exception if {@code fromKey} or {@code toKey}
jaroslav@597: * cannot be compared to keys currently in the map.
jaroslav@597: * @throws NullPointerException if {@code fromKey} or {@code toKey}
jaroslav@597: * is null and this map does not permit null keys
jaroslav@597: * @throws IllegalArgumentException if {@code fromKey} is greater than
jaroslav@597: * {@code toKey}; or if this map itself has a restricted
jaroslav@597: * range, and {@code fromKey} or {@code toKey} lies
jaroslav@597: * outside the bounds of the range
jaroslav@597: */
jaroslav@597: SortedMap The returned map will throw an {@code IllegalArgumentException}
jaroslav@597: * on an attempt to insert a key outside its range.
jaroslav@597: *
jaroslav@597: * @param toKey high endpoint (exclusive) of the keys in the returned map
jaroslav@597: * @return a view of the portion of this map whose keys are strictly
jaroslav@597: * less than {@code toKey}
jaroslav@597: * @throws ClassCastException if {@code toKey} is not compatible
jaroslav@597: * with this map's comparator (or, if the map has no comparator,
jaroslav@597: * if {@code toKey} does not implement {@link Comparable}).
jaroslav@597: * Implementations may, but are not required to, throw this
jaroslav@597: * exception if {@code toKey} cannot be compared to keys
jaroslav@597: * currently in the map.
jaroslav@597: * @throws NullPointerException if {@code toKey} is null and
jaroslav@597: * this map does not permit null keys
jaroslav@597: * @throws IllegalArgumentException if this map itself has a
jaroslav@597: * restricted range, and {@code toKey} lies outside the
jaroslav@597: * bounds of the range
jaroslav@597: */
jaroslav@597: SortedMap The returned map will throw an {@code IllegalArgumentException}
jaroslav@597: * on an attempt to insert a key outside its range.
jaroslav@597: *
jaroslav@597: * @param fromKey low endpoint (inclusive) of the keys in the returned map
jaroslav@597: * @return a view of the portion of this map whose keys are greater
jaroslav@597: * than or equal to {@code fromKey}
jaroslav@597: * @throws ClassCastException if {@code fromKey} is not compatible
jaroslav@597: * with this map's comparator (or, if the map has no comparator,
jaroslav@597: * if {@code fromKey} does not implement {@link Comparable}).
jaroslav@597: * Implementations may, but are not required to, throw this
jaroslav@597: * exception if {@code fromKey} cannot be compared to keys
jaroslav@597: * currently in the map.
jaroslav@597: * @throws NullPointerException if {@code fromKey} is null and
jaroslav@597: * this map does not permit null keys
jaroslav@597: * @throws IllegalArgumentException if this map itself has a
jaroslav@597: * restricted range, and {@code fromKey} lies outside the
jaroslav@597: * bounds of the range
jaroslav@597: */
jaroslav@597: SortedMap