001 /*
002 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util;
027
028 /**
029 * A {@link Map} that further provides a <i>total ordering</i> on its keys.
030 * The map is ordered according to the {@linkplain Comparable natural
031 * ordering} of its keys, or by a {@link Comparator} typically
032 * provided at sorted map creation time. This order is reflected when
033 * iterating over the sorted map's collection views (returned by the
034 * <tt>entrySet</tt>, <tt>keySet</tt> and <tt>values</tt> methods).
035 * Several additional operations are provided to take advantage of the
036 * ordering. (This interface is the map analogue of {@link
037 * SortedSet}.)
038 *
039 * <p>All keys inserted into a sorted map must implement the <tt>Comparable</tt>
040 * interface (or be accepted by the specified comparator). Furthermore, all
041 * such keys must be <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> (or
042 * <tt>comparator.compare(k1, k2)</tt>) must not throw a
043 * <tt>ClassCastException</tt> for any keys <tt>k1</tt> and <tt>k2</tt> in
044 * the sorted map. Attempts to violate this restriction will cause the
045 * offending method or constructor invocation to throw a
046 * <tt>ClassCastException</tt>.
047 *
048 * <p>Note that the ordering maintained by a sorted map (whether or not an
049 * explicit comparator is provided) must be <i>consistent with equals</i> if
050 * the sorted map is to correctly implement the <tt>Map</tt> interface. (See
051 * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
052 * precise definition of <i>consistent with equals</i>.) This is so because
053 * the <tt>Map</tt> interface is defined in terms of the <tt>equals</tt>
054 * operation, but a sorted map performs all key comparisons using its
055 * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two keys that are
056 * deemed equal by this method are, from the standpoint of the sorted map,
057 * equal. The behavior of a tree map <i>is</i> well-defined even if its
058 * ordering is inconsistent with equals; it just fails to obey the general
059 * contract of the <tt>Map</tt> interface.
060 *
061 * <p>All general-purpose sorted map implementation classes should
062 * provide four "standard" constructors: 1) A void (no arguments)
063 * constructor, which creates an empty sorted map sorted according to
064 * the natural ordering of its keys. 2) A constructor with a
065 * single argument of type <tt>Comparator</tt>, which creates an empty
066 * sorted map sorted according to the specified comparator. 3) A
067 * constructor with a single argument of type <tt>Map</tt>, which
068 * creates a new map with the same key-value mappings as its argument,
069 * sorted according to the keys' natural ordering. 4) A constructor
070 * with a single argument of type <tt>SortedMap</tt>,
071 * which creates a new sorted map with the same key-value mappings and
072 * the same ordering as the input sorted map. There is no way to
073 * enforce this recommendation, as interfaces cannot contain
074 * constructors.
075 *
076 * <p>Note: several methods return submaps with restricted key ranges.
077 * Such ranges are <i>half-open</i>, that is, they include their low
078 * endpoint but not their high endpoint (where applicable). If you need a
079 * <i>closed range</i> (which includes both endpoints), and the key type
080 * allows for calculation of the successor of a given key, merely request
081 * the subrange from <tt>lowEndpoint</tt> to
082 * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>m</tt>
083 * is a map whose keys are strings. The following idiom obtains a view
084 * containing all of the key-value mappings in <tt>m</tt> whose keys are
085 * between <tt>low</tt> and <tt>high</tt>, inclusive:<pre>
086 * SortedMap<String, V> sub = m.subMap(low, high+"\0");</pre>
087 *
088 * A similar technique can be used to generate an <i>open range</i>
089 * (which contains neither endpoint). The following idiom obtains a
090 * view containing all of the key-value mappings in <tt>m</tt> whose keys
091 * are between <tt>low</tt> and <tt>high</tt>, exclusive:<pre>
092 * SortedMap<String, V> sub = m.subMap(low+"\0", high);</pre>
093 *
094 * <p>This interface is a member of the
095 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
096 * Java Collections Framework</a>.
097 *
098 * @param <K> the type of keys maintained by this map
099 * @param <V> the type of mapped values
100 *
101 * @author Josh Bloch
102 * @version 1.34, 05/05/07
103 * @see Map
104 * @see TreeMap
105 * @see SortedSet
106 * @see Comparator
107 * @see Comparable
108 * @see Collection
109 * @see ClassCastException
110 * @since 1.2
111 */
112
113 public interface SortedMap<K, V> extends Map<K, V> {
114 /**
115 * Returns the comparator used to order the keys in this map, or
116 * <tt>null</tt> if this map uses the {@linkplain Comparable
117 * natural ordering} of its keys.
118 *
119 * @return the comparator used to order the keys in this map,
120 * or <tt>null</tt> if this map uses the natural ordering
121 * of its keys
122 */
123 Comparator<? super K> comparator();
124
125 /**
126 * Returns a view of the portion of this map whose keys range from
127 * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If
128 * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned map
129 * is empty.) The returned map is backed by this map, so changes
130 * in the returned map are reflected in this map, and vice-versa.
131 * The returned map supports all optional map operations that this
132 * map supports.
133 *
134 * <p>The returned map will throw an <tt>IllegalArgumentException</tt>
135 * on an attempt to insert a key outside its range.
136 *
137 * @param fromKey low endpoint (inclusive) of the keys in the returned map
138 * @param toKey high endpoint (exclusive) of the keys in the returned map
139 * @return a view of the portion of this map whose keys range from
140 * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive
141 * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
142 * cannot be compared to one another using this map's comparator
143 * (or, if the map has no comparator, using natural ordering).
144 * Implementations may, but are not required to, throw this
145 * exception if <tt>fromKey</tt> or <tt>toKey</tt>
146 * cannot be compared to keys currently in the map.
147 * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt>
148 * is null and this map does not permit null keys
149 * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
150 * <tt>toKey</tt>; or if this map itself has a restricted
151 * range, and <tt>fromKey</tt> or <tt>toKey</tt> lies
152 * outside the bounds of the range
153 */
154 SortedMap<K, V> subMap(K fromKey, K toKey);
155
156 /**
157 * Returns a view of the portion of this map whose keys are
158 * strictly less than <tt>toKey</tt>. The returned map is backed
159 * by this map, so changes in the returned map are reflected in
160 * this map, and vice-versa. The returned map supports all
161 * optional map operations that this map supports.
162 *
163 * <p>The returned map will throw an <tt>IllegalArgumentException</tt>
164 * on an attempt to insert a key outside its range.
165 *
166 * @param toKey high endpoint (exclusive) of the keys in the returned map
167 * @return a view of the portion of this map whose keys are strictly
168 * less than <tt>toKey</tt>
169 * @throws ClassCastException if <tt>toKey</tt> is not compatible
170 * with this map's comparator (or, if the map has no comparator,
171 * if <tt>toKey</tt> does not implement {@link Comparable}).
172 * Implementations may, but are not required to, throw this
173 * exception if <tt>toKey</tt> cannot be compared to keys
174 * currently in the map.
175 * @throws NullPointerException if <tt>toKey</tt> is null and
176 * this map does not permit null keys
177 * @throws IllegalArgumentException if this map itself has a
178 * restricted range, and <tt>toKey</tt> lies outside the
179 * bounds of the range
180 */
181 SortedMap<K, V> headMap(K toKey);
182
183 /**
184 * Returns a view of the portion of this map whose keys are
185 * greater than or equal to <tt>fromKey</tt>. The returned map is
186 * backed by this map, so changes in the returned map are
187 * reflected in this map, and vice-versa. The returned map
188 * supports all optional map operations that this map supports.
189 *
190 * <p>The returned map will throw an <tt>IllegalArgumentException</tt>
191 * on an attempt to insert a key outside its range.
192 *
193 * @param fromKey low endpoint (inclusive) of the keys in the returned map
194 * @return a view of the portion of this map whose keys are greater
195 * than or equal to <tt>fromKey</tt>
196 * @throws ClassCastException if <tt>fromKey</tt> is not compatible
197 * with this map's comparator (or, if the map has no comparator,
198 * if <tt>fromKey</tt> does not implement {@link Comparable}).
199 * Implementations may, but are not required to, throw this
200 * exception if <tt>fromKey</tt> cannot be compared to keys
201 * currently in the map.
202 * @throws NullPointerException if <tt>fromKey</tt> is null and
203 * this map does not permit null keys
204 * @throws IllegalArgumentException if this map itself has a
205 * restricted range, and <tt>fromKey</tt> lies outside the
206 * bounds of the range
207 */
208 SortedMap<K, V> tailMap(K fromKey);
209
210 /**
211 * Returns the first (lowest) key currently in this map.
212 *
213 * @return the first (lowest) key currently in this map
214 * @throws NoSuchElementException if this map is empty
215 */
216 K firstKey();
217
218 /**
219 * Returns the last (highest) key currently in this map.
220 *
221 * @return the last (highest) key currently in this map
222 * @throws NoSuchElementException if this map is empty
223 */
224 K lastKey();
225
226 /**
227 * Returns a {@link Set} view of the keys contained in this map.
228 * The set's iterator returns the keys in ascending order.
229 * The set is backed by the map, so changes to the map are
230 * reflected in the set, and vice-versa. If the map is modified
231 * while an iteration over the set is in progress (except through
232 * the iterator's own <tt>remove</tt> operation), the results of
233 * the iteration are undefined. The set supports element removal,
234 * which removes the corresponding mapping from the map, via the
235 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
236 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
237 * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
238 * operations.
239 *
240 * @return a set view of the keys contained in this map, sorted in
241 * ascending order
242 */
243 Set<K> keySet();
244
245 /**
246 * Returns a {@link Collection} view of the values contained in this map.
247 * The collection's iterator returns the values in ascending order
248 * of the corresponding keys.
249 * The collection is backed by the map, so changes to the map are
250 * reflected in the collection, and vice-versa. If the map is
251 * modified while an iteration over the collection is in progress
252 * (except through the iterator's own <tt>remove</tt> operation),
253 * the results of the iteration are undefined. The collection
254 * supports element removal, which removes the corresponding
255 * mapping from the map, via the <tt>Iterator.remove</tt>,
256 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
257 * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
258 * support the <tt>add</tt> or <tt>addAll</tt> operations.
259 *
260 * @return a collection view of the values contained in this map,
261 * sorted in ascending key order
262 */
263 Collection<V> values();
264
265 /**
266 * Returns a {@link Set} view of the mappings contained in this map.
267 * The set's iterator returns the entries in ascending key order.
268 * The set is backed by the map, so changes to the map are
269 * reflected in the set, and vice-versa. If the map is modified
270 * while an iteration over the set is in progress (except through
271 * the iterator's own <tt>remove</tt> operation, or through the
272 * <tt>setValue</tt> operation on a map entry returned by the
273 * iterator) the results of the iteration are undefined. The set
274 * supports element removal, which removes the corresponding
275 * mapping from the map, via the <tt>Iterator.remove</tt>,
276 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
277 * <tt>clear</tt> operations. It does not support the
278 * <tt>add</tt> or <tt>addAll</tt> operations.
279 *
280 * @return a set view of the mappings contained in this map,
281 * sorted in ascending key order
282 */
283 Set<Map.Entry<K, V>> entrySet();
284 }
|