1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * This file is available under and governed by the GNU General Public
27  * License version 2 only, as published by the Free Software Foundation.
28  * However, the following notice accompanied the original version of this
29  * file:
30  * Written by Doug Lea and Josh Bloch with assistance from members of JCP
31  * JSR-166 Expert Group and released to the public domain, as explained at
32  * http://creativecommons.org/publicdomain/zero/1.0/
33  */
34 
35 package java.util;
36 
37 // BEGIN android-note
38 // removed link to collections framework docs
39 // END android-note
40 
41 /**
42  * A {@link SortedMap} extended with navigation methods returning the
43  * closest matches for given search targets. Methods
44  * {@link #lowerEntry}, {@link #floorEntry}, {@link #ceilingEntry},
45  * and {@link #higherEntry} return {@code Map.Entry} objects
46  * associated with keys respectively less than, less than or equal,
47  * greater than or equal, and greater than a given key, returning
48  * {@code null} if there is no such key.  Similarly, methods
49  * {@link #lowerKey}, {@link #floorKey}, {@link #ceilingKey}, and
50  * {@link #higherKey} return only the associated keys. All of these
51  * methods are designed for locating, not traversing entries.
52  *
53  * <p>A {@code NavigableMap} may be accessed and traversed in either
54  * ascending or descending key order.  The {@link #descendingMap}
55  * method returns a view of the map with the senses of all relational
56  * and directional methods inverted. The performance of ascending
57  * operations and views is likely to be faster than that of descending
58  * ones.  Methods
59  * {@link #subMap(Object, boolean, Object, boolean) subMap(K, boolean, K, boolean)},
60  * {@link #headMap(Object, boolean) headMap(K, boolean)}, and
61  * {@link #tailMap(Object, boolean) tailMap(K, boolean)}
62  * differ from the like-named {@code SortedMap} methods in accepting
63  * additional arguments describing whether lower and upper bounds are
64  * inclusive versus exclusive.  Submaps of any {@code NavigableMap}
65  * must implement the {@code NavigableMap} interface.
66  *
67  * <p>This interface additionally defines methods {@link #firstEntry},
68  * {@link #pollFirstEntry}, {@link #lastEntry}, and
69  * {@link #pollLastEntry} that return and/or remove the least and
70  * greatest mappings, if any exist, else returning {@code null}.
71  *
72  * <p>Implementations of entry-returning methods are expected to
73  * return {@code Map.Entry} pairs representing snapshots of mappings
74  * at the time they were produced, and thus generally do <em>not</em>
75  * support the optional {@code Entry.setValue} method. Note however
76  * that it is possible to change mappings in the associated map using
77  * method {@code put}.
78  *
79  * <p>Methods
80  * {@link #subMap(Object, Object) subMap(K, K)},
81  * {@link #headMap(Object) headMap(K)}, and
82  * {@link #tailMap(Object) tailMap(K)}
83  * are specified to return {@code SortedMap} to allow existing
84  * implementations of {@code SortedMap} to be compatibly retrofitted to
85  * implement {@code NavigableMap}, but extensions and implementations
86  * of this interface are encouraged to override these methods to return
87  * {@code NavigableMap}.  Similarly,
88  * {@link #keySet()} can be overridden to return {@link NavigableSet}.
89  *
90  * @author Doug Lea
91  * @author Josh Bloch
92  * @param <K> the type of keys maintained by this map
93  * @param <V> the type of mapped values
94  * @since 1.6
95  */
96 public interface NavigableMap<K,V> extends SortedMap<K,V> {
97     /**
98      * Returns a key-value mapping associated with the greatest key
99      * strictly less than the given key, or {@code null} if there is
100      * no such key.
101      *
102      * @param key the key
103      * @return an entry with the greatest key less than {@code key},
104      *         or {@code null} if there is no such key
105      * @throws ClassCastException if the specified key cannot be compared
106      *         with the keys currently in the map
107      * @throws NullPointerException if the specified key is null
108      *         and this map does not permit null keys
109      */
lowerEntry(K key)110     Map.Entry<K,V> lowerEntry(K key);
111 
112     /**
113      * Returns the greatest key strictly less than the given key, or
114      * {@code null} if there is no such key.
115      *
116      * @param key the key
117      * @return the greatest key less than {@code key},
118      *         or {@code null} if there is no such key
119      * @throws ClassCastException if the specified key cannot be compared
120      *         with the keys currently in the map
121      * @throws NullPointerException if the specified key is null
122      *         and this map does not permit null keys
123      */
lowerKey(K key)124     K lowerKey(K key);
125 
126     /**
127      * Returns a key-value mapping associated with the greatest key
128      * less than or equal to the given key, or {@code null} if there
129      * is no such key.
130      *
131      * @param key the key
132      * @return an entry with the greatest key less than or equal to
133      *         {@code key}, or {@code null} if there is no such key
134      * @throws ClassCastException if the specified key cannot be compared
135      *         with the keys currently in the map
136      * @throws NullPointerException if the specified key is null
137      *         and this map does not permit null keys
138      */
floorEntry(K key)139     Map.Entry<K,V> floorEntry(K key);
140 
141     /**
142      * Returns the greatest key less than or equal to the given key,
143      * or {@code null} if there is no such key.
144      *
145      * @param key the key
146      * @return the greatest key less than or equal to {@code key},
147      *         or {@code null} if there is no such key
148      * @throws ClassCastException if the specified key cannot be compared
149      *         with the keys currently in the map
150      * @throws NullPointerException if the specified key is null
151      *         and this map does not permit null keys
152      */
floorKey(K key)153     K floorKey(K key);
154 
155     /**
156      * Returns a key-value mapping associated with the least key
157      * greater than or equal to the given key, or {@code null} if
158      * there is no such key.
159      *
160      * @param key the key
161      * @return an entry with the least key greater than or equal to
162      *         {@code key}, or {@code null} if there is no such key
163      * @throws ClassCastException if the specified key cannot be compared
164      *         with the keys currently in the map
165      * @throws NullPointerException if the specified key is null
166      *         and this map does not permit null keys
167      */
ceilingEntry(K key)168     Map.Entry<K,V> ceilingEntry(K key);
169 
170     /**
171      * Returns the least key greater than or equal to the given key,
172      * or {@code null} if there is no such key.
173      *
174      * @param key the key
175      * @return the least key greater than or equal to {@code key},
176      *         or {@code null} if there is no such key
177      * @throws ClassCastException if the specified key cannot be compared
178      *         with the keys currently in the map
179      * @throws NullPointerException if the specified key is null
180      *         and this map does not permit null keys
181      */
ceilingKey(K key)182     K ceilingKey(K key);
183 
184     /**
185      * Returns a key-value mapping associated with the least key
186      * strictly greater than the given key, or {@code null} if there
187      * is no such key.
188      *
189      * @param key the key
190      * @return an entry with the least key greater than {@code key},
191      *         or {@code null} if there is no such key
192      * @throws ClassCastException if the specified key cannot be compared
193      *         with the keys currently in the map
194      * @throws NullPointerException if the specified key is null
195      *         and this map does not permit null keys
196      */
higherEntry(K key)197     Map.Entry<K,V> higherEntry(K key);
198 
199     /**
200      * Returns the least key strictly greater than the given key, or
201      * {@code null} if there is no such key.
202      *
203      * @param key the key
204      * @return the least key greater than {@code key},
205      *         or {@code null} if there is no such key
206      * @throws ClassCastException if the specified key cannot be compared
207      *         with the keys currently in the map
208      * @throws NullPointerException if the specified key is null
209      *         and this map does not permit null keys
210      */
higherKey(K key)211     K higherKey(K key);
212 
213     /**
214      * Returns a key-value mapping associated with the least
215      * key in this map, or {@code null} if the map is empty.
216      *
217      * @return an entry with the least key,
218      *         or {@code null} if this map is empty
219      */
firstEntry()220     Map.Entry<K,V> firstEntry();
221 
222     /**
223      * Returns a key-value mapping associated with the greatest
224      * key in this map, or {@code null} if the map is empty.
225      *
226      * @return an entry with the greatest key,
227      *         or {@code null} if this map is empty
228      */
lastEntry()229     Map.Entry<K,V> lastEntry();
230 
231     /**
232      * Removes and returns a key-value mapping associated with
233      * the least key in this map, or {@code null} if the map is empty.
234      *
235      * @return the removed first entry of this map,
236      *         or {@code null} if this map is empty
237      */
pollFirstEntry()238     Map.Entry<K,V> pollFirstEntry();
239 
240     /**
241      * Removes and returns a key-value mapping associated with
242      * the greatest key in this map, or {@code null} if the map is empty.
243      *
244      * @return the removed last entry of this map,
245      *         or {@code null} if this map is empty
246      */
pollLastEntry()247     Map.Entry<K,V> pollLastEntry();
248 
249     /**
250      * Returns a reverse order view of the mappings contained in this map.
251      * The descending map is backed by this map, so changes to the map are
252      * reflected in the descending map, and vice-versa.  If either map is
253      * modified while an iteration over a collection view of either map
254      * is in progress (except through the iterator's own {@code remove}
255      * operation), the results of the iteration are undefined.
256      *
257      * <p>The returned map has an ordering equivalent to
258      * {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}{@code (comparator())}.
259      * The expression {@code m.descendingMap().descendingMap()} returns a
260      * view of {@code m} essentially equivalent to {@code m}.
261      *
262      * @return a reverse order view of this map
263      */
descendingMap()264     NavigableMap<K,V> descendingMap();
265 
266     /**
267      * Returns a {@link NavigableSet} view of the keys contained in this map.
268      * The set's iterator returns the keys in ascending order.
269      * The set is backed by the map, so changes to the map are reflected in
270      * the set, and vice-versa.  If the map is modified while an iteration
271      * over the set is in progress (except through the iterator's own {@code
272      * remove} operation), the results of the iteration are undefined.  The
273      * set supports element removal, which removes the corresponding mapping
274      * from the map, via the {@code Iterator.remove}, {@code Set.remove},
275      * {@code removeAll}, {@code retainAll}, and {@code clear} operations.
276      * It does not support the {@code add} or {@code addAll} operations.
277      *
278      * @return a navigable set view of the keys in this map
279      */
navigableKeySet()280     NavigableSet<K> navigableKeySet();
281 
282     /**
283      * Returns a reverse order {@link NavigableSet} view of the keys contained in this map.
284      * The set's iterator returns the keys in descending order.
285      * The set is backed by the map, so changes to the map are reflected in
286      * the set, and vice-versa.  If the map is modified while an iteration
287      * over the set is in progress (except through the iterator's own {@code
288      * remove} operation), the results of the iteration are undefined.  The
289      * set supports element removal, which removes the corresponding mapping
290      * from the map, via the {@code Iterator.remove}, {@code Set.remove},
291      * {@code removeAll}, {@code retainAll}, and {@code clear} operations.
292      * It does not support the {@code add} or {@code addAll} operations.
293      *
294      * @return a reverse order navigable set view of the keys in this map
295      */
descendingKeySet()296     NavigableSet<K> descendingKeySet();
297 
298     /**
299      * Returns a view of the portion of this map whose keys range from
300      * {@code fromKey} to {@code toKey}.  If {@code fromKey} and
301      * {@code toKey} are equal, the returned map is empty unless
302      * {@code fromInclusive} and {@code toInclusive} are both true.  The
303      * returned map is backed by this map, so changes in the returned map are
304      * reflected in this map, and vice-versa.  The returned map supports all
305      * optional map operations that this map supports.
306      *
307      * <p>The returned map will throw an {@code IllegalArgumentException}
308      * on an attempt to insert a key outside of its range, or to construct a
309      * submap either of whose endpoints lie outside its range.
310      *
311      * @param fromKey low endpoint of the keys in the returned map
312      * @param fromInclusive {@code true} if the low endpoint
313      *        is to be included in the returned view
314      * @param toKey high endpoint of the keys in the returned map
315      * @param toInclusive {@code true} if the high endpoint
316      *        is to be included in the returned view
317      * @return a view of the portion of this map whose keys range from
318      *         {@code fromKey} to {@code toKey}
319      * @throws ClassCastException if {@code fromKey} and {@code toKey}
320      *         cannot be compared to one another using this map's comparator
321      *         (or, if the map has no comparator, using natural ordering).
322      *         Implementations may, but are not required to, throw this
323      *         exception if {@code fromKey} or {@code toKey}
324      *         cannot be compared to keys currently in the map.
325      * @throws NullPointerException if {@code fromKey} or {@code toKey}
326      *         is null and this map does not permit null keys
327      * @throws IllegalArgumentException if {@code fromKey} is greater than
328      *         {@code toKey}; or if this map itself has a restricted
329      *         range, and {@code fromKey} or {@code toKey} lies
330      *         outside the bounds of the range
331      */
subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)332     NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
333                              K toKey,   boolean toInclusive);
334 
335     /**
336      * Returns a view of the portion of this map whose keys are less than (or
337      * equal to, if {@code inclusive} is true) {@code toKey}.  The returned
338      * map is backed by this map, so changes in the returned map are reflected
339      * in this map, and vice-versa.  The returned map supports all optional
340      * map operations that this map supports.
341      *
342      * <p>The returned map will throw an {@code IllegalArgumentException}
343      * on an attempt to insert a key outside its range.
344      *
345      * @param toKey high endpoint of the keys in the returned map
346      * @param inclusive {@code true} if the high endpoint
347      *        is to be included in the returned view
348      * @return a view of the portion of this map whose keys are less than
349      *         (or equal to, if {@code inclusive} is true) {@code toKey}
350      * @throws ClassCastException if {@code toKey} is not compatible
351      *         with this map's comparator (or, if the map has no comparator,
352      *         if {@code toKey} does not implement {@link Comparable}).
353      *         Implementations may, but are not required to, throw this
354      *         exception if {@code toKey} cannot be compared to keys
355      *         currently in the map.
356      * @throws NullPointerException if {@code toKey} is null
357      *         and this map does not permit null keys
358      * @throws IllegalArgumentException if this map itself has a
359      *         restricted range, and {@code toKey} lies outside the
360      *         bounds of the range
361      */
headMap(K toKey, boolean inclusive)362     NavigableMap<K,V> headMap(K toKey, boolean inclusive);
363 
364     /**
365      * Returns a view of the portion of this map whose keys are greater than (or
366      * equal to, if {@code inclusive} is true) {@code fromKey}.  The returned
367      * map is backed by this map, so changes in the returned map are reflected
368      * in this map, and vice-versa.  The returned map supports all optional
369      * map operations that this map supports.
370      *
371      * <p>The returned map will throw an {@code IllegalArgumentException}
372      * on an attempt to insert a key outside its range.
373      *
374      * @param fromKey low endpoint of the keys in the returned map
375      * @param inclusive {@code true} if the low endpoint
376      *        is to be included in the returned view
377      * @return a view of the portion of this map whose keys are greater than
378      *         (or equal to, if {@code inclusive} is true) {@code fromKey}
379      * @throws ClassCastException if {@code fromKey} is not compatible
380      *         with this map's comparator (or, if the map has no comparator,
381      *         if {@code fromKey} does not implement {@link Comparable}).
382      *         Implementations may, but are not required to, throw this
383      *         exception if {@code fromKey} cannot be compared to keys
384      *         currently in the map.
385      * @throws NullPointerException if {@code fromKey} is null
386      *         and this map does not permit null keys
387      * @throws IllegalArgumentException if this map itself has a
388      *         restricted range, and {@code fromKey} lies outside the
389      *         bounds of the range
390      */
tailMap(K fromKey, boolean inclusive)391     NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
392 
393     /**
394      * {@inheritDoc}
395      *
396      * <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
397      *
398      * @throws ClassCastException       {@inheritDoc}
399      * @throws NullPointerException     {@inheritDoc}
400      * @throws IllegalArgumentException {@inheritDoc}
401      */
subMap(K fromKey, K toKey)402     SortedMap<K,V> subMap(K fromKey, K toKey);
403 
404     /**
405      * {@inheritDoc}
406      *
407      * <p>Equivalent to {@code headMap(toKey, false)}.
408      *
409      * @throws ClassCastException       {@inheritDoc}
410      * @throws NullPointerException     {@inheritDoc}
411      * @throws IllegalArgumentException {@inheritDoc}
412      */
headMap(K toKey)413     SortedMap<K,V> headMap(K toKey);
414 
415     /**
416      * {@inheritDoc}
417      *
418      * <p>Equivalent to {@code tailMap(fromKey, true)}.
419      *
420      * @throws ClassCastException       {@inheritDoc}
421      * @throws NullPointerException     {@inheritDoc}
422      * @throws IllegalArgumentException {@inheritDoc}
423      */
tailMap(K fromKey)424     SortedMap<K,V> tailMap(K fromKey);
425 }
426