src/share/classes/java/util/TreeMap.java
Print this page
rev 4788 : Fix bunch of generics warnings
*** 305,315 ****
* permit null keys
*/
public void putAll(Map<? extends K, ? extends V> map) {
int mapSize = map.size();
if (size==0 && mapSize!=0 && map instanceof SortedMap) {
! Comparator c = ((SortedMap)map).comparator();
if (c == comparator || (c != null && c.equals(comparator))) {
++modCount;
try {
buildFromSorted(mapSize, map.entrySet().iterator(),
null, null);
--- 305,315 ----
* permit null keys
*/
public void putAll(Map<? extends K, ? extends V> map) {
int mapSize = map.size();
if (size==0 && mapSize!=0 && map instanceof SortedMap) {
! Comparator<?> c = ((SortedMap<?,?>)map).comparator();
if (c == comparator || (c != null && c.equals(comparator))) {
++modCount;
try {
buildFromSorted(mapSize, map.entrySet().iterator(),
null, null);
*** 338,347 ****
--- 338,348 ----
// Offload comparator-based version for sake of performance
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
+ @SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root;
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0)
*** 359,368 ****
--- 360,370 ----
* for performance. (This is not worth doing for most methods,
* that are less dependent on comparator performance, but is
* worthwhile here.)
*/
final Entry<K,V> getEntryUsingComparator(Object key) {
+ @SuppressWarnings("unchecked")
K k = (K) key;
Comparator<? super K> cpr = comparator;
if (cpr != null) {
Entry<K,V> p = root;
while (p != null) {
*** 552,561 ****
--- 554,564 ----
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
+ @SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
*** 616,628 ****
* values themselves are not cloned.)
*
* @return a shallow copy of this map
*/
public Object clone() {
! TreeMap<K,V> clone = null;
try {
! clone = (TreeMap<K,V>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
// Put clone into "virgin" state (except for comparator)
--- 619,631 ----
* values themselves are not cloned.)
*
* @return a shallow copy of this map
*/
public Object clone() {
! TreeMap<?,?> clone;
try {
! clone = (TreeMap<?,?>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
// Put clone into "virgin" state (except for comparator)
*** 801,811 ****
/**
* @since 1.6
*/
public NavigableSet<K> navigableKeySet() {
KeySet<K> nks = navigableKeySet;
! return (nks != null) ? nks : (navigableKeySet = new KeySet(this));
}
/**
* @since 1.6
*/
--- 804,814 ----
/**
* @since 1.6
*/
public NavigableSet<K> navigableKeySet() {
KeySet<K> nks = navigableKeySet;
! return (nks != null) ? nks : (navigableKeySet = new KeySet<>(this));
}
/**
* @since 1.6
*/
*** 857,867 ****
* @since 1.6
*/
public NavigableMap<K, V> descendingMap() {
NavigableMap<K, V> km = descendingMap;
return (km != null) ? km :
! (descendingMap = new DescendingSubMap(this,
true, null, true,
true, null, true));
}
/**
--- 860,870 ----
* @since 1.6
*/
public NavigableMap<K, V> descendingMap() {
NavigableMap<K, V> km = descendingMap;
return (km != null) ? km :
! (descendingMap = new DescendingSubMap<>(this,
true, null, true,
true, null, true));
}
/**
*** 872,882 ****
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) {
! return new AscendingSubMap(this,
false, fromKey, fromInclusive,
false, toKey, toInclusive);
}
/**
--- 875,885 ----
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) {
! return new AscendingSubMap<>(this,
false, fromKey, fromInclusive,
false, toKey, toInclusive);
}
/**
*** 886,896 ****
* does not permit null keys
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
! return new AscendingSubMap(this,
true, null, true,
false, toKey, inclusive);
}
/**
--- 889,899 ----
* does not permit null keys
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
! return new AscendingSubMap<>(this,
true, null, true,
false, toKey, inclusive);
}
/**
*** 900,910 ****
* does not permit null keys
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
! return new AscendingSubMap(this,
false, fromKey, inclusive,
true, null, true);
}
/**
--- 903,913 ----
* does not permit null keys
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
! return new AscendingSubMap<>(this,
false, fromKey, inclusive,
true, null, true);
}
/**
*** 976,996 ****
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
! V value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
return p != null && valEquals(p.getValue(), value);
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
! V value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
if (p != null && valEquals(p.getValue(), value)) {
deleteEntry(p);
return true;
}
--- 979,999 ----
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
! Object value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
return p != null && valEquals(p.getValue(), value);
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
! Object value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
if (p != null && valEquals(p.getValue(), value)) {
deleteEntry(p);
return true;
}
*** 1021,1045 ****
Iterator<K> descendingKeyIterator() {
return new DescendingKeyIterator(getLastEntry());
}
static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {
! private final NavigableMap<E, Object> m;
! KeySet(NavigableMap<E,Object> map) { m = map; }
public Iterator<E> iterator() {
if (m instanceof TreeMap)
! return ((TreeMap<E,Object>)m).keyIterator();
else
! return (Iterator<E>)(((TreeMap.NavigableSubMap)m).keyIterator());
}
public Iterator<E> descendingIterator() {
if (m instanceof TreeMap)
! return ((TreeMap<E,Object>)m).descendingKeyIterator();
else
! return (Iterator<E>)(((TreeMap.NavigableSubMap)m).descendingKeyIterator());
}
public int size() { return m.size(); }
public boolean isEmpty() { return m.isEmpty(); }
public boolean contains(Object o) { return m.containsKey(o); }
--- 1024,1048 ----
Iterator<K> descendingKeyIterator() {
return new DescendingKeyIterator(getLastEntry());
}
static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {
! private final NavigableMap<E, ?> m;
! KeySet(NavigableMap<E,?> map) { m = map; }
public Iterator<E> iterator() {
if (m instanceof TreeMap)
! return ((TreeMap<E,?>)m).keyIterator();
else
! return ((TreeMap.NavigableSubMap<E,?>)m).keyIterator();
}
public Iterator<E> descendingIterator() {
if (m instanceof TreeMap)
! return ((TreeMap<E,?>)m).descendingKeyIterator();
else
! return ((TreeMap.NavigableSubMap<E,?>)m).descendingKeyIterator();
}
public int size() { return m.size(); }
public boolean isEmpty() { return m.isEmpty(); }
public boolean contains(Object o) { return m.containsKey(o); }
*** 1050,1064 ****
public E higher(E e) { return m.higherKey(e); }
public E first() { return m.firstKey(); }
public E last() { return m.lastKey(); }
public Comparator<? super E> comparator() { return m.comparator(); }
public E pollFirst() {
! Map.Entry<E,Object> e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
public E pollLast() {
! Map.Entry<E,Object> e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
public boolean remove(Object o) {
int oldSize = size();
m.remove(o);
--- 1053,1067 ----
public E higher(E e) { return m.higherKey(e); }
public E first() { return m.firstKey(); }
public E last() { return m.lastKey(); }
public Comparator<? super E> comparator() { return m.comparator(); }
public E pollFirst() {
! Map.Entry<E,?> e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
public E pollLast() {
! Map.Entry<E,?> e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
public boolean remove(Object o) {
int oldSize = size();
m.remove(o);
*** 1083,1093 ****
}
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
public NavigableSet<E> descendingSet() {
! return new KeySet(m.descendingMap());
}
}
/**
* Base class for TreeMap Iterators
--- 1086,1096 ----
}
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
public NavigableSet<E> descendingSet() {
! return new KeySet<>(m.descendingMap());
}
}
/**
* Base class for TreeMap Iterators
*** 1182,1191 ****
--- 1185,1195 ----
// Little utilities
/**
* Compares two keys using the correct comparison method for this TreeMap.
*/
+ @SuppressWarnings("unchecked")
final int compare(Object k1, Object k2) {
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
*** 1486,1496 ****
transient KeySet<K> navigableKeySetView = null;
public final NavigableSet<K> navigableKeySet() {
KeySet<K> nksv = navigableKeySetView;
return (nksv != null) ? nksv :
! (navigableKeySetView = new TreeMap.KeySet(this));
}
public final Set<K> keySet() {
return navigableKeySet();
}
--- 1490,1500 ----
transient KeySet<K> navigableKeySetView = null;
public final NavigableSet<K> navigableKeySet() {
KeySet<K> nksv = navigableKeySetView;
return (nksv != null) ? nksv :
! (navigableKeySetView = new TreeMap.KeySet<>(this));
}
public final Set<K> keySet() {
return navigableKeySet();
}
*** 1520,1530 ****
if (fromStart && toEnd)
return m.size();
if (size == -1 || sizeModCount != m.modCount) {
sizeModCount = m.modCount;
size = 0;
! Iterator i = iterator();
while (i.hasNext()) {
size++;
i.next();
}
}
--- 1524,1534 ----
if (fromStart && toEnd)
return m.size();
if (size == -1 || sizeModCount != m.modCount) {
sizeModCount = m.modCount;
size = 0;
! Iterator<?> i = iterator();
while (i.hasNext()) {
size++;
i.next();
}
}
*** 1537,1560 ****
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
! K key = entry.getKey();
if (!inRange(key))
return false;
! TreeMap.Entry node = m.getEntry(key);
return node != null &&
valEquals(node.getValue(), entry.getValue());
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
! K key = entry.getKey();
if (!inRange(key))
return false;
TreeMap.Entry<K,V> node = m.getEntry(key);
if (node!=null && valEquals(node.getValue(),
entry.getValue())) {
--- 1541,1564 ----
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
! Object key = entry.getKey();
if (!inRange(key))
return false;
! TreeMap.Entry<?,?> node = m.getEntry(key);
return node != null &&
valEquals(node.getValue(), entry.getValue());
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
! Object key = entry.getKey();
if (!inRange(key))
return false;
TreeMap.Entry<K,V> node = m.getEntry(key);
if (node!=null && valEquals(node.getValue(),
entry.getValue())) {
*** 1707,1742 ****
K toKey, boolean toInclusive) {
if (!inRange(fromKey, fromInclusive))
throw new IllegalArgumentException("fromKey out of range");
if (!inRange(toKey, toInclusive))
throw new IllegalArgumentException("toKey out of range");
! return new AscendingSubMap(m,
false, fromKey, fromInclusive,
false, toKey, toInclusive);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
if (!inRange(toKey, inclusive))
throw new IllegalArgumentException("toKey out of range");
! return new AscendingSubMap(m,
fromStart, lo, loInclusive,
false, toKey, inclusive);
}
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
if (!inRange(fromKey, inclusive))
throw new IllegalArgumentException("fromKey out of range");
! return new AscendingSubMap(m,
false, fromKey, inclusive,
toEnd, hi, hiInclusive);
}
public NavigableMap<K,V> descendingMap() {
NavigableMap<K,V> mv = descendingMapView;
return (mv != null) ? mv :
(descendingMapView =
! new DescendingSubMap(m,
fromStart, lo, loInclusive,
toEnd, hi, hiInclusive));
}
Iterator<K> keyIterator() {
--- 1711,1746 ----
K toKey, boolean toInclusive) {
if (!inRange(fromKey, fromInclusive))
throw new IllegalArgumentException("fromKey out of range");
if (!inRange(toKey, toInclusive))
throw new IllegalArgumentException("toKey out of range");
! return new AscendingSubMap<>(m,
false, fromKey, fromInclusive,
false, toKey, toInclusive);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
if (!inRange(toKey, inclusive))
throw new IllegalArgumentException("toKey out of range");
! return new AscendingSubMap<>(m,
fromStart, lo, loInclusive,
false, toKey, inclusive);
}
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
if (!inRange(fromKey, inclusive))
throw new IllegalArgumentException("fromKey out of range");
! return new AscendingSubMap<>(m,
false, fromKey, inclusive,
toEnd, hi, hiInclusive);
}
public NavigableMap<K,V> descendingMap() {
NavigableMap<K,V> mv = descendingMapView;
return (mv != null) ? mv :
(descendingMapView =
! new DescendingSubMap<>(m,
fromStart, lo, loInclusive,
toEnd, hi, hiInclusive));
}
Iterator<K> keyIterator() {
*** 1788,1823 ****
K toKey, boolean toInclusive) {
if (!inRange(fromKey, fromInclusive))
throw new IllegalArgumentException("fromKey out of range");
if (!inRange(toKey, toInclusive))
throw new IllegalArgumentException("toKey out of range");
! return new DescendingSubMap(m,
false, toKey, toInclusive,
false, fromKey, fromInclusive);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
if (!inRange(toKey, inclusive))
throw new IllegalArgumentException("toKey out of range");
! return new DescendingSubMap(m,
false, toKey, inclusive,
toEnd, hi, hiInclusive);
}
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
if (!inRange(fromKey, inclusive))
throw new IllegalArgumentException("fromKey out of range");
! return new DescendingSubMap(m,
fromStart, lo, loInclusive,
false, fromKey, inclusive);
}
public NavigableMap<K,V> descendingMap() {
NavigableMap<K,V> mv = descendingMapView;
return (mv != null) ? mv :
(descendingMapView =
! new AscendingSubMap(m,
fromStart, lo, loInclusive,
toEnd, hi, hiInclusive));
}
Iterator<K> keyIterator() {
--- 1792,1827 ----
K toKey, boolean toInclusive) {
if (!inRange(fromKey, fromInclusive))
throw new IllegalArgumentException("fromKey out of range");
if (!inRange(toKey, toInclusive))
throw new IllegalArgumentException("toKey out of range");
! return new DescendingSubMap<>(m,
false, toKey, toInclusive,
false, fromKey, fromInclusive);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
if (!inRange(toKey, inclusive))
throw new IllegalArgumentException("toKey out of range");
! return new DescendingSubMap<>(m,
false, toKey, inclusive,
toEnd, hi, hiInclusive);
}
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
if (!inRange(fromKey, inclusive))
throw new IllegalArgumentException("fromKey out of range");
! return new DescendingSubMap<>(m,
fromStart, lo, loInclusive,
false, fromKey, inclusive);
}
public NavigableMap<K,V> descendingMap() {
NavigableMap<K,V> mv = descendingMapView;
return (mv != null) ? mv :
(descendingMapView =
! new AscendingSubMap<>(m,
fromStart, lo, loInclusive,
toEnd, hi, hiInclusive));
}
Iterator<K> keyIterator() {
*** 1860,1870 ****
implements SortedMap<K,V>, java.io.Serializable {
private static final long serialVersionUID = -6520786458950516097L;
private boolean fromStart = false, toEnd = false;
private K fromKey, toKey;
private Object readResolve() {
! return new AscendingSubMap(TreeMap.this,
fromStart, fromKey, true,
toEnd, toKey, false);
}
public Set<Map.Entry<K,V>> entrySet() { throw new InternalError(); }
public K lastKey() { throw new InternalError(); }
--- 1864,1874 ----
implements SortedMap<K,V>, java.io.Serializable {
private static final long serialVersionUID = -6520786458950516097L;
private boolean fromStart = false, toEnd = false;
private K fromKey, toKey;
private Object readResolve() {
! return new AscendingSubMap<>(TreeMap.this,
fromStart, fromKey, true,
toEnd, toKey, false);
}
public Set<Map.Entry<K,V>> entrySet() { throw new InternalError(); }
public K lastKey() { throw new InternalError(); }
*** 2329,2344 ****
* possibly values read from this stream in serialized form.
* Exactly one of it and str should be non-null.
* @param defaultVal if non-null, this default value is used for
* each value in the map. If null, each value is read from
* iterator or stream, as described above.
! * @throws IOException propagated from stream reads. This cannot
* occur if str is null.
* @throws ClassNotFoundException propagated from readObject.
* This cannot occur if str is null.
*/
! private void buildFromSorted(int size, Iterator it,
java.io.ObjectInputStream str,
V defaultVal)
throws java.io.IOException, ClassNotFoundException {
this.size = size;
root = buildFromSorted(0, 0, size-1, computeRedLevel(size),
--- 2333,2348 ----
* possibly values read from this stream in serialized form.
* Exactly one of it and str should be non-null.
* @param defaultVal if non-null, this default value is used for
* each value in the map. If null, each value is read from
* iterator or stream, as described above.
! * @throws java.io.IOException propagated from stream reads. This cannot
* occur if str is null.
* @throws ClassNotFoundException propagated from readObject.
* This cannot occur if str is null.
*/
! private void buildFromSorted(int size, Iterator<?> it,
java.io.ObjectInputStream str,
V defaultVal)
throws java.io.IOException, ClassNotFoundException {
this.size = size;
root = buildFromSorted(0, 0, size-1, computeRedLevel(size),
*** 2357,2369 ****
* @param hi the last element index of this subtree. Initial should be
* size-1.
* @param redLevel the level at which nodes should be red.
* Must be equal to computeRedLevel for tree of this size.
*/
private final Entry<K,V> buildFromSorted(int level, int lo, int hi,
int redLevel,
! Iterator it,
java.io.ObjectInputStream str,
V defaultVal)
throws java.io.IOException, ClassNotFoundException {
/*
* Strategy: The root is the middlemost element. To get to it, we
--- 2361,2374 ----
* @param hi the last element index of this subtree. Initial should be
* size-1.
* @param redLevel the level at which nodes should be red.
* Must be equal to computeRedLevel for tree of this size.
*/
+ @SuppressWarnings("unchecked")
private final Entry<K,V> buildFromSorted(int level, int lo, int hi,
int redLevel,
! Iterator<?> it,
java.io.ObjectInputStream str,
V defaultVal)
throws java.io.IOException, ClassNotFoundException {
/*
* Strategy: The root is the middlemost element. To get to it, we
*** 2389,2401 ****
// extract key and/or value from iterator or stream
K key;
V value;
if (it != null) {
if (defaultVal==null) {
! Map.Entry<K,V> entry = (Map.Entry<K,V>)it.next();
! key = entry.getKey();
! value = entry.getValue();
} else {
key = (K)it.next();
value = defaultVal;
}
} else { // use stream
--- 2394,2406 ----
// extract key and/or value from iterator or stream
K key;
V value;
if (it != null) {
if (defaultVal==null) {
! Map.Entry<?,?> entry = (Map.Entry<?,?>)it.next();
! key = (K)entry.getKey();
! value = (V)entry.getValue();
} else {
key = (K)it.next();
value = defaultVal;
}
} else { // use stream