6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
10 |
|
|
11 |
|
/** |
12 |
|
* A Red-Black tree based {@link NavigableMap} implementation. |
217 |
|
* specified value. More formally, returns <tt>true</tt> if and only if |
218 |
|
* this map contains at least one mapping to a value <tt>v</tt> such |
219 |
|
* that <tt>(value==null ? v==null : value.equals(v))</tt>. This |
220 |
< |
* operation requires time linear in the map size. |
220 |
> |
* operation will probably require time linear in the map size for |
221 |
> |
* most implementations. |
222 |
|
* |
223 |
|
* @param value value whose presence in this map is to be tested |
224 |
|
* @return <tt>true</tt> if a mapping to <tt>value</tt> exists; |
255 |
|
* <tt>null</tt> if the map contains no mapping for the key. A return |
256 |
|
* value of <tt>null</tt> does not <i>necessarily</i> indicate that the |
257 |
|
* map contains no mapping for the key; it's also possible that the map |
258 |
< |
* explicitly maps the key to <tt>null</tt>. The {@link #containsKey} |
259 |
< |
* operation may be used to distinguish these two cases. |
258 |
> |
* explicitly maps the key to <tt>null</tt>. The {@link #containsKey |
259 |
> |
* containsKey} operation may be used to distinguish these two cases. |
260 |
|
* |
261 |
|
* @param key key whose associated value is to be returned |
262 |
|
* @return the value to which this map maps the specified key, or |
521 |
|
|
522 |
|
/** |
523 |
|
* Associates the specified value with the specified key in this map. |
524 |
< |
* If the map previously contained a mapping for this key, the old |
524 |
> |
* If the map previously contained a mapping for the key, the old |
525 |
|
* value is replaced. |
526 |
|
* |
527 |
|
* @param key key with which the specified value is to be associated |
647 |
|
|
648 |
|
public Map.Entry<K,V> firstEntry() { |
649 |
|
Entry<K,V> e = getFirstEntry(); |
650 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
650 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
651 |
|
} |
652 |
|
|
653 |
|
public Map.Entry<K,V> lastEntry() { |
654 |
|
Entry<K,V> e = getLastEntry(); |
655 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
655 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
656 |
|
} |
657 |
|
|
658 |
|
public Map.Entry<K,V> pollFirstEntry() { |
659 |
|
Entry<K,V> p = getFirstEntry(); |
660 |
|
if (p == null) |
661 |
|
return null; |
662 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); |
662 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p); |
663 |
|
deleteEntry(p); |
664 |
|
return result; |
665 |
|
} |
668 |
|
Entry<K,V> p = getLastEntry(); |
669 |
|
if (p == null) |
670 |
|
return null; |
671 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); |
671 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p); |
672 |
|
deleteEntry(p); |
673 |
|
return result; |
674 |
|
} |
681 |
|
*/ |
682 |
|
public Map.Entry<K,V> lowerEntry(K key) { |
683 |
|
Entry<K,V> e = getLowerEntry(key); |
684 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
684 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
685 |
|
} |
686 |
|
|
687 |
|
/** |
703 |
|
*/ |
704 |
|
public Map.Entry<K,V> floorEntry(K key) { |
705 |
|
Entry<K,V> e = getFloorEntry(key); |
706 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
706 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
725 |
|
*/ |
726 |
|
public Map.Entry<K,V> ceilingEntry(K key) { |
727 |
|
Entry<K,V> e = getCeilingEntry(key); |
728 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
728 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
729 |
|
} |
730 |
|
|
731 |
|
/** |
747 |
|
*/ |
748 |
|
public Map.Entry<K,V> higherEntry(K key) { |
749 |
|
Entry<K,V> e = getHigherEntry(key); |
750 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
750 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
751 |
|
} |
752 |
|
|
753 |
|
/** |
782 |
|
* the iteration are undefined. The set supports element removal, |
783 |
|
* which removes the corresponding mapping from the map, via the |
784 |
|
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
785 |
< |
* <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> |
786 |
< |
* operations. It does not support the add or <tt>addAll</tt> |
785 |
> |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
786 |
> |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
787 |
|
* operations. |
788 |
|
*/ |
789 |
|
public Set<K> keySet() { |
828 |
|
* mapping from the map, via the <tt>Iterator.remove</tt>, |
829 |
|
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
830 |
|
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
831 |
< |
* support the add or <tt>addAll</tt> operations. |
831 |
> |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
832 |
|
*/ |
833 |
|
public Collection<V> values() { |
834 |
|
Collection<V> vs = values; |
1068 |
|
} |
1069 |
|
|
1070 |
|
public boolean containsKey(Object key) { |
1071 |
< |
return inRange((K) key) && TreeMap.this.containsKey(key); |
1071 |
> |
return inRange(key) && TreeMap.this.containsKey(key); |
1072 |
|
} |
1073 |
|
|
1074 |
|
public V get(Object key) { |
1075 |
< |
if (!inRange((K) key)) |
1075 |
> |
if (!inRange(key)) |
1076 |
|
return null; |
1077 |
|
return TreeMap.this.get(key); |
1078 |
|
} |
1084 |
|
} |
1085 |
|
|
1086 |
|
public V remove(Object key) { |
1087 |
< |
if (!inRange((K) key)) |
1087 |
> |
if (!inRange(key)) |
1088 |
|
return null; |
1089 |
|
return TreeMap.this.remove(key); |
1090 |
|
} |
1130 |
|
getFirstEntry() : getCeilingEntry(fromKey); |
1131 |
|
if (e == null || (!toEnd && compare(e.key, toKey) >= 0)) |
1132 |
|
return null; |
1133 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); |
1133 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1134 |
|
deleteEntry(e); |
1135 |
|
return result; |
1136 |
|
} |
1140 |
|
getLastEntry() : getLowerEntry(toKey); |
1141 |
|
if (e == null || (!fromStart && compare(e.key, fromKey) < 0)) |
1142 |
|
return null; |
1143 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); |
1143 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1144 |
|
deleteEntry(e); |
1145 |
|
return result; |
1146 |
|
} |
1155 |
|
|
1156 |
|
public Map.Entry<K,V> ceilingEntry(K key) { |
1157 |
|
TreeMap.Entry<K,V> e = subceiling(key); |
1158 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1158 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1159 |
|
} |
1160 |
|
|
1161 |
|
public K ceilingKey(K key) { |
1174 |
|
|
1175 |
|
public Map.Entry<K,V> higherEntry(K key) { |
1176 |
|
TreeMap.Entry<K,V> e = subhigher(key); |
1177 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1177 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1178 |
|
} |
1179 |
|
|
1180 |
|
public K higherKey(K key) { |
1192 |
|
|
1193 |
|
public Map.Entry<K,V> floorEntry(K key) { |
1194 |
|
TreeMap.Entry<K,V> e = subfloor(key); |
1195 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1195 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1196 |
|
} |
1197 |
|
|
1198 |
|
public K floorKey(K key) { |
1210 |
|
|
1211 |
|
public Map.Entry<K,V> lowerEntry(K key) { |
1212 |
|
TreeMap.Entry<K,V> e = sublower(key); |
1213 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1213 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1214 |
|
} |
1215 |
|
|
1216 |
|
public K lowerKey(K key) { |
1353 |
|
return navigableTailMap(fromKey); |
1354 |
|
} |
1355 |
|
|
1356 |
< |
private boolean inRange(K key) { |
1356 |
> |
private boolean inRange(Object key) { |
1357 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1358 |
|
(toEnd || compare(key, toKey) < 0); |
1359 |
|
} |
1360 |
|
|
1361 |
|
// This form allows the high endpoint (as well as all legit keys) |
1362 |
< |
private boolean inRange2(K key) { |
1362 |
> |
private boolean inRange2(Object key) { |
1363 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1364 |
|
(toEnd || compare(key, toKey) <= 0); |
1365 |
|
} |
1515 |
|
/** |
1516 |
|
* Compares two keys using the correct comparison method for this TreeMap. |
1517 |
|
*/ |
1518 |
< |
private int compare(K k1, K k2) { |
1519 |
< |
return comparator==null ? ((Comparable<? super K>)k1).compareTo(k2) |
1520 |
< |
: comparator.compare(k1, k2); |
1518 |
> |
private int compare(Object k1, Object k2) { |
1519 |
> |
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2) |
1520 |
> |
: comparator.compare((K)k1, (K)k2); |
1521 |
|
} |
1522 |
|
|
1523 |
|
/** |
1524 |
< |
* Test two values for equality. Differs from o1.equals(o2) only in |
1524 |
> |
* Test two values for equality. Differs from o1.equals(o2) only in |
1525 |
|
* that it copes with <tt>null</tt> o1 properly. |
1526 |
|
*/ |
1527 |
|
private static boolean valEquals(Object o1, Object o2) { |