1. /*
  2. * @(#)SortedSet.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 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.
  51. *
  52. * @author Josh Bloch
  53. * @version 1.11 11/29/01
  54. * @see Set
  55. * @see TreeSet
  56. * @see SortedMap
  57. * @see Collection
  58. * @see Comparable
  59. * @see Comparator
  60. * @see java.lang.ClassCastException
  61. * @since JDK1.2
  62. */
  63. public interface SortedSet extends Set {
  64. /**
  65. * Returns the comparator associated with this sorted set, or
  66. * <tt>null</tt> if it uses its elements' natural ordering.
  67. *
  68. * @return the comparator associated with this sorted set, or
  69. * <tt>null</tt> if it uses its elements' natural ordering.
  70. */
  71. Comparator comparator();
  72. /**
  73. * Returns a view of the portion of this sorted set whose elements range
  74. * from <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, exclusive.
  75. * (If <tt>fromElement</tt> and <tt>toElement</tt> are equal, the returned
  76. * sorted set is empty.) The returned sorted set is backed by this sorted
  77. * set, so changes in the returned sorted set are reflected in this sorted
  78. * set, and vice-versa. The returned sorted set supports all optional set
  79. * operations that this sorted set supports.<p>
  80. *
  81. * The sorted set returned by this method will throw an
  82. * <tt>IllegalArgumentException</tt> if the user attempts to insert a
  83. * element outside the specified range.<p>
  84. *
  85. * Note: this method always returns a <i>half-open range</i> (which
  86. * includes its low endpoint but not its high endpoint). If you need a
  87. * <i>closed range</i> (which includes both endpoints), and the element
  88. * type allows for calculation of the successor a given value, merely
  89. * request the subrange from <tt>lowEndpoint</tt> to
  90. * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt>
  91. * is a sorted set of strings. The following idiom obtains a view
  92. * containing all of the strings in <tt>s</tt> from <tt>low</tt> to
  93. * <tt>high</tt>, inclusive: <pre>
  94. * SortedSet sub = s.subSet(low, high+"\0");
  95. * </pre>
  96. *
  97. * A similar technique can be used to generate an <i>open range</i> (which
  98. * contains neither endpoint). The following idiom obtains a view
  99. * containing all of the Strings in <tt>s</tt> from <tt>low</tt> to
  100. * <tt>high</tt>, exclusive: <pre>
  101. * SortedSet sub = s.subSet(low+"\0", high);
  102. * </pre>
  103. *
  104. * @param fromElement low endpoint (inclusive) of the subSet.
  105. * @param toElement high endpoint (exclusive) of the subSet.
  106. * @return a view of the specified range within this sorted set.
  107. *
  108. * @throws ClassCastException if <tt>fromElement</tt> or <tt>toElement</tt>
  109. * cannot be compared with the elements currently in the sorted
  110. * set. (Implementations may, but are not required to, throw
  111. * this exception under these circumstances.)
  112. * @throws NullPointerException if <tt>fromElement</tt> or
  113. * <tt>toElement</tt> is <tt>null</tt> and this sorted set does
  114. * not tolerate <tt>null</tt> elements.
  115. * @throws IllegalArgumentException if <tt>fromElement</tt> is greater
  116. * than <tt>toElement</tt>.
  117. */
  118. SortedSet subSet(Object fromElement, Object toElement);
  119. /**
  120. * Returns a view of the portion of this sorted set whose elements are
  121. * strictly less than <tt>toElement</tt>. The returned sorted set is
  122. * backed by this sorted set, so changes in the returned sorted set are
  123. * reflected in this sorted set, and vice-versa. The returned sorted set
  124. * supports all optional set operations.<p>
  125. *
  126. * The sorted set returned by this method will throw an
  127. * <tt>IllegalArgumentException</tt> if the user attempts to insert a
  128. * element outside the specified range.<p>
  129. *
  130. * Note: this method always returns a view that does not contain its
  131. * (high) endpoint. If you need a view that does contain this endpoint,
  132. * and the element type allows for calculation of the successor a given
  133. * value, merely request a headSet bounded by
  134. * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt>
  135. * is a sorted set of strings. The following idiom obtains a view
  136. * containing all of the strings in <tt>s</tt> that are less than or equal
  137. * to <tt>high</tt>:
  138. * <pre> SortedSet head = s.headSet(high+"\0");</pre>
  139. *
  140. * @param toElement high endpoint (exclusive) of the headSet.
  141. * @return a view of the specified initial range of this sorted set.
  142. *
  143. * @throws ClassCastException if <tt>toElement</tt> cannot be compared
  144. * with the elements currently in the sorted set.
  145. * (Implementations may, but are not required to, throw this
  146. * exception under these circumstances.)
  147. * @throws NullPointerException if <tt>toElement</tt> is <tt>null</tt> and
  148. * this sorted set does not tolerate <tt>null</tt> elements.
  149. */
  150. SortedSet headSet(Object toElement);
  151. /**
  152. * Returns a view of the portion of this sorted set whose elements are
  153. * greater than or equal to <tt>fromElement</tt>. The returned sorted set
  154. * is backed by this sorted set, so changes in the returned sorted set are
  155. * reflected in this sorted set, and vice-versa. The returned sorted set
  156. * supports all optional set operations.<p>
  157. *
  158. * The sorted set returned by this method will throw an
  159. * <tt>IllegalArgumentException</tt> if the user attempts to insert a
  160. * element outside the specified range.<p>
  161. *
  162. * Note: this method always returns a view that contains its (low)
  163. * endpoint. If you need a view that does not contain this endpoint, and
  164. * the element type allows for calculation of the successor a given value,
  165. * merely request a tailSet bounded by <tt>successor(lowEndpoint)</tt>.
  166. * For example, suppose that <tt>s</tt> is a sorted set of strings. The
  167. * following idiom obtains a view containing all of the strings in
  168. * <tt>s</tt> that are strictly greater than <tt>low</tt>:
  169. *
  170. * <pre> SortedSet tail = s.tailSet(low+"\0");</pre>
  171. *
  172. * @param fromElement low endpoint (inclusive) of the tailSet.
  173. * @return a view of the specified final range of this sorted set.
  174. *
  175. * @throws ClassCastException if <tt>fromElement</tt> cannot be compared
  176. * with the elements currently in the sorted set.
  177. * (Implementations may, but are not required to, throw this
  178. * exception under these circumstances.)
  179. *
  180. * @throws NullPointerException if <tt>fromElement</tt> is <tt>null</tt>
  181. * and this sorted set does not tolerate <tt>null</tt>
  182. * elements.
  183. */
  184. SortedSet tailSet(Object fromElement);
  185. /**
  186. * Returns the first (lowest) element currently in this sorted set.
  187. *
  188. * @return the first (lowest) element currently in this sorted set.
  189. * @throws NoSuchElementException sorted set is empty.
  190. */
  191. Object first();
  192. /**
  193. * Returns the last (highest) element currently in this sorted set.
  194. *
  195. * @return the last (highest) element currently in this sorted set.
  196. * @throws NoSuchElementException sorted set is empty.
  197. */
  198. Object last();
  199. }