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