PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/library/java/util/SortedMap.java

https://bitbucket.org/chancey/z
Java | 284 lines | 12 code | 11 blank | 261 comment | 0 complexity | b071a0ca06e11a00847e0fbd8c19e5bf MD5 | raw file
  1. /*
  2. * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package java.util;
  26. /**
  27. * A {@link Map} that further provides a <em>total ordering</em> on its keys.
  28. * The map is ordered according to the {@linkplain Comparable natural
  29. * ordering} of its keys, or by a {@link Comparator} typically
  30. * provided at sorted map creation time. This order is reflected when
  31. * iterating over the sorted map's collection views (returned by the
  32. * {@code entrySet}, {@code keySet} and {@code values} methods).
  33. * Several additional operations are provided to take advantage of the
  34. * ordering. (This interface is the map analogue of {@link SortedSet}.)
  35. *
  36. * <p>All keys inserted into a sorted map must implement the {@code Comparable}
  37. * interface (or be accepted by the specified comparator). Furthermore, all
  38. * such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} (or
  39. * {@code comparator.compare(k1, k2)}) must not throw a
  40. * {@code ClassCastException} for any keys {@code k1} and {@code k2} in
  41. * the sorted map. Attempts to violate this restriction will cause the
  42. * offending method or constructor invocation to throw a
  43. * {@code ClassCastException}.
  44. *
  45. * <p>Note that the ordering maintained by a sorted map (whether or not an
  46. * explicit comparator is provided) must be <em>consistent with equals</em> if
  47. * the sorted map is to correctly implement the {@code Map} interface. (See
  48. * the {@code Comparable} interface or {@code Comparator} interface for a
  49. * precise definition of <em>consistent with equals</em>.) This is so because
  50. * the {@code Map} interface is defined in terms of the {@code equals}
  51. * operation, but a sorted map performs all key comparisons using its
  52. * {@code compareTo} (or {@code compare}) method, so two keys that are
  53. * deemed equal by this method are, from the standpoint of the sorted map,
  54. * equal. The behavior of a tree map <em>is</em> well-defined even if its
  55. * ordering is inconsistent with equals; it just fails to obey the general
  56. * contract of the {@code Map} interface.
  57. *
  58. * <p>All general-purpose sorted map implementation classes should provide four
  59. * "standard" constructors. It is not possible to enforce this recommendation
  60. * though as required constructors cannot be specified by interfaces. The
  61. * expected "standard" constructors for all sorted map implementations are:
  62. * <ol>
  63. * <li>A void (no arguments) constructor, which creates an empty sorted map
  64. * sorted according to the natural ordering of its keys.</li>
  65. * <li>A constructor with a single argument of type {@code Comparator}, which
  66. * creates an empty sorted map sorted according to the specified comparator.</li>
  67. * <li>A constructor with a single argument of type {@code Map}, which creates
  68. * a new map with the same key-value mappings as its argument, sorted
  69. * according to the keys' natural ordering.</li>
  70. * <li>A constructor with a single argument of type {@code SortedMap}, which
  71. * creates a new sorted map with the same key-value mappings and the same
  72. * ordering as the input sorted map.</li>
  73. * </ol>
  74. *
  75. * <p><strong>Note</strong>: several methods return submaps with restricted key
  76. * ranges. Such ranges are <em>half-open</em>, that is, they include their low
  77. * endpoint but not their high endpoint (where applicable). If you need a
  78. * <em>closed range</em> (which includes both endpoints), and the key type
  79. * allows for calculation of the successor of a given key, merely request
  80. * the subrange from {@code lowEndpoint} to
  81. * {@code successor(highEndpoint)}. For example, suppose that {@code m}
  82. * is a map whose keys are strings. The following idiom obtains a view
  83. * containing all of the key-value mappings in {@code m} whose keys are
  84. * between {@code low} and {@code high}, inclusive:<pre>
  85. * SortedMap&lt;String, V&gt; sub = m.subMap(low, high+"\0");</pre>
  86. *
  87. * A similar technique can be used to generate an <em>open range</em>
  88. * (which contains neither endpoint). The following idiom obtains a
  89. * view containing all of the key-value mappings in {@code m} whose keys
  90. * are between {@code low} and {@code high}, exclusive:<pre>
  91. * SortedMap&lt;String, V&gt; sub = m.subMap(low+"\0", high);</pre>
  92. *
  93. * <p>This interface is a member of the
  94. * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  95. * Java Collections Framework</a>.
  96. *
  97. * @param <K> the type of keys maintained by this map
  98. * @param <V> the type of mapped values
  99. *
  100. * @author Josh Bloch
  101. * @see Map
  102. * @see TreeMap
  103. * @see SortedSet
  104. * @see Comparator
  105. * @see Comparable
  106. * @see Collection
  107. * @see ClassCastException
  108. * @since 1.2
  109. */
  110. public interface SortedMap<K,V> extends Map<K,V> {
  111. /**
  112. * Returns the comparator used to order the keys in this map, or
  113. * {@code null} if this map uses the {@linkplain Comparable
  114. * natural ordering} of its keys.
  115. *
  116. * @return the comparator used to order the keys in this map,
  117. * or {@code null} if this map uses the natural ordering
  118. * of its keys
  119. */
  120. Comparator<? super K> comparator();
  121. /**
  122. * Returns a view of the portion of this map whose keys range from
  123. * {@code fromKey}, inclusive, to {@code toKey}, exclusive. (If
  124. * {@code fromKey} and {@code toKey} are equal, the returned map
  125. * is empty.) The returned map is backed by this map, so changes
  126. * in the returned map are reflected in this map, and vice-versa.
  127. * The returned map supports all optional map operations that this
  128. * map supports.
  129. *
  130. * <p>The returned map will throw an {@code IllegalArgumentException}
  131. * on an attempt to insert a key outside its range.
  132. *
  133. * @param fromKey low endpoint (inclusive) of the keys in the returned map
  134. * @param toKey high endpoint (exclusive) of the keys in the returned map
  135. * @return a view of the portion of this map whose keys range from
  136. * {@code fromKey}, inclusive, to {@code toKey}, exclusive
  137. * @throws ClassCastException if {@code fromKey} and {@code toKey}
  138. * cannot be compared to one another using this map's comparator
  139. * (or, if the map has no comparator, using natural ordering).
  140. * Implementations may, but are not required to, throw this
  141. * exception if {@code fromKey} or {@code toKey}
  142. * cannot be compared to keys currently in the map.
  143. * @throws NullPointerException if {@code fromKey} or {@code toKey}
  144. * is null and this map does not permit null keys
  145. * @throws IllegalArgumentException if {@code fromKey} is greater than
  146. * {@code toKey}; or if this map itself has a restricted
  147. * range, and {@code fromKey} or {@code toKey} lies
  148. * outside the bounds of the range
  149. */
  150. SortedMap<K,V> subMap(K fromKey, K toKey);
  151. /**
  152. * Returns a view of the portion of this map whose keys are
  153. * strictly less than {@code toKey}. The returned map is backed
  154. * by this map, so changes in the returned map are reflected in
  155. * this map, and vice-versa. The returned map supports all
  156. * optional map operations that this map supports.
  157. *
  158. * <p>The returned map will throw an {@code IllegalArgumentException}
  159. * on an attempt to insert a key outside its range.
  160. *
  161. * @param toKey high endpoint (exclusive) of the keys in the returned map
  162. * @return a view of the portion of this map whose keys are strictly
  163. * less than {@code toKey}
  164. * @throws ClassCastException if {@code toKey} is not compatible
  165. * with this map's comparator (or, if the map has no comparator,
  166. * if {@code toKey} does not implement {@link Comparable}).
  167. * Implementations may, but are not required to, throw this
  168. * exception if {@code toKey} cannot be compared to keys
  169. * currently in the map.
  170. * @throws NullPointerException if {@code toKey} is null and
  171. * this map does not permit null keys
  172. * @throws IllegalArgumentException if this map itself has a
  173. * restricted range, and {@code toKey} lies outside the
  174. * bounds of the range
  175. */
  176. SortedMap<K,V> headMap(K toKey);
  177. /**
  178. * Returns a view of the portion of this map whose keys are
  179. * greater than or equal to {@code fromKey}. The returned map is
  180. * backed by this map, so changes in the returned map are
  181. * reflected in this map, and vice-versa. The returned map
  182. * supports all optional map operations that this map supports.
  183. *
  184. * <p>The returned map will throw an {@code IllegalArgumentException}
  185. * on an attempt to insert a key outside its range.
  186. *
  187. * @param fromKey low endpoint (inclusive) of the keys in the returned map
  188. * @return a view of the portion of this map whose keys are greater
  189. * than or equal to {@code fromKey}
  190. * @throws ClassCastException if {@code fromKey} is not compatible
  191. * with this map's comparator (or, if the map has no comparator,
  192. * if {@code fromKey} does not implement {@link Comparable}).
  193. * Implementations may, but are not required to, throw this
  194. * exception if {@code fromKey} cannot be compared to keys
  195. * currently in the map.
  196. * @throws NullPointerException if {@code fromKey} is null and
  197. * this map does not permit null keys
  198. * @throws IllegalArgumentException if this map itself has a
  199. * restricted range, and {@code fromKey} lies outside the
  200. * bounds of the range
  201. */
  202. SortedMap<K,V> tailMap(K fromKey);
  203. /**
  204. * Returns the first (lowest) key currently in this map.
  205. *
  206. * @return the first (lowest) key currently in this map
  207. * @throws NoSuchElementException if this map is empty
  208. */
  209. K firstKey();
  210. /**
  211. * Returns the last (highest) key currently in this map.
  212. *
  213. * @return the last (highest) key currently in this map
  214. * @throws NoSuchElementException if this map is empty
  215. */
  216. K lastKey();
  217. /**
  218. * Returns a {@link Set} view of the keys contained in this map.
  219. * The set's iterator returns the keys in ascending order.
  220. * The set is backed by the map, so changes to the map are
  221. * reflected in the set, and vice-versa. If the map is modified
  222. * while an iteration over the set is in progress (except through
  223. * the iterator's own {@code remove} operation), the results of
  224. * the iteration are undefined. The set supports element removal,
  225. * which removes the corresponding mapping from the map, via the
  226. * {@code Iterator.remove}, {@code Set.remove},
  227. * {@code removeAll}, {@code retainAll}, and {@code clear}
  228. * operations. It does not support the {@code add} or {@code addAll}
  229. * operations.
  230. *
  231. * @return a set view of the keys contained in this map, sorted in
  232. * ascending order
  233. */
  234. Set<K> keySet();
  235. /**
  236. * Returns a {@link Collection} view of the values contained in this map.
  237. * The collection's iterator returns the values in ascending order
  238. * of the corresponding keys.
  239. * The collection is backed by the map, so changes to the map are
  240. * reflected in the collection, and vice-versa. If the map is
  241. * modified while an iteration over the collection is in progress
  242. * (except through the iterator's own {@code remove} operation),
  243. * the results of the iteration are undefined. The collection
  244. * supports element removal, which removes the corresponding
  245. * mapping from the map, via the {@code Iterator.remove},
  246. * {@code Collection.remove}, {@code removeAll},
  247. * {@code retainAll} and {@code clear} operations. It does not
  248. * support the {@code add} or {@code addAll} operations.
  249. *
  250. * @return a collection view of the values contained in this map,
  251. * sorted in ascending key order
  252. */
  253. Collection<V> values();
  254. /**
  255. * Returns a {@link Set} view of the mappings contained in this map.
  256. * The set's iterator returns the entries in ascending key order.
  257. * The set is backed by the map, so changes to the map are
  258. * reflected in the set, and vice-versa. If the map is modified
  259. * while an iteration over the set is in progress (except through
  260. * the iterator's own {@code remove} operation, or through the
  261. * {@code setValue} operation on a map entry returned by the
  262. * iterator) the results of the iteration are undefined. The set
  263. * supports element removal, which removes the corresponding
  264. * mapping from the map, via the {@code Iterator.remove},
  265. * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
  266. * {@code clear} operations. It does not support the
  267. * {@code add} or {@code addAll} operations.
  268. *
  269. * @return a set view of the mappings contained in this map,
  270. * sorted in ascending key order
  271. */
  272. Set<Map.Entry<K, V>> entrySet();
  273. }