/luni/src/main/java/java/util/NavigableMap.java

https://bitbucket.org/aways/android_libcore · Java · 395 lines · 25 code · 23 blank · 347 comment · 0 complexity · 321b0bc7c3dcd55ec053c21b21a8486e MD5 · raw file

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