--- jsr166/src/main/java/util/TreeMap.java 2007/05/20 07:54:01 1.43 +++ jsr166/src/main/java/util/TreeMap.java 2010/09/01 07:48:47 1.49 @@ -1,5 +1,5 @@ /* - * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. + * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -93,7 +93,6 @@ package java.util; * @param the type of mapped values * * @author Josh Bloch and Doug Lea - * @version %I%, %G% * @see Map * @see HashMap * @see Hashtable @@ -237,7 +236,7 @@ public class TreeMap * * @param value value whose presence in this map is to be tested * @return true if a mapping to value exists; - * false otherwise + * false otherwise * @since 1.2 */ public boolean containsValue(Object value) { @@ -309,14 +308,14 @@ public class TreeMap 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); - } catch (java.io.IOException cannotHappen) { - } catch (ClassNotFoundException cannotHappen) { - } - return; + ++modCount; + try { + buildFromSorted(mapSize, map.entrySet().iterator(), + null, null); + } catch (java.io.IOException cannotHappen) { + } catch (ClassNotFoundException cannotHappen) { + } + return; } } super.putAll(map); @@ -340,7 +339,7 @@ public class TreeMap return getEntryUsingComparator(key); if (key == null) throw new NullPointerException(); - Comparable k = (Comparable) key; + Comparable k = (Comparable) key; Entry p = root; while (p != null) { int cmp = k.compareTo(p.key); @@ -361,7 +360,7 @@ public class TreeMap * worthwhile here.) */ final Entry getEntryUsingComparator(Object key) { - K k = (K) key; + K k = (K) key; Comparator cpr = comparator; if (cpr != null) { Entry p = root; @@ -528,11 +527,11 @@ public class TreeMap public V put(K key, V value) { Entry t = root; if (t == null) { - // TBD: - // 5045147: (coll) Adding null to an empty TreeSet should - // throw NullPointerException - // - // compare(key, key); // type check + // TBD: + // 5045147: (coll) Adding null to an empty TreeSet should + // throw NullPointerException + // + // compare(key, key); // type check root = new Entry(key, value, null); size = 1; modCount++; @@ -1022,7 +1021,7 @@ public class TreeMap } Iterator descendingKeyIterator() { - return new DescendingKeyIterator(getFirstEntry()); + return new DescendingKeyIterator(getLastEntry()); } static final class KeySet extends AbstractSet implements NavigableSet { @@ -1069,14 +1068,14 @@ public class TreeMap } public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { - return new TreeSet(m.subMap(fromElement, fromInclusive, - toElement, toInclusive)); + return new KeySet(m.subMap(fromElement, fromInclusive, + toElement, toInclusive)); } public NavigableSet headSet(E toElement, boolean inclusive) { - return new TreeSet(m.headMap(toElement, inclusive)); + return new KeySet(m.headMap(toElement, inclusive)); } public NavigableSet tailSet(E fromElement, boolean inclusive) { - return new TreeSet(m.tailMap(fromElement, inclusive)); + return new KeySet(m.tailMap(fromElement, inclusive)); } public SortedSet subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); @@ -1088,7 +1087,7 @@ public class TreeMap return tailSet(fromElement, true); } public NavigableSet descendingSet() { - return new TreeSet(m.descendingMap()); + return new KeySet(m.descendingMap()); } } @@ -1110,23 +1109,25 @@ public class TreeMap return next != null; } - final Entry nextEntry() { - Entry e = lastReturned = next; + final Entry nextEntry() { + Entry e = next; if (e == null) throw new NoSuchElementException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); next = successor(e); + lastReturned = e; return e; } final Entry prevEntry() { - Entry e = lastReturned= next; + Entry e = next; if (e == null) throw new NoSuchElementException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); next = predecessor(e); + lastReturned = e; return e; } @@ -1316,7 +1317,7 @@ public class TreeMap */ final TreeMap.Entry absLowest() { - TreeMap.Entry e = + TreeMap.Entry e = (fromStart ? m.getFirstEntry() : (loInclusive ? m.getCeilingEntry(lo) : m.getHigherEntry(lo))); @@ -1324,7 +1325,7 @@ public class TreeMap } final TreeMap.Entry absHighest() { - TreeMap.Entry e = + TreeMap.Entry e = (toEnd ? m.getLastEntry() : (hiInclusive ? m.getFloorEntry(hi) : m.getLowerEntry(hi))); @@ -1334,28 +1335,28 @@ public class TreeMap final TreeMap.Entry absCeiling(K key) { if (tooLow(key)) return absLowest(); - TreeMap.Entry e = m.getCeilingEntry(key); + TreeMap.Entry e = m.getCeilingEntry(key); return (e == null || tooHigh(e.key)) ? null : e; } final TreeMap.Entry absHigher(K key) { if (tooLow(key)) return absLowest(); - TreeMap.Entry e = m.getHigherEntry(key); + TreeMap.Entry e = m.getHigherEntry(key); return (e == null || tooHigh(e.key)) ? null : e; } final TreeMap.Entry absFloor(K key) { if (tooHigh(key)) return absHighest(); - TreeMap.Entry e = m.getFloorEntry(key); + TreeMap.Entry e = m.getFloorEntry(key); return (e == null || tooLow(e.key)) ? null : e; } final TreeMap.Entry absLower(K key) { if (tooHigh(key)) return absHighest(); - TreeMap.Entry e = m.getLowerEntry(key); + TreeMap.Entry e = m.getLowerEntry(key); return (e == null || tooLow(e.key)) ? null : e; } @@ -1466,7 +1467,7 @@ public class TreeMap } public final Map.Entry pollFirstEntry() { - TreeMap.Entry e = subLowest(); + TreeMap.Entry e = subLowest(); Map.Entry result = exportEntry(e); if (e != null) m.deleteEntry(e); @@ -1474,7 +1475,7 @@ public class TreeMap } public final Map.Entry pollLastEntry() { - TreeMap.Entry e = subHighest(); + TreeMap.Entry e = subHighest(); Map.Entry result = exportEntry(e); if (e != null) m.deleteEntry(e); @@ -1587,22 +1588,24 @@ public class TreeMap } final TreeMap.Entry nextEntry() { - TreeMap.Entry e = lastReturned = next; + TreeMap.Entry e = next; if (e == null || e.key == fenceKey) throw new NoSuchElementException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); next = successor(e); + lastReturned = e; return e; } final TreeMap.Entry prevEntry() { - TreeMap.Entry e = lastReturned = next; + TreeMap.Entry e = next; if (e == null || e.key == fenceKey) throw new NoSuchElementException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); next = predecessor(e); + lastReturned = e; return e; } @@ -1855,7 +1858,7 @@ public class TreeMap * @serial include */ private class SubMap extends AbstractMap - implements SortedMap, java.io.Serializable { + implements SortedMap, java.io.Serializable { private static final long serialVersionUID = -6520786458950516097L; private boolean fromStart = false, toEnd = false; private K fromKey, toKey; @@ -1885,7 +1888,7 @@ public class TreeMap */ static final class Entry implements Map.Entry { - K key; + K key; V value; Entry left = null; Entry right = null; @@ -2040,7 +2043,7 @@ public class TreeMap private static void setColor(Entry p, boolean c) { if (p != null) - p.color = c; + p.color = c; } private static Entry leftOf(Entry p) { @@ -2139,7 +2142,7 @@ public class TreeMap // If strictly internal, copy successor's element to p and then make p // point to successor. if (p.left != null && p.right != null) { - Entry s = successor (p); + Entry s = successor(p); p.key = s.key; p.value = s.value; p = s; @@ -2296,11 +2299,11 @@ public class TreeMap /** Intended to be called only from TreeSet.addAll */ void addAllForTreeSet(SortedSet set, V defaultVal) { - try { - buildFromSorted(set.size(), set.iterator(), null, defaultVal); - } catch (java.io.IOException cannotHappen) { - } catch (ClassNotFoundException cannotHappen) { - } + try { + buildFromSorted(set.size(), set.iterator(), null, defaultVal); + } catch (java.io.IOException cannotHappen) { + } catch (ClassNotFoundException cannotHappen) { + } } @@ -2335,12 +2338,12 @@ public class TreeMap * This cannot occur if str is null. */ private void buildFromSorted(int size, Iterator it, - java.io.ObjectInputStream str, - V defaultVal) + java.io.ObjectInputStream str, + V defaultVal) throws java.io.IOException, ClassNotFoundException { this.size = size; root = buildFromSorted(0, 0, size-1, computeRedLevel(size), - it, str, defaultVal); + it, str, defaultVal); } /** @@ -2358,10 +2361,10 @@ public class TreeMap * Must be equal to computeRedLevel for tree of this size. */ private final Entry buildFromSorted(int level, int lo, int hi, - int redLevel, - Iterator it, - java.io.ObjectInputStream str, - V defaultVal) + 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 @@ -2382,7 +2385,7 @@ public class TreeMap Entry left = null; if (lo < mid) left = buildFromSorted(level+1, lo, mid - 1, redLevel, - it, str, defaultVal); + it, str, defaultVal); // extract key and/or value from iterator or stream K key; @@ -2414,7 +2417,7 @@ public class TreeMap if (mid < hi) { Entry right = buildFromSorted(level+1, mid+1, hi, redLevel, - it, str, defaultVal); + it, str, defaultVal); middle.right = right; right.parent = middle; }