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