SortedMap.java 7.1 KB

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