SortedSet.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* SortedSet.java -- A set that makes guarantees about the order of its
  2. elements
  3. Copyright (C) 1998, 2001 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., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 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. * map 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 extends Set
  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 comparator();
  77. /**
  78. * Returns the first (lowest sorted) element in the map.
  79. *
  80. * @return the first element
  81. */
  82. Object first();
  83. /**
  84. * Returns a view of the portion of the set strictly less than toElement. The
  85. * view is backed by this set, so changes in one show up in the other.
  86. * The subset supports all optional operations of the original.
  87. * <p>
  88. *
  89. * The returned set throws an IllegalArgumentException any time an element is
  90. * used which is out of the range of toElement. Note that the endpoint is not
  91. * included; if you want the endpoint, pass the successor object in to
  92. * toElement. For example, for Strings, you can request
  93. * <code>headSet(limit + "\0")</code>.
  94. *
  95. * @param toElement the exclusive upper range of the subset
  96. * @return the subset
  97. * @throws ClassCastException if toElement is not comparable to the set
  98. * contents
  99. * @throws IllegalArgumentException if this is a subSet, and toElement is out
  100. * of range
  101. * @throws NullPointerException if toElement is null but the map does not
  102. * allow null elements
  103. */
  104. SortedSet headSet(Object toElement);
  105. /**
  106. * Returns the last (highest sorted) element in the map.
  107. *
  108. * @return the last element
  109. */
  110. Object last();
  111. /**
  112. * Returns a view of the portion of the set greater than or equal to
  113. * fromElement, and strictly less than toElement. The view is backed by
  114. * this set, so changes in one show up in the other. The subset supports all
  115. * optional operations of the original.
  116. * <p>
  117. *
  118. * The returned set throws an IllegalArgumentException any time an element is
  119. * used which is out of the range of fromElement and toElement. Note that the
  120. * lower endpoint is included, but the upper is not; if you want to
  121. * change the inclusion or exclusion of an endpoint, pass the successor
  122. * object in instead. For example, for Strings, you can request
  123. * <code>subSet(lowlimit + "\0", highlimit + "\0")</code> to reverse
  124. * the inclusiveness of both endpoints.
  125. *
  126. * @param fromElement the inclusive lower range of the subset
  127. * @param toElement the exclusive upper range of the subset
  128. * @return the subset
  129. * @throws ClassCastException if fromElement or toElement is not comparable
  130. * to the set contents
  131. * @throws IllegalArgumentException if this is a subSet, and fromElement or
  132. * toElement is out of range
  133. * @throws NullPointerException if fromElement or toElement is null but the
  134. * set does not allow null elements
  135. */
  136. SortedSet subSet(Object fromElement, Object toElement);
  137. /**
  138. * Returns a view of the portion of the set greater than or equal to
  139. * fromElement. The view is backed by this set, so changes in one show up
  140. * in the other. The subset supports all optional operations of the original.
  141. * <p>
  142. *
  143. * The returned set throws an IllegalArgumentException any time an element is
  144. * used which is out of the range of fromElement. Note that the endpoint is
  145. * included; if you do not want the endpoint, pass the successor object in
  146. * to fromElement. For example, for Strings, you can request
  147. * <code>tailSet(limit + "\0")</code>.
  148. *
  149. * @param fromElement the inclusive lower range of the subset
  150. * @return the subset
  151. * @throws ClassCastException if fromElement is not comparable to the set
  152. * contents
  153. * @throws IllegalArgumentException if this is a subSet, and fromElement is
  154. * out of range
  155. * @throws NullPointerException if fromElement is null but the set does not
  156. * allow null elements
  157. */
  158. SortedSet tailSet(Object fromElement);
  159. }