1078 |
|
/** |
1079 |
|
* Returns a view of the portion of this map whose keys range from |
1080 |
|
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If |
1081 |
< |
* <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map |
1082 |
< |
* is empty.) The returned sorted map is backed by this map, so changes |
1083 |
< |
* in the returned sorted map are reflected in this map, and vice-versa. |
1084 |
< |
* The returned sorted map supports all optional map operations.<p> |
1081 |
> |
* <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned |
1082 |
> |
* navigable map is empty.) The returned navigable map is backed |
1083 |
> |
* by this map, so changes in the returned navigable map are |
1084 |
> |
* reflected in this map, and vice-versa. The returned navigable |
1085 |
> |
* map supports all optional map operations.<p> |
1086 |
|
* |
1087 |
< |
* The sorted map returned by this method will throw an |
1087 |
> |
* The navigable map returned by this method will throw an |
1088 |
|
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1089 |
|
* less than <tt>fromKey</tt> or greater than or equal to |
1090 |
|
* <tt>toKey</tt>.<p> |
1092 |
|
* Note: this method always returns a <i>half-open range</i> (which |
1093 |
|
* includes its low endpoint but not its high endpoint). If you need a |
1094 |
|
* <i>closed range</i> (which includes both endpoints), and the key type |
1095 |
< |
* allows for calculation of the successor a given key, merely request the |
1095 |
> |
* allows for calculation of the successor of a given key, merely request the |
1096 |
|
* subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>. |
1097 |
< |
* For example, suppose that <tt>m</tt> is a sorted map whose keys are |
1097 |
> |
* For example, suppose that <tt>m</tt> is a navigable map whose keys are |
1098 |
|
* strings. The following idiom obtains a view containing all of the |
1099 |
|
* key-value mappings in <tt>m</tt> whose keys are between <tt>low</tt> |
1100 |
|
* and <tt>high</tt>, inclusive: |
1101 |
< |
* <pre> NavigableMap sub = m.submap(low, high+"\0");</pre> |
1101 |
> |
* <pre> NavigableMap sub = m.navigableSubMap(low, high+"\0");</pre> |
1102 |
|
* A similar technique can be used to generate an <i>open range</i> (which |
1103 |
|
* contains neither endpoint). The following idiom obtains a view |
1104 |
|
* containing all of the key-value mappings in <tt>m</tt> whose keys are |
1105 |
|
* between <tt>low</tt> and <tt>high</tt>, exclusive: |
1106 |
< |
* <pre> NavigableMap sub = m.subMap(low+"\0", high);</pre> |
1106 |
> |
* <pre> NavigableMap sub = m.navigableSubMap(low+"\0", high);</pre> |
1107 |
|
* |
1108 |
|
* @param fromKey low endpoint (inclusive) of the subMap. |
1109 |
|
* @param toKey high endpoint (exclusive) of the subMap. |
1120 |
|
* <tt>null</tt> and this map uses natural order, or its |
1121 |
|
* comparator does not tolerate <tt>null</tt> keys. |
1122 |
|
*/ |
1123 |
< |
public NavigableMap<K,V> subMap(K fromKey, K toKey) { |
1123 |
> |
public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) { |
1124 |
|
return new SubMap(fromKey, toKey); |
1125 |
|
} |
1126 |
|
|
1127 |
+ |
|
1128 |
|
/** |
1129 |
|
* Returns a view of the portion of this map whose keys are strictly less |
1130 |
< |
* than <tt>toKey</tt>. The returned sorted map is backed by this map, so |
1131 |
< |
* changes in the returned sorted map are reflected in this map, and |
1132 |
< |
* vice-versa. The returned sorted map supports all optional map |
1130 |
> |
* than <tt>toKey</tt>. The returned navigable map is backed by this map, so |
1131 |
> |
* changes in the returned navigable map are reflected in this map, and |
1132 |
> |
* vice-versa. The returned navigable map supports all optional map |
1133 |
|
* operations.<p> |
1134 |
|
* |
1135 |
< |
* The sorted map returned by this method will throw an |
1135 |
> |
* The navigable map returned by this method will throw an |
1136 |
|
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1137 |
|
* greater than or equal to <tt>toKey</tt>.<p> |
1138 |
|
* |
1139 |
|
* Note: this method always returns a view that does not contain its |
1140 |
|
* (high) endpoint. If you need a view that does contain this endpoint, |
1141 |
< |
* and the key type allows for calculation of the successor a given key, |
1141 |
> |
* and the key type allows for calculation of the successor of a given key, |
1142 |
|
* merely request a headMap bounded by <tt>successor(highEndpoint)</tt>. |
1143 |
< |
* For example, suppose that suppose that <tt>m</tt> is a sorted map whose |
1143 |
> |
* For example, suppose that suppose that <tt>m</tt> is a navigable map whose |
1144 |
|
* keys are strings. The following idiom obtains a view containing all of |
1145 |
|
* the key-value mappings in <tt>m</tt> whose keys are less than or equal |
1146 |
|
* to <tt>high</tt>: |
1162 |
|
* this map uses natural order, or its comparator does not |
1163 |
|
* tolerate <tt>null</tt> keys. |
1164 |
|
*/ |
1165 |
< |
public NavigableMap<K,V> headMap(K toKey) { |
1165 |
> |
public NavigableMap<K,V> navigableHeadMap(K toKey) { |
1166 |
|
return new SubMap(toKey, true); |
1167 |
|
} |
1168 |
|
|
1169 |
|
/** |
1170 |
|
* Returns a view of the portion of this map whose keys are greater than |
1171 |
< |
* or equal to <tt>fromKey</tt>. The returned sorted map is backed by |
1172 |
< |
* this map, so changes in the returned sorted map are reflected in this |
1173 |
< |
* map, and vice-versa. The returned sorted map supports all optional map |
1171 |
> |
* or equal to <tt>fromKey</tt>. The returned navigable map is backed by |
1172 |
> |
* this map, so changes in the returned navigable map are reflected in this |
1173 |
> |
* map, and vice-versa. The returned navigable map supports all optional map |
1174 |
|
* operations.<p> |
1175 |
|
* |
1176 |
< |
* The sorted map returned by this method will throw an |
1176 |
> |
* The navigable map returned by this method will throw an |
1177 |
|
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1178 |
|
* less than <tt>fromKey</tt>.<p> |
1179 |
|
* |
1180 |
|
* Note: this method always returns a view that contains its (low) |
1181 |
|
* endpoint. If you need a view that does not contain this endpoint, and |
1182 |
< |
* the element type allows for calculation of the successor a given value, |
1182 |
> |
* the element type allows for calculation of the successor of a given value, |
1183 |
|
* merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>. |
1184 |
< |
* For example, suppose that <tt>m</tt> is a sorted map whose keys |
1184 |
> |
* For example, suppose that <tt>m</tt> is a navigable map whose keys |
1185 |
|
* are strings. The following idiom obtains a view containing |
1186 |
|
* all of the key-value mappings in <tt>m</tt> whose keys are strictly |
1187 |
|
* greater than <tt>low</tt>: <pre> |
1201 |
|
* this map uses natural order, or its comparator does not |
1202 |
|
* tolerate <tt>null</tt> keys. |
1203 |
|
*/ |
1204 |
< |
public NavigableMap<K,V> tailMap(K fromKey) { |
1204 |
> |
public NavigableMap<K,V> navigableTailMap(K fromKey) { |
1205 |
> |
return new SubMap(fromKey, false); |
1206 |
> |
} |
1207 |
> |
|
1208 |
> |
/** |
1209 |
> |
* Equivalent to <tt>navigableSubMap</tt> but with a return |
1210 |
> |
* type conforming to the <tt>SortedMap</tt> interface. |
1211 |
> |
* @param fromKey low endpoint (inclusive) of the subMap. |
1212 |
> |
* @param toKey high endpoint (exclusive) of the subMap. |
1213 |
> |
* |
1214 |
> |
* @return a view of the portion of this map whose keys range from |
1215 |
> |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. |
1216 |
> |
* |
1217 |
> |
* @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt> |
1218 |
> |
* cannot be compared to one another using this map's comparator |
1219 |
> |
* (or, if the map has no comparator, using natural ordering). |
1220 |
> |
* @throws IllegalArgumentException if <tt>fromKey</tt> is greater than |
1221 |
> |
* <tt>toKey</tt>. |
1222 |
> |
* @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is |
1223 |
> |
* <tt>null</tt> and this map uses natural order, or its |
1224 |
> |
* comparator does not tolerate <tt>null</tt> keys. |
1225 |
> |
*/ |
1226 |
> |
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
1227 |
> |
return new SubMap(fromKey, toKey); |
1228 |
> |
} |
1229 |
> |
|
1230 |
> |
|
1231 |
> |
/** |
1232 |
> |
* Equivalent to <tt>navigableHeadMap</tt> but with a return |
1233 |
> |
* type conforming to the <tt>SortedMap</tt> interface. |
1234 |
> |
* |
1235 |
> |
* @param toKey high endpoint (exclusive) of the headMap. |
1236 |
> |
* @return a view of the portion of this map whose keys are strictly |
1237 |
> |
* less than <tt>toKey</tt>. |
1238 |
> |
* |
1239 |
> |
* @throws ClassCastException if <tt>toKey</tt> is not compatible |
1240 |
> |
* with this map's comparator (or, if the map has no comparator, |
1241 |
> |
* if <tt>toKey</tt> does not implement <tt>Comparable</tt>). |
1242 |
> |
* @throws IllegalArgumentException if this map is itself a subMap, |
1243 |
> |
* headMap, or tailMap, and <tt>toKey</tt> is not within the |
1244 |
> |
* specified range of the subMap, headMap, or tailMap. |
1245 |
> |
* @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and |
1246 |
> |
* this map uses natural order, or its comparator does not |
1247 |
> |
* tolerate <tt>null</tt> keys. |
1248 |
> |
*/ |
1249 |
> |
public SortedMap<K,V> headMap(K toKey) { |
1250 |
> |
return new SubMap(toKey, true); |
1251 |
> |
} |
1252 |
> |
|
1253 |
> |
/** |
1254 |
> |
* Equivalent to <tt>navigableTailMap</tt> but with a return |
1255 |
> |
* type conforming to the <tt>SortedMap</tt> interface. |
1256 |
> |
* |
1257 |
> |
* @param fromKey low endpoint (inclusive) of the tailMap. |
1258 |
> |
* @return a view of the portion of this map whose keys are greater |
1259 |
> |
* than or equal to <tt>fromKey</tt>. |
1260 |
> |
* @throws ClassCastException if <tt>fromKey</tt> is not compatible |
1261 |
> |
* with this map's comparator (or, if the map has no comparator, |
1262 |
> |
* if <tt>fromKey</tt> does not implement <tt>Comparable</tt>). |
1263 |
> |
* @throws IllegalArgumentException if this map is itself a subMap, |
1264 |
> |
* headMap, or tailMap, and <tt>fromKey</tt> is not within the |
1265 |
> |
* specified range of the subMap, headMap, or tailMap. |
1266 |
> |
* @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and |
1267 |
> |
* this map uses natural order, or its comparator does not |
1268 |
> |
* tolerate <tt>null</tt> keys. |
1269 |
> |
*/ |
1270 |
> |
public SortedMap<K,V> tailMap(K fromKey) { |
1271 |
|
return new SubMap(fromKey, false); |
1272 |
|
} |
1273 |
|
|
1565 |
|
} |
1566 |
|
|
1567 |
|
|
1568 |
< |
public NavigableMap<K,V> subMap(K fromKey, K toKey) { |
1568 |
> |
public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) { |
1569 |
|
if (!inRange2(fromKey)) |
1570 |
|
throw new IllegalArgumentException("fromKey out of range"); |
1571 |
|
if (!inRange2(toKey)) |
1573 |
|
return new SubMap(fromKey, toKey); |
1574 |
|
} |
1575 |
|
|
1576 |
< |
public NavigableMap<K,V> headMap(K toKey) { |
1576 |
> |
public NavigableMap<K,V> navigableHeadMap(K toKey) { |
1577 |
|
if (!inRange2(toKey)) |
1578 |
|
throw new IllegalArgumentException("toKey out of range"); |
1579 |
|
return new SubMap(fromStart, fromKey, false, toKey); |
1580 |
|
} |
1581 |
|
|
1582 |
< |
public NavigableMap<K,V> tailMap(K fromKey) { |
1582 |
> |
public NavigableMap<K,V> navigableTailMap(K fromKey) { |
1583 |
|
if (!inRange2(fromKey)) |
1584 |
|
throw new IllegalArgumentException("fromKey out of range"); |
1585 |
|
return new SubMap(false, fromKey, toEnd, toKey); |
1586 |
|
} |
1587 |
|
|
1588 |
+ |
|
1589 |
+ |
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
1590 |
+ |
return navigableSubMap(fromKey, toKey); |
1591 |
+ |
} |
1592 |
+ |
|
1593 |
+ |
public SortedMap<K,V> headMap(K toKey) { |
1594 |
+ |
return navigableHeadMap(toKey); |
1595 |
+ |
} |
1596 |
+ |
|
1597 |
+ |
public SortedMap<K,V> tailMap(K fromKey) { |
1598 |
+ |
return navigableTailMap(fromKey); |
1599 |
+ |
} |
1600 |
+ |
|
1601 |
|
private boolean inRange(K key) { |
1602 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1603 |
|
(toEnd || compare(key, toKey) < 0); |