1. /*
  2. * @(#)SortedMap.java 1.12 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util;
  11. /**
  12. * A map that further guarantees that it will be in ascending key order,
  13. * sorted according to the <i>natural ordering</i> of its keys (see the
  14. * <tt>Comparable</tt> interface), or by a comparator provided at sorted map
  15. * creation time. This order is reflected when iterating over the sorted
  16. * map's collection views (returned by the <tt>entrySet</tt>, <tt>keySet</tt>
  17. * and <tt>values</tt> methods). Several additional operations are provided
  18. * to take advantage of the ordering. (This interface is the map analogue of
  19. * the <tt>SortedSet</tt> interface.)<p>
  20. *
  21. * All keys inserted into a sorted map must implement the <tt>Comparable</tt>
  22. * interface (or be accepted by the specified comparator). Furthermore, all
  23. * such keys must be <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> (or
  24. * <tt>comparator.compare(k1, k2)</tt>) must not throw a
  25. * <tt>ClassCastException</tt> for any elements <tt>k1</tt> and <tt>k2</tt> in
  26. * the sorted map. Attempts to violate this restriction will cause the
  27. * offending method or constructor invocation to throw a
  28. * <tt>ClassCastException</tt>.<p>
  29. *
  30. * Note that the ordering maintained by a sorted map (whether or not an
  31. * explicit comparator is provided) must be <i>consistent with equals</i> if
  32. * the sorted map is to correctly implement the <tt>Map</tt> interface. (See
  33. * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
  34. * precise definition of <i>consistent with equals</i>.) This is so because
  35. * the <tt>Map</tt> interface is defined in terms of the <tt>equals</tt>
  36. * operation, but a sorted map performs all key comparisons using its
  37. * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two keys that are
  38. * deemed equal by this method are, from the standpoint of the sorted map,
  39. * equal. The behavior of a tree map <i>is</i> well-defined even if its
  40. * ordering is inconsistent with equals; it just fails to obey the general
  41. * contract of the <tt>Map</tt> interface.<p>
  42. *
  43. * All general-purpose sorted map implementation classes should provide four
  44. * "standard" constructors: 1) A void (no arguments) constructor, which
  45. * creates an empty sorted map sorted according to the <i>natural order</i> of
  46. * its keys. 2) A constructor with a single argument of type
  47. * <tt>Comparator</tt>, which creates an empty sorted map sorted according to
  48. * the specified comparator. 3) A constructor with a single argument of type
  49. * <tt>Map</tt>, which creates a new map with the same key-value mappings as
  50. * its argument, sorted according to the keys' natural ordering. 4) A
  51. * constructor with a single argument of type sorted map, which creates a new
  52. * sorted map with the same key-value mappings and the same ordering as the
  53. * input sorted map. There is no way to enforce this recommendation (as
  54. * interfaces cannot contain constructors) but the SDK implementation
  55. * (TreeMap) complies.
  56. *
  57. * @author Josh Bloch
  58. * @version 1.12, 02/02/00
  59. * @see Map
  60. * @see TreeMap
  61. * @see SortedSet
  62. * @see Comparator
  63. * @see Comparable
  64. * @see Collection
  65. * @see ClassCastException
  66. * @since 1.2
  67. */
  68. public interface SortedMap extends Map {
  69. /**
  70. * Returns the comparator associated with this sorted map, or
  71. * <tt>null</tt> if it uses its keys' natural ordering.
  72. *
  73. * @return the comparator associated with this sorted map, or
  74. * <tt>null</tt> if it uses its keys' natural ordering.
  75. */
  76. Comparator comparator();
  77. /**
  78. * Returns a view of the portion of this sorted map whose keys range from
  79. * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If
  80. * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map
  81. * is empty.) The returned sorted map is backed by this sorted map, so
  82. * changes in the returned sorted map are reflected in this sorted map,
  83. * and vice-versa. The returned Map supports all optional map operations
  84. * that this sorted map supports.<p>
  85. *
  86. * The map returned by this method will throw an
  87. * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
  88. * outside the specified range.<p>
  89. *
  90. * Note: this method always returns a <i>half-open range</i> (which
  91. * includes its low endpoint but not its high endpoint). If you need a
  92. * <i>closed range</i> (which includes both endpoints), and the key type
  93. * allows for calculation of the successor a given key, merely request the
  94. * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
  95. * For example, suppose that <tt>m</tt> is a map whose keys are strings.
  96. * The following idiom obtains a view containing all of the key-value
  97. * mappings in <tt>m</tt> whose keys are between <tt>low</tt> and
  98. * <tt>high</tt>, inclusive:
  99. *
  100. * <pre> Map sub = m.subMap(low, high+"\0");</pre>
  101. *
  102. * A similarly technique can be used to generate an <i>open range</i>
  103. * (which contains neither endpoint). The following idiom obtains a
  104. * view containing all of the key-value mappings in <tt>m</tt> whose keys
  105. * are between <tt>low</tt> and <tt>high</tt>, exclusive:
  106. *
  107. * <pre> Map sub = m.subMap(low+"\0", high);</pre>
  108. *
  109. * @param fromKey low endpoint (inclusive) of the subMap.
  110. * @param toKey high endpoint (exclusive) of the subMap.
  111. * @return a view of the specified range within this sorted map.
  112. *
  113. * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
  114. * cannot be compared to one another using this map's comparator
  115. * (or, if the map has no comparator, using natural ordering).
  116. * Implementations may, but are not required to, throw this
  117. * exception if <tt>fromKey</tt> or <tt>toKey</tt>
  118. * cannot be compared to keys currently in the map.
  119. * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
  120. * <tt>toKey</tt> or if this map is itself a subMap, headMap,
  121. * or tailMap, and <tt>fromKey</tt> or <tt>toKey</tt> are not
  122. * within the specified range of the subMap, headMap, or tailMap.
  123. * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
  124. * <tt>null</tt> and this sorted map does not tolerate
  125. * <tt>null</tt> keys.
  126. */
  127. SortedMap subMap(Object fromKey, Object toKey);
  128. /**
  129. * Returns a view of the portion of this sorted map whose keys are
  130. * strictly less than toKey. The returned sorted map is backed by this
  131. * sorted map, so changes in the returned sorted map are reflected in this
  132. * sorted map, and vice-versa. The returned map supports all optional map
  133. * operations that this sorted map supports.<p>
  134. *
  135. * The map returned by this method will throw an IllegalArgumentException
  136. * if the user attempts to insert a key outside the specified range.<p>
  137. *
  138. * Note: this method always returns a view that does not contain its
  139. * (high) endpoint. If you need a view that does contain this endpoint,
  140. * and the key type allows for calculation of the successor a given
  141. * key, merely request a headMap bounded by successor(highEndpoint).
  142. * For example, suppose that suppose that <tt>m</tt> is a map whose keys
  143. * are strings. The following idiom obtains a view containing all of the
  144. * key-value mappings in <tt>m</tt> whose keys are less than or equal to
  145. * <tt>high</tt>:
  146. *
  147. * <pre> Map head = m.headMap(high+"\0");</pre>
  148. *
  149. * @param toKey high endpoint (exclusive) of the subMap.
  150. * @return a view of the specified initial range of this sorted map.
  151. * @throws ClassCastException if <tt>toKey</tt> is not compatible
  152. * with this map's comparator (or, if the map has no comparator,
  153. * if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
  154. * Implementations may, but are not required to, throw this
  155. * exception if <tt>toKey</tt> cannot be compared to keys
  156. * currently in the map.
  157. * @throws IllegalArgumentException if this map is itself a subMap,
  158. * headMap, or tailMap, and <tt>toKey</tt> is not within the
  159. * specified range of the subMap, headMap, or tailMap.
  160. * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
  161. * this sorted map does not tolerate <tt>null</tt> keys.
  162. */
  163. SortedMap headMap(Object toKey);
  164. /**
  165. * Returns a view of the portion of this sorted map whose keys are greater
  166. * than or equal to <tt>fromKey</tt>. The returned sorted map is backed
  167. * by this sorted map, so changes in the returned sorted map are reflected
  168. * in this sorted map, and vice-versa. The returned map supports all
  169. * optional map operations that this sorted map supports.<p>
  170. *
  171. * The map returned by this method will throw an
  172. * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
  173. * outside the specified range.<p>
  174. *
  175. * Note: this method always returns a view that contains its (low)
  176. * endpoint. If you need a view that does not contain this endpoint, and
  177. * the element type allows for calculation of the successor a given value,
  178. * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
  179. * For example, suppose that suppose that <tt>m</tt> is a map whose keys
  180. * are strings. The following idiom obtains a view containing all of the
  181. * key-value mappings in <tt>m</tt> whose keys are strictly greater than
  182. * <tt>low</tt>:
  183. *
  184. * <pre> Map tail = m.tailMap(low+"\0");</pre>
  185. *
  186. * @param fromKey low endpoint (inclusive) of the tailMap.
  187. * @return a view of the specified final range of this sorted map.
  188. * @throws ClassCastException if <tt>fromKey</tt> is not compatible
  189. * with this map's comparator (or, if the map has no comparator,
  190. * if <tt>fromKey</tt> does not implement <tt>Comparable</tt>).
  191. * Implementations may, but are not required to, throw this
  192. * exception if <tt>fromKey</tt> cannot be compared to keys
  193. * currently in the map.
  194. * @throws IllegalArgumentException if this map is itself a subMap,
  195. * headMap, or tailMap, and <tt>fromKey</tt> is not within the
  196. * specified range of the subMap, headMap, or tailMap.
  197. * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
  198. * this sorted map does not tolerate <tt>null</tt> keys.
  199. */
  200. SortedMap tailMap(Object fromKey);
  201. /**
  202. * Returns the first (lowest) key currently in this sorted map.
  203. *
  204. * @return the first (lowest) key currently in this sorted map.
  205. * @throws NoSuchElementException if this map is empty.
  206. */
  207. Object firstKey();
  208. /**
  209. * Returns the last (highest) key currently in this sorted map.
  210. *
  211. * @return the last (highest) key currently in this sorted map.
  212. * @throws NoSuchElementException if this map is empty.
  213. */
  214. Object lastKey();
  215. }