1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util;
9   
10  /**
11   * A {@link Map} that further provides a <i>total ordering</i> on its keys.
12   * The map is ordered according to the {@linkplain Comparable natural
13   * ordering} of its keys, or by a {@link Comparator} typically
14   * provided at sorted map creation time.  This order is reflected when
15   * iterating over the sorted map's collection views (returned by the
16   * <tt>entrySet</tt>, <tt>keySet</tt> and <tt>values</tt> methods).
17   * Several additional operations are provided to take advantage of the
18   * ordering.  (This interface is the map analogue of {@link
19   * SortedSet}.)
20   *
21   * <p>All keys inserted into a sorted map must implement the <tt>Comparable</tt>
22   * interface (or be accepted by the specified comparator).  Furthermore, all
23   * such keys must be <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> (or
24   * <tt>comparator.compare(k1, k2)</tt>) must not throw a
25   * <tt>ClassCastException</tt> for any keys <tt>k1</tt> and <tt>k2</tt> in
26   * the sorted map.  Attempts to violate this restriction will cause the
27   * offending method or constructor invocation to throw a
28   * <tt>ClassCastException</tt>.
29   *
30   * <p>Note that the ordering maintained by a sorted map (whether or not an
31   * explicit comparator is provided) must be <i>consistent with equals</i> if
32   * the sorted map is to correctly implement the <tt>Map</tt> interface.  (See
33   * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
34   * precise definition of <i>consistent with equals</i>.)  This is so because
35   * the <tt>Map</tt> interface is defined in terms of the <tt>equals</tt>
36   * operation, but a sorted map performs all key comparisons using its
37   * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two keys that are
38   * deemed equal by this method are, from the standpoint of the sorted map,
39   * equal.  The behavior of a tree map <i>is</i> well-defined even if its
40   * ordering is inconsistent with equals; it just fails to obey the general
41   * contract of the <tt>Map</tt> interface.
42   *
43   * <p>All general-purpose sorted map implementation classes should
44   * provide four "standard" constructors: 1) A void (no arguments)
45   * constructor, which creates an empty sorted map sorted according to
46   * the natural ordering of its keys.  2) A constructor with a
47   * single argument of type <tt>Comparator</tt>, which creates an empty
48   * sorted map sorted according to the specified comparator.  3) A
49   * constructor with a single argument of type <tt>Map</tt>, which
50   * creates a new map with the same key-value mappings as its argument,
51   * sorted according to the keys' natural ordering.  4) A constructor
52   * with a single argument of type <tt>SortedMap</tt>,
53   * which creates a new sorted map with the same key-value mappings and
54   * the same ordering as the input sorted map.  There is no way to
55   * enforce this recommendation, as interfaces cannot contain
56   * constructors.
57   *
58   * <p>Note: several methods return submaps with restricted key ranges.
59   * Such ranges are <i>half-open</i>, that is, they include their low
60   * endpoint but not their high endpoint (where applicable).  If you need a
61   * <i>closed range</i> (which includes both endpoints), and the key type
62   * allows for calculation of the successor of a given key, merely request
63   * the subrange from <tt>lowEndpoint</tt> to
64   * <tt>successor(highEndpoint)</tt>.  For example, suppose that <tt>m</tt>
65   * is a map whose keys are strings.  The following idiom obtains a view
66   * containing all of the key-value mappings in <tt>m</tt> whose keys are
67   * between <tt>low</tt> and <tt>high</tt>, inclusive:<pre>
68   *   SortedMap&lt;String, V&gt; sub = m.subMap(low, high+"\0");</pre>
69   *
70   * A similar technique can be used to generate an <i>open range</i>
71   * (which contains neither endpoint).  The following idiom obtains a
72   * view containing all of the key-value mappings in <tt>m</tt> whose keys
73   * are between <tt>low</tt> and <tt>high</tt>, exclusive:<pre>
74   *   SortedMap&lt;String, V&gt; sub = m.subMap(low+"\0", high);</pre>
75   *
76   * <p>This interface is a member of the
77   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
78   * Java Collections Framework</a>.
79   *
80   * @param <K> the type of keys maintained by this map
81   * @param <V> the type of mapped values
82   *
83   * @author  Josh Bloch
84   * @version %I%, %G%
85   * @see Map
86   * @see TreeMap
87   * @see SortedSet
88   * @see Comparator
89   * @see Comparable
90   * @see Collection
91   * @see ClassCastException
92   * @since 1.2
93   */
94  
95  public interface SortedMap<K,V> extends Map<K,V> {
96      /**
97       * Returns the comparator used to order the keys in this map, or
98       * <tt>null</tt> if this map uses the {@linkplain Comparable
99       * natural ordering} of its keys.
100      *
101      * @return the comparator used to order the keys in this map,
102      *         or <tt>null</tt> if this map uses the natural ordering
103      *         of its keys
104      */
105     Comparator<? super K> comparator();
106 
107     /**
108      * Returns a view of the portion of this map whose keys range from
109      * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.  (If
110      * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned map
111      * is empty.)  The returned map is backed by this map, so changes
112      * in the returned map are reflected in this map, and vice-versa.
113      * The returned map supports all optional map operations that this
114      * map supports.
115      *
116      * <p>The returned map will throw an <tt>IllegalArgumentException</tt>
117      * on an attempt to insert a key outside its range.
118      *
119      * @param fromKey low endpoint (inclusive) of the keys in the returned map
120      * @param toKey high endpoint (exclusive) of the keys in the returned map
121      * @return a view of the portion of this map whose keys range from
122      *         <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive
123      * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
124      *         cannot be compared to one another using this map's comparator
125      *         (or, if the map has no comparator, using natural ordering).
126      *         Implementations may, but are not required to, throw this
127      *         exception if <tt>fromKey</tt> or <tt>toKey</tt>
128      *         cannot be compared to keys currently in the map.
129      * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt>
130      *         is null and this map does not permit null keys
131      * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
132      *         <tt>toKey</tt>; or if this map itself has a restricted
133      *         range, and <tt>fromKey</tt> or <tt>toKey</tt> lies
134      *         outside the bounds of the range
135      */
136     SortedMap<K,V> subMap(K fromKey, K toKey);
137 
138     /**
139      * Returns a view of the portion of this map whose keys are
140      * strictly less than <tt>toKey</tt>.  The returned map is backed
141      * by this map, so changes in the returned map are reflected in
142      * this map, and vice-versa.  The returned map supports all
143      * optional map operations that this map supports.
144      *
145      * <p>The returned map will throw an <tt>IllegalArgumentException</tt>
146      * on an attempt to insert a key outside its range.
147      *
148      * @param toKey high endpoint (exclusive) of the keys in the returned map
149      * @return a view of the portion of this map whose keys are strictly
150      *         less than <tt>toKey</tt>
151      * @throws ClassCastException if <tt>toKey</tt> is not compatible
152      *         with this map's comparator (or, if the map has no comparator,
153      *         if <tt>toKey</tt> does not implement {@link Comparable}).
154      *         Implementations may, but are not required to, throw this
155      *         exception if <tt>toKey</tt> cannot be compared to keys
156      *         currently in the map.
157      * @throws NullPointerException if <tt>toKey</tt> is null and
158      *         this map does not permit null keys
159      * @throws IllegalArgumentException if this map itself has a
160      *         restricted range, and <tt>toKey</tt> lies outside the
161      *         bounds of the range
162      */
163     SortedMap<K,V> headMap(K toKey);
164 
165     /**
166      * Returns a view of the portion of this map whose keys are
167      * greater than or equal to <tt>fromKey</tt>.  The returned map is
168      * backed by this map, so changes in the returned map are
169      * reflected in this map, and vice-versa.  The returned map
170      * supports all optional map operations that this map supports.
171      *
172      * <p>The returned map will throw an <tt>IllegalArgumentException</tt>
173      * on an attempt to insert a key outside its range.
174      *
175      * @param fromKey low endpoint (inclusive) of the keys in the returned map
176      * @return a view of the portion of this map whose keys are greater
177      *         than or equal to <tt>fromKey</tt>
178      * @throws ClassCastException if <tt>fromKey</tt> is not compatible
179      *         with this map's comparator (or, if the map has no comparator,
180      *         if <tt>fromKey</tt> does not implement {@link Comparable}).
181      *         Implementations may, but are not required to, throw this
182      *         exception if <tt>fromKey</tt> cannot be compared to keys
183      *         currently in the map.
184      * @throws NullPointerException if <tt>fromKey</tt> is null and
185      *         this map does not permit null keys
186      * @throws IllegalArgumentException if this map itself has a
187      *         restricted range, and <tt>fromKey</tt> lies outside the
188      *         bounds of the range
189      */
190     SortedMap<K,V> tailMap(K fromKey);
191 
192     /**
193      * Returns the first (lowest) key currently in this map.
194      *
195      * @return the first (lowest) key currently in this map
196      * @throws NoSuchElementException if this map is empty
197      */
198     K firstKey();
199 
200     /**
201      * Returns the last (highest) key currently in this map.
202      *
203      * @return the last (highest) key currently in this map
204      * @throws NoSuchElementException if this map is empty
205      */
206     K lastKey();
207 
208     /**
209      * Returns a {@link Set} view of the keys contained in this map.
210      * The set's iterator returns the keys in ascending order.
211      * The set is backed by the map, so changes to the map are
212      * reflected in the set, and vice-versa.  If the map is modified
213      * while an iteration over the set is in progress (except through
214      * the iterator's own <tt>remove</tt> operation), the results of
215      * the iteration are undefined.  The set supports element removal,
216      * which removes the corresponding mapping from the map, via the
217      * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
218      * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
219      * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
220      * operations.
221      *
222      * @return a set view of the keys contained in this map, sorted in
223      *         ascending order
224      */
225     Set<K> keySet();
226 
227     /**
228      * Returns a {@link Collection} view of the values contained in this map.
229      * The collection's iterator returns the values in ascending order
230      * of the corresponding keys.
231      * The collection is backed by the map, so changes to the map are
232      * reflected in the collection, and vice-versa.  If the map is
233      * modified while an iteration over the collection is in progress
234      * (except through the iterator's own <tt>remove</tt> operation),
235      * the results of the iteration are undefined.  The collection
236      * supports element removal, which removes the corresponding
237      * mapping from the map, via the <tt>Iterator.remove</tt>,
238      * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
239      * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
240      * support the <tt>add</tt> or <tt>addAll</tt> operations.
241      *
242      * @return a collection view of the values contained in this map,
243      *         sorted in ascending key order
244      */
245     Collection<V> values();
246 
247     /**
248      * Returns a {@link Set} view of the mappings contained in this map.
249      * The set's iterator returns the entries in ascending key order.
250      * The set is backed by the map, so changes to the map are
251      * reflected in the set, and vice-versa.  If the map is modified
252      * while an iteration over the set is in progress (except through
253      * the iterator's own <tt>remove</tt> operation, or through the
254      * <tt>setValue</tt> operation on a map entry returned by the
255      * iterator) the results of the iteration are undefined.  The set
256      * supports element removal, which removes the corresponding
257      * mapping from the map, via the <tt>Iterator.remove</tt>,
258      * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
259      * <tt>clear</tt> operations.  It does not support the
260      * <tt>add</tt> or <tt>addAll</tt> operations.
261      *
262      * @return a set view of the mappings contained in this map,
263      *         sorted in ascending key order
264      */
265     Set<Map.Entry<K, V>> entrySet();
266 }
267