/android/upstream/java/util/NavigableSet.java

https://bitbucket.org/festevezga/xobotos · Java · 291 lines · 19 code · 17 blank · 255 comment · 0 complexity · c4b93ad5e9b538e1638e25070f615309 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/licenses/publicdomain
  5. */
  6. package java.util;
  7. // BEGIN android-note
  8. // removed link to collections framework docs
  9. // changed {@link #subSet(Object)} to {@link #subSet} to satisfy DroidDoc
  10. // END android-note
  11. /**
  12. * A {@link SortedSet} extended with navigation methods reporting
  13. * closest matches for given search targets. Methods {@code lower},
  14. * {@code floor}, {@code ceiling}, and {@code higher} return elements
  15. * respectively less than, less than or equal, greater than or equal,
  16. * and greater than a given element, returning {@code null} if there
  17. * is no such element. A {@code NavigableSet} may be accessed and
  18. * traversed in either ascending or descending order. The {@code
  19. * descendingSet} method returns a view of the set with the senses of
  20. * all relational and directional methods inverted. The performance of
  21. * ascending operations and views is likely to be faster than that of
  22. * descending ones. This interface additionally defines methods
  23. * {@code pollFirst} and {@code pollLast} that return and remove the
  24. * lowest and highest element, if one exists, else returning {@code
  25. * null}. Methods {@code subSet}, {@code headSet},
  26. * and {@code tailSet} differ from the like-named {@code
  27. * SortedSet} methods in accepting additional arguments describing
  28. * whether lower and upper bounds are inclusive versus exclusive.
  29. * Subsets of any {@code NavigableSet} must implement the {@code
  30. * NavigableSet} interface.
  31. *
  32. * <p> The return values of navigation methods may be ambiguous in
  33. * implementations that permit {@code null} elements. However, even
  34. * in this case the result can be disambiguated by checking
  35. * {@code contains(null)}. To avoid such issues, implementations of
  36. * this interface are encouraged to <em>not</em> permit insertion of
  37. * {@code null} elements. (Note that sorted sets of {@link
  38. * Comparable} elements intrinsically do not permit {@code null}.)
  39. *
  40. * <p>Methods
  41. * {@link #subSet subSet(E, E)},
  42. * {@link #headSet headSet(E)}, and
  43. * {@link #tailSet tailSet(E)}
  44. * are specified to return {@code SortedSet} to allow existing
  45. * implementations of {@code SortedSet} to be compatibly retrofitted to
  46. * implement {@code NavigableSet}, but extensions and implementations
  47. * of this interface are encouraged to override these methods to return
  48. * {@code NavigableSet}.
  49. *
  50. * @author Doug Lea
  51. * @author Josh Bloch
  52. * @param <E> the type of elements maintained by this set
  53. * @since 1.6
  54. */
  55. public interface NavigableSet<E> extends SortedSet<E> {
  56. /**
  57. * Returns the greatest element in this set strictly less than the
  58. * given element, or {@code null} if there is no such element.
  59. *
  60. * @param e the value to match
  61. * @return the greatest element less than {@code e},
  62. * or {@code null} if there is no such element
  63. * @throws ClassCastException if the specified element cannot be
  64. * compared with the elements currently in the set
  65. * @throws NullPointerException if the specified element is null
  66. * and this set does not permit null elements
  67. */
  68. E lower(E e);
  69. /**
  70. * Returns the greatest element in this set less than or equal to
  71. * the given element, or {@code null} if there is no such element.
  72. *
  73. * @param e the value to match
  74. * @return the greatest element less than or equal to {@code e},
  75. * or {@code null} if there is no such element
  76. * @throws ClassCastException if the specified element cannot be
  77. * compared with the elements currently in the set
  78. * @throws NullPointerException if the specified element is null
  79. * and this set does not permit null elements
  80. */
  81. E floor(E e);
  82. /**
  83. * Returns the least element in this set greater than or equal to
  84. * the given element, or {@code null} if there is no such element.
  85. *
  86. * @param e the value to match
  87. * @return the least element greater than or equal to {@code e},
  88. * or {@code null} if there is no such element
  89. * @throws ClassCastException if the specified element cannot be
  90. * compared with the elements currently in the set
  91. * @throws NullPointerException if the specified element is null
  92. * and this set does not permit null elements
  93. */
  94. E ceiling(E e);
  95. /**
  96. * Returns the least element in this set strictly greater than the
  97. * given element, or {@code null} if there is no such element.
  98. *
  99. * @param e the value to match
  100. * @return the least element greater than {@code e},
  101. * or {@code null} if there is no such element
  102. * @throws ClassCastException if the specified element cannot be
  103. * compared with the elements currently in the set
  104. * @throws NullPointerException if the specified element is null
  105. * and this set does not permit null elements
  106. */
  107. E higher(E e);
  108. /**
  109. * Retrieves and removes the first (lowest) element,
  110. * or returns {@code null} if this set is empty.
  111. *
  112. * @return the first element, or {@code null} if this set is empty
  113. */
  114. E pollFirst();
  115. /**
  116. * Retrieves and removes the last (highest) element,
  117. * or returns {@code null} if this set is empty.
  118. *
  119. * @return the last element, or {@code null} if this set is empty
  120. */
  121. E pollLast();
  122. /**
  123. * Returns an iterator over the elements in this set, in ascending order.
  124. *
  125. * @return an iterator over the elements in this set, in ascending order
  126. */
  127. Iterator<E> iterator();
  128. /**
  129. * Returns a reverse order view of the elements contained in this set.
  130. * The descending set is backed by this set, so changes to the set are
  131. * reflected in the descending set, and vice-versa. If either set is
  132. * modified while an iteration over either set is in progress (except
  133. * through the iterator's own {@code remove} operation), the results of
  134. * the iteration are undefined.
  135. *
  136. * <p>The returned set has an ordering equivalent to
  137. * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
  138. * The expression {@code s.descendingSet().descendingSet()} returns a
  139. * view of {@code s} essentially equivalent to {@code s}.
  140. *
  141. * @return a reverse order view of this set
  142. */
  143. NavigableSet<E> descendingSet();
  144. /**
  145. * Returns an iterator over the elements in this set, in descending order.
  146. * Equivalent in effect to {@code descendingSet().iterator()}.
  147. *
  148. * @return an iterator over the elements in this set, in descending order
  149. */
  150. Iterator<E> descendingIterator();
  151. /**
  152. * Returns a view of the portion of this set whose elements range from
  153. * {@code fromElement} to {@code toElement}. If {@code fromElement} and
  154. * {@code toElement} are equal, the returned set is empty unless {@code
  155. * fromExclusive} and {@code toExclusive} are both true. The returned set
  156. * is backed by this set, so changes in the returned set are reflected in
  157. * this set, and vice-versa. The returned set supports all optional set
  158. * operations that this set supports.
  159. *
  160. * <p>The returned set will throw an {@code IllegalArgumentException}
  161. * on an attempt to insert an element outside its range.
  162. *
  163. * @param fromElement low endpoint of the returned set
  164. * @param fromInclusive {@code true} if the low endpoint
  165. * is to be included in the returned view
  166. * @param toElement high endpoint of the returned set
  167. * @param toInclusive {@code true} if the high endpoint
  168. * is to be included in the returned view
  169. * @return a view of the portion of this set whose elements range from
  170. * {@code fromElement}, inclusive, to {@code toElement}, exclusive
  171. * @throws ClassCastException if {@code fromElement} and
  172. * {@code toElement} cannot be compared to one another using this
  173. * set's comparator (or, if the set has no comparator, using
  174. * natural ordering). Implementations may, but are not required
  175. * to, throw this exception if {@code fromElement} or
  176. * {@code toElement} cannot be compared to elements currently in
  177. * the set.
  178. * @throws NullPointerException if {@code fromElement} or
  179. * {@code toElement} is null and this set does
  180. * not permit null elements
  181. * @throws IllegalArgumentException if {@code fromElement} is
  182. * greater than {@code toElement}; or if this set itself
  183. * has a restricted range, and {@code fromElement} or
  184. * {@code toElement} lies outside the bounds of the range.
  185. */
  186. NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
  187. E toElement, boolean toInclusive);
  188. /**
  189. * Returns a view of the portion of this set whose elements are less than
  190. * (or equal to, if {@code inclusive} is true) {@code toElement}. The
  191. * returned set is backed by this set, so changes in the returned set are
  192. * reflected in this set, and vice-versa. The returned set supports all
  193. * optional set operations that this set supports.
  194. *
  195. * <p>The returned set will throw an {@code IllegalArgumentException}
  196. * on an attempt to insert an element outside its range.
  197. *
  198. * @param toElement high endpoint of the returned set
  199. * @param inclusive {@code true} if the high endpoint
  200. * is to be included in the returned view
  201. * @return a view of the portion of this set whose elements are less than
  202. * (or equal to, if {@code inclusive} is true) {@code toElement}
  203. * @throws ClassCastException if {@code toElement} is not compatible
  204. * with this set's comparator (or, if the set has no comparator,
  205. * if {@code toElement} does not implement {@link Comparable}).
  206. * Implementations may, but are not required to, throw this
  207. * exception if {@code toElement} cannot be compared to elements
  208. * currently in the set.
  209. * @throws NullPointerException if {@code toElement} is null and
  210. * this set does not permit null elements
  211. * @throws IllegalArgumentException if this set itself has a
  212. * restricted range, and {@code toElement} lies outside the
  213. * bounds of the range
  214. */
  215. NavigableSet<E> headSet(E toElement, boolean inclusive);
  216. /**
  217. * Returns a view of the portion of this set whose elements are greater
  218. * than (or equal to, if {@code inclusive} is true) {@code fromElement}.
  219. * The returned set is backed by this set, so changes in the returned set
  220. * are reflected in this set, and vice-versa. The returned set supports
  221. * all optional set operations that this set supports.
  222. *
  223. * <p>The returned set will throw an {@code IllegalArgumentException}
  224. * on an attempt to insert an element outside its range.
  225. *
  226. * @param fromElement low endpoint of the returned set
  227. * @param inclusive {@code true} if the low endpoint
  228. * is to be included in the returned view
  229. * @return a view of the portion of this set whose elements are greater
  230. * than or equal to {@code fromElement}
  231. * @throws ClassCastException if {@code fromElement} is not compatible
  232. * with this set's comparator (or, if the set has no comparator,
  233. * if {@code fromElement} does not implement {@link Comparable}).
  234. * Implementations may, but are not required to, throw this
  235. * exception if {@code fromElement} cannot be compared to elements
  236. * currently in the set.
  237. * @throws NullPointerException if {@code fromElement} is null
  238. * and this set does not permit null elements
  239. * @throws IllegalArgumentException if this set itself has a
  240. * restricted range, and {@code fromElement} lies outside the
  241. * bounds of the range
  242. */
  243. NavigableSet<E> tailSet(E fromElement, boolean inclusive);
  244. /**
  245. * {@inheritDoc}
  246. *
  247. * <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.
  248. *
  249. * @throws ClassCastException {@inheritDoc}
  250. * @throws NullPointerException {@inheritDoc}
  251. * @throws IllegalArgumentException {@inheritDoc}
  252. */
  253. SortedSet<E> subSet(E fromElement, E toElement);
  254. /**
  255. * {@inheritDoc}
  256. *
  257. * <p>Equivalent to {@code headSet(toElement, false)}.
  258. *
  259. * @throws ClassCastException {@inheritDoc}
  260. * @throws NullPointerException {@inheritDoc}
  261. * @throws IllegalArgumentException {@inheritDoc}
  262. na */
  263. SortedSet<E> headSet(E toElement);
  264. /**
  265. * {@inheritDoc}
  266. *
  267. * <p>Equivalent to {@code tailSet(fromElement, true)}.
  268. *
  269. * @throws ClassCastException {@inheritDoc}
  270. * @throws NullPointerException {@inheritDoc}
  271. * @throws IllegalArgumentException {@inheritDoc}
  272. */
  273. SortedSet<E> tailSet(E fromElement);
  274. }