rt/emul/compact/src/main/java/java/util/SortedMap.java
changeset 772 d382dacfd73f
parent 597 ee8a922f4268
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/SortedMap.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util;
    1.30 +
    1.31 +/**
    1.32 + * A {@link Map} that further provides a <em>total ordering</em> on its keys.
    1.33 + * The map is ordered according to the {@linkplain Comparable natural
    1.34 + * ordering} of its keys, or by a {@link Comparator} typically
    1.35 + * provided at sorted map creation time.  This order is reflected when
    1.36 + * iterating over the sorted map's collection views (returned by the
    1.37 + * {@code entrySet}, {@code keySet} and {@code values} methods).
    1.38 + * Several additional operations are provided to take advantage of the
    1.39 + * ordering.  (This interface is the map analogue of {@link SortedSet}.)
    1.40 + *
    1.41 + * <p>All keys inserted into a sorted map must implement the {@code Comparable}
    1.42 + * interface (or be accepted by the specified comparator).  Furthermore, all
    1.43 + * such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} (or
    1.44 + * {@code comparator.compare(k1, k2)}) must not throw a
    1.45 + * {@code ClassCastException} for any keys {@code k1} and {@code k2} in
    1.46 + * the sorted map.  Attempts to violate this restriction will cause the
    1.47 + * offending method or constructor invocation to throw a
    1.48 + * {@code ClassCastException}.
    1.49 + *
    1.50 + * <p>Note that the ordering maintained by a sorted map (whether or not an
    1.51 + * explicit comparator is provided) must be <em>consistent with equals</em> if
    1.52 + * the sorted map is to correctly implement the {@code Map} interface.  (See
    1.53 + * the {@code Comparable} interface or {@code Comparator} interface for a
    1.54 + * precise definition of <em>consistent with equals</em>.)  This is so because
    1.55 + * the {@code Map} interface is defined in terms of the {@code equals}
    1.56 + * operation, but a sorted map performs all key comparisons using its
    1.57 + * {@code compareTo} (or {@code compare}) method, so two keys that are
    1.58 + * deemed equal by this method are, from the standpoint of the sorted map,
    1.59 + * equal.  The behavior of a tree map <em>is</em> well-defined even if its
    1.60 + * ordering is inconsistent with equals; it just fails to obey the general
    1.61 + * contract of the {@code Map} interface.
    1.62 + *
    1.63 + * <p>All general-purpose sorted map implementation classes should provide four
    1.64 + * "standard" constructors. It is not possible to enforce this recommendation
    1.65 + * though as required constructors cannot be specified by interfaces. The
    1.66 + * expected "standard" constructors for all sorted map implementations are:
    1.67 + * <ol>
    1.68 + *   <li>A void (no arguments) constructor, which creates an empty sorted map
    1.69 + *   sorted according to the natural ordering of its keys.</li>
    1.70 + *   <li>A constructor with a single argument of type {@code Comparator}, which
    1.71 + *   creates an empty sorted map sorted according to the specified comparator.</li>
    1.72 + *   <li>A constructor with a single argument of type {@code Map}, which creates
    1.73 + *   a new map with the same key-value mappings as its argument, sorted
    1.74 + *   according to the keys' natural ordering.</li>
    1.75 + *   <li>A constructor with a single argument of type {@code SortedMap}, which
    1.76 + *   creates a new sorted map with the same key-value mappings and the same
    1.77 + *   ordering as the input sorted map.</li>
    1.78 + * </ol>
    1.79 + *
    1.80 + * <p><strong>Note</strong>: several methods return submaps with restricted key
    1.81 + * ranges. Such ranges are <em>half-open</em>, that is, they include their low
    1.82 + * endpoint but not their high endpoint (where applicable).  If you need a
    1.83 + * <em>closed range</em> (which includes both endpoints), and the key type
    1.84 + * allows for calculation of the successor of a given key, merely request
    1.85 + * the subrange from {@code lowEndpoint} to
    1.86 + * {@code successor(highEndpoint)}.  For example, suppose that {@code m}
    1.87 + * is a map whose keys are strings.  The following idiom obtains a view
    1.88 + * containing all of the key-value mappings in {@code m} whose keys are
    1.89 + * between {@code low} and {@code high}, inclusive:<pre>
    1.90 + *   SortedMap&lt;String, V&gt; sub = m.subMap(low, high+"\0");</pre>
    1.91 + *
    1.92 + * A similar technique can be used to generate an <em>open range</em>
    1.93 + * (which contains neither endpoint).  The following idiom obtains a
    1.94 + * view containing all of the key-value mappings in {@code m} whose keys
    1.95 + * are between {@code low} and {@code high}, exclusive:<pre>
    1.96 + *   SortedMap&lt;String, V&gt; sub = m.subMap(low+"\0", high);</pre>
    1.97 + *
    1.98 + * <p>This interface is a member of the
    1.99 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   1.100 + * Java Collections Framework</a>.
   1.101 + *
   1.102 + * @param <K> the type of keys maintained by this map
   1.103 + * @param <V> the type of mapped values
   1.104 + *
   1.105 + * @author  Josh Bloch
   1.106 + * @see Map
   1.107 + * @see TreeMap
   1.108 + * @see SortedSet
   1.109 + * @see Comparator
   1.110 + * @see Comparable
   1.111 + * @see Collection
   1.112 + * @see ClassCastException
   1.113 + * @since 1.2
   1.114 + */
   1.115 +
   1.116 +public interface SortedMap<K,V> extends Map<K,V> {
   1.117 +    /**
   1.118 +     * Returns the comparator used to order the keys in this map, or
   1.119 +     * {@code null} if this map uses the {@linkplain Comparable
   1.120 +     * natural ordering} of its keys.
   1.121 +     *
   1.122 +     * @return the comparator used to order the keys in this map,
   1.123 +     *         or {@code null} if this map uses the natural ordering
   1.124 +     *         of its keys
   1.125 +     */
   1.126 +    Comparator<? super K> comparator();
   1.127 +
   1.128 +    /**
   1.129 +     * Returns a view of the portion of this map whose keys range from
   1.130 +     * {@code fromKey}, inclusive, to {@code toKey}, exclusive.  (If
   1.131 +     * {@code fromKey} and {@code toKey} are equal, the returned map
   1.132 +     * is empty.)  The returned map is backed by this map, so changes
   1.133 +     * in the returned map are reflected in this map, and vice-versa.
   1.134 +     * The returned map supports all optional map operations that this
   1.135 +     * map supports.
   1.136 +     *
   1.137 +     * <p>The returned map will throw an {@code IllegalArgumentException}
   1.138 +     * on an attempt to insert a key outside its range.
   1.139 +     *
   1.140 +     * @param fromKey low endpoint (inclusive) of the keys in the returned map
   1.141 +     * @param toKey high endpoint (exclusive) of the keys in the returned map
   1.142 +     * @return a view of the portion of this map whose keys range from
   1.143 +     *         {@code fromKey}, inclusive, to {@code toKey}, exclusive
   1.144 +     * @throws ClassCastException if {@code fromKey} and {@code toKey}
   1.145 +     *         cannot be compared to one another using this map's comparator
   1.146 +     *         (or, if the map has no comparator, using natural ordering).
   1.147 +     *         Implementations may, but are not required to, throw this
   1.148 +     *         exception if {@code fromKey} or {@code toKey}
   1.149 +     *         cannot be compared to keys currently in the map.
   1.150 +     * @throws NullPointerException if {@code fromKey} or {@code toKey}
   1.151 +     *         is null and this map does not permit null keys
   1.152 +     * @throws IllegalArgumentException if {@code fromKey} is greater than
   1.153 +     *         {@code toKey}; or if this map itself has a restricted
   1.154 +     *         range, and {@code fromKey} or {@code toKey} lies
   1.155 +     *         outside the bounds of the range
   1.156 +     */
   1.157 +    SortedMap<K,V> subMap(K fromKey, K toKey);
   1.158 +
   1.159 +    /**
   1.160 +     * Returns a view of the portion of this map whose keys are
   1.161 +     * strictly less than {@code toKey}.  The returned map is backed
   1.162 +     * by this map, so changes in the returned map are reflected in
   1.163 +     * this map, and vice-versa.  The returned map supports all
   1.164 +     * optional map operations that this map supports.
   1.165 +     *
   1.166 +     * <p>The returned map will throw an {@code IllegalArgumentException}
   1.167 +     * on an attempt to insert a key outside its range.
   1.168 +     *
   1.169 +     * @param toKey high endpoint (exclusive) of the keys in the returned map
   1.170 +     * @return a view of the portion of this map whose keys are strictly
   1.171 +     *         less than {@code toKey}
   1.172 +     * @throws ClassCastException if {@code toKey} is not compatible
   1.173 +     *         with this map's comparator (or, if the map has no comparator,
   1.174 +     *         if {@code toKey} does not implement {@link Comparable}).
   1.175 +     *         Implementations may, but are not required to, throw this
   1.176 +     *         exception if {@code toKey} cannot be compared to keys
   1.177 +     *         currently in the map.
   1.178 +     * @throws NullPointerException if {@code toKey} is null and
   1.179 +     *         this map does not permit null keys
   1.180 +     * @throws IllegalArgumentException if this map itself has a
   1.181 +     *         restricted range, and {@code toKey} lies outside the
   1.182 +     *         bounds of the range
   1.183 +     */
   1.184 +    SortedMap<K,V> headMap(K toKey);
   1.185 +
   1.186 +    /**
   1.187 +     * Returns a view of the portion of this map whose keys are
   1.188 +     * greater than or equal to {@code fromKey}.  The returned map is
   1.189 +     * backed by this map, so changes in the returned map are
   1.190 +     * reflected in this map, and vice-versa.  The returned map
   1.191 +     * supports all optional map operations that this map supports.
   1.192 +     *
   1.193 +     * <p>The returned map will throw an {@code IllegalArgumentException}
   1.194 +     * on an attempt to insert a key outside its range.
   1.195 +     *
   1.196 +     * @param fromKey low endpoint (inclusive) of the keys in the returned map
   1.197 +     * @return a view of the portion of this map whose keys are greater
   1.198 +     *         than or equal to {@code fromKey}
   1.199 +     * @throws ClassCastException if {@code fromKey} is not compatible
   1.200 +     *         with this map's comparator (or, if the map has no comparator,
   1.201 +     *         if {@code fromKey} does not implement {@link Comparable}).
   1.202 +     *         Implementations may, but are not required to, throw this
   1.203 +     *         exception if {@code fromKey} cannot be compared to keys
   1.204 +     *         currently in the map.
   1.205 +     * @throws NullPointerException if {@code fromKey} is null and
   1.206 +     *         this map does not permit null keys
   1.207 +     * @throws IllegalArgumentException if this map itself has a
   1.208 +     *         restricted range, and {@code fromKey} lies outside the
   1.209 +     *         bounds of the range
   1.210 +     */
   1.211 +    SortedMap<K,V> tailMap(K fromKey);
   1.212 +
   1.213 +    /**
   1.214 +     * Returns the first (lowest) key currently in this map.
   1.215 +     *
   1.216 +     * @return the first (lowest) key currently in this map
   1.217 +     * @throws NoSuchElementException if this map is empty
   1.218 +     */
   1.219 +    K firstKey();
   1.220 +
   1.221 +    /**
   1.222 +     * Returns the last (highest) key currently in this map.
   1.223 +     *
   1.224 +     * @return the last (highest) key currently in this map
   1.225 +     * @throws NoSuchElementException if this map is empty
   1.226 +     */
   1.227 +    K lastKey();
   1.228 +
   1.229 +    /**
   1.230 +     * Returns a {@link Set} view of the keys contained in this map.
   1.231 +     * The set's iterator returns the keys in ascending order.
   1.232 +     * The set is backed by the map, so changes to the map are
   1.233 +     * reflected in the set, and vice-versa.  If the map is modified
   1.234 +     * while an iteration over the set is in progress (except through
   1.235 +     * the iterator's own {@code remove} operation), the results of
   1.236 +     * the iteration are undefined.  The set supports element removal,
   1.237 +     * which removes the corresponding mapping from the map, via the
   1.238 +     * {@code Iterator.remove}, {@code Set.remove},
   1.239 +     * {@code removeAll}, {@code retainAll}, and {@code clear}
   1.240 +     * operations.  It does not support the {@code add} or {@code addAll}
   1.241 +     * operations.
   1.242 +     *
   1.243 +     * @return a set view of the keys contained in this map, sorted in
   1.244 +     *         ascending order
   1.245 +     */
   1.246 +    Set<K> keySet();
   1.247 +
   1.248 +    /**
   1.249 +     * Returns a {@link Collection} view of the values contained in this map.
   1.250 +     * The collection's iterator returns the values in ascending order
   1.251 +     * of the corresponding keys.
   1.252 +     * The collection is backed by the map, so changes to the map are
   1.253 +     * reflected in the collection, and vice-versa.  If the map is
   1.254 +     * modified while an iteration over the collection is in progress
   1.255 +     * (except through the iterator's own {@code remove} operation),
   1.256 +     * the results of the iteration are undefined.  The collection
   1.257 +     * supports element removal, which removes the corresponding
   1.258 +     * mapping from the map, via the {@code Iterator.remove},
   1.259 +     * {@code Collection.remove}, {@code removeAll},
   1.260 +     * {@code retainAll} and {@code clear} operations.  It does not
   1.261 +     * support the {@code add} or {@code addAll} operations.
   1.262 +     *
   1.263 +     * @return a collection view of the values contained in this map,
   1.264 +     *         sorted in ascending key order
   1.265 +     */
   1.266 +    Collection<V> values();
   1.267 +
   1.268 +    /**
   1.269 +     * Returns a {@link Set} view of the mappings contained in this map.
   1.270 +     * The set's iterator returns the entries in ascending key order.
   1.271 +     * The set is backed by the map, so changes to the map are
   1.272 +     * reflected in the set, and vice-versa.  If the map is modified
   1.273 +     * while an iteration over the set is in progress (except through
   1.274 +     * the iterator's own {@code remove} operation, or through the
   1.275 +     * {@code setValue} operation on a map entry returned by the
   1.276 +     * iterator) the results of the iteration are undefined.  The set
   1.277 +     * supports element removal, which removes the corresponding
   1.278 +     * mapping from the map, via the {@code Iterator.remove},
   1.279 +     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
   1.280 +     * {@code clear} operations.  It does not support the
   1.281 +     * {@code add} or {@code addAll} operations.
   1.282 +     *
   1.283 +     * @return a set view of the mappings contained in this map,
   1.284 +     *         sorted in ascending key order
   1.285 +     */
   1.286 +    Set<Map.Entry<K, V>> entrySet();
   1.287 +}