SortedSet.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SortedSet.java -- A set that makes guarantees about the order of its
  2. elements
  3. Copyright (C) 1998, 2001, 2004, 2005 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. /**
  34. * A set which guarantees its iteration order. The elements in the set
  35. * are related by the <i>natural ordering</i> if they are Comparable, or
  36. * by the provided Comparator. Additional operations take advantage of
  37. * the sorted nature of the set.
  38. * <p>
  39. *
  40. * All elements entered in the set must be mutually comparable; in other words,
  41. * <code>k1.compareTo(k2)</code> or <code>comparator.compare(k1, k2)</code>
  42. * must not throw a ClassCastException. The ordering must be <i>consistent
  43. * with equals</i> (see {@link Comparator} for this definition), if the
  44. * set is to obey the general contract of the Set interface. If not,
  45. * the results are well-defined, but probably not what you wanted.
  46. * <p>
  47. *
  48. * It is recommended that all implementing classes provide four constructors:
  49. * 1) one that takes no arguments and builds an empty set sorted by natural
  50. * order of the elements; 2) one that takes a Comparator for the sorting order;
  51. * 3) one that takes a Set and sorts according to the natural order of its
  52. * elements; and 4) one that takes a SortedSet and sorts by the same
  53. * comparator. Unfortunately, the Java language does not provide a way to
  54. * enforce this.
  55. *
  56. * @author Original author unknown
  57. * @author Eric Blake (ebb9@email.byu.edu)
  58. * @see Set
  59. * @see TreeSet
  60. * @see SortedMap
  61. * @see Collection
  62. * @see Comparable
  63. * @see Comparator
  64. * @see ClassCastException
  65. * @since 1.2
  66. * @status updated to 1.4
  67. */
  68. public interface SortedSet<E> extends Set<E>
  69. {
  70. /**
  71. * Returns the comparator used in sorting this set, or null if it is
  72. * the elements' natural ordering.
  73. *
  74. * @return the sorting comparator
  75. */
  76. Comparator<? super E> comparator();
  77. /**
  78. * Returns the first (lowest sorted) element in the set.
  79. *
  80. * @return the first element
  81. * @throws NoSuchElementException if the set is empty.
  82. */
  83. E first();
  84. /**
  85. * Returns a view of the portion of the set strictly less than toElement. The
  86. * view is backed by this set, so changes in one show up in the other.
  87. * The subset supports all optional operations of the original.
  88. * <p>
  89. *
  90. * The returned set throws an IllegalArgumentException any time an element is
  91. * used which is out of the range of toElement. Note that the endpoint, toElement,
  92. * is not included; if you want this value included, pass its successor object in to
  93. * toElement. For example, for Integers, you could request
  94. * <code>headSet(new Integer(limit.intValue() + 1))</code>.
  95. *
  96. * @param toElement the exclusive upper range of the subset
  97. * @return the subset
  98. * @throws ClassCastException if toElement is not comparable to the set
  99. * contents
  100. * @throws IllegalArgumentException if this is a subSet, and toElement is out
  101. * of range
  102. * @throws NullPointerException if toElement is null but the set does not
  103. * allow null elements
  104. */
  105. SortedSet<E> headSet(E toElement);
  106. /**
  107. * Returns the last (highest sorted) element in the set.
  108. *
  109. * @return the last element
  110. * @throws NoSuchElementException if the set is empty.
  111. */
  112. E last();
  113. /**
  114. * Returns a view of the portion of the set greater than or equal to
  115. * fromElement, and strictly less than toElement. The view is backed by
  116. * this set, so changes in one show up in the other. The subset supports all
  117. * optional operations of the original.
  118. * <p>
  119. *
  120. * The returned set throws an IllegalArgumentException any time an element is
  121. * used which is out of the range of fromElement and toElement. Note that the
  122. * lower endpoint is included, but the upper is not; if you want to
  123. * change the inclusion or exclusion of an endpoint, pass its successor
  124. * object in instead. For example, for Integers, you can request
  125. * <code>subSet(new Integer(lowlimit.intValue() + 1),
  126. * new Integer(highlimit.intValue() + 1))</code> to reverse
  127. * the inclusiveness of both endpoints.
  128. *
  129. * @param fromElement the inclusive lower range of the subset
  130. * @param toElement the exclusive upper range of the subset
  131. * @return the subset
  132. * @throws ClassCastException if fromElement or toElement is not comparable
  133. * to the set contents
  134. * @throws IllegalArgumentException if this is a subSet, and fromElement or
  135. * toElement is out of range
  136. * @throws NullPointerException if fromElement or toElement is null but the
  137. * set does not allow null elements
  138. */
  139. SortedSet<E> subSet(E fromElement, E toElement);
  140. /**
  141. * Returns a view of the portion of the set greater than or equal to
  142. * fromElement. The view is backed by this set, so changes in one show up
  143. * in the other. The subset supports all optional operations of the original.
  144. * <p>
  145. *
  146. * The returned set throws an IllegalArgumentException any time an element is
  147. * used which is out of the range of fromElement. Note that the endpoint,
  148. * fromElement, is included; if you do not want this value to be included, pass its
  149. * successor object in to fromElement. For example, for Integers, you could request
  150. * <code>tailSet(new Integer(limit.intValue() + 1))</code>.
  151. *
  152. * @param fromElement the inclusive lower range of the subset
  153. * @return the subset
  154. * @throws ClassCastException if fromElement is not comparable to the set
  155. * contents
  156. * @throws IllegalArgumentException if this is a subSet, and fromElement is
  157. * out of range
  158. * @throws NullPointerException if fromElement is null but the set does not
  159. * allow null elements
  160. */
  161. SortedSet<E> tailSet(E fromElement);
  162. }