--- jsr166/src/main/java/util/TreeMap.java 2006/05/02 19:42:46 1.35 +++ jsr166/src/main/java/util/TreeMap.java 2006/05/10 02:31:44 1.38 @@ -1050,7 +1050,7 @@ public class TreeMap return size() != oldSize; } public NavigableSet subSet(E fromElement, boolean fromInclusive, - E toElement, boolean toInclusive) { + E toElement, boolean toInclusive) { return new TreeSet(m.subMap(fromElement, fromInclusive, toElement, toInclusive)); } @@ -1117,10 +1117,11 @@ public class TreeMap throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); + // deleted entries are replaced by their successors if (lastReturned.left != null && lastReturned.right != null) next = lastReturned; deleteEntry(lastReturned); - expectedModCount++; + expectedModCount = modCount; lastReturned = null; } } @@ -1207,14 +1208,17 @@ public class TreeMap // SubMaps + /** + * @serial include + */ static abstract class NavigableSubMap extends AbstractMap implements NavigableMap, java.io.Serializable { - /* + /** * The backing map. */ final TreeMap m; - /* + /** * Endpoints are represented as triples (fromStart, lo, * loInclusive) and (toEnd, hi, hiInclusive). If fromStart is * true, then the low (absolute) bound is the start of the @@ -1222,7 +1226,6 @@ public class TreeMap * if loInclusive is true, lo is the inclusive bound, else lo * is the exclusive bound. Similarly for the upper bound. */ - final K lo, hi; final boolean fromStart, toEnd; final boolean loInclusive, hiInclusive; @@ -1347,7 +1350,7 @@ public class TreeMap } // Abstract methods defined in ascending vs descending classes - // These relay to the appropriate absolute versions + // These relay to the appropriate absolute versions abstract TreeMap.Entry subLowest(); abstract TreeMap.Entry subHighest(); @@ -1579,17 +1582,29 @@ public class TreeMap return e; } - public void remove() { + final void removeAscending() { if (lastReturned == null) throw new IllegalStateException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); + // deleted entries are replaced by their successors if (lastReturned.left != null && lastReturned.right != null) next = lastReturned; m.deleteEntry(lastReturned); - expectedModCount++; lastReturned = null; + expectedModCount = m.modCount; + } + + final void removeDescending() { + if (lastReturned == null) + throw new IllegalStateException(); + if (m.modCount != expectedModCount) + throw new ConcurrentModificationException(); + m.deleteEntry(lastReturned); + lastReturned = null; + expectedModCount = m.modCount; } + } final class SubMapEntryIterator extends SubMapIterator> { @@ -1600,6 +1615,9 @@ public class TreeMap public Map.Entry next() { return nextEntry(); } + public void remove() { + removeAscending(); + } } final class SubMapKeyIterator extends SubMapIterator { @@ -1610,6 +1628,9 @@ public class TreeMap public K next() { return nextEntry().key; } + public void remove() { + removeAscending(); + } } final class DescendingSubMapEntryIterator extends SubMapIterator> { @@ -1621,6 +1642,9 @@ public class TreeMap public Map.Entry next() { return prevEntry(); } + public void remove() { + removeDescending(); + } } final class DescendingSubMapKeyIterator extends SubMapIterator { @@ -1631,15 +1655,21 @@ public class TreeMap public K next() { return prevEntry().key; } + public void remove() { + removeDescending(); + } } } + /** + * @serial include + */ static final class AscendingSubMap extends NavigableSubMap { private static final long serialVersionUID = 912986545866124060L; AscendingSubMap(TreeMap m, boolean fromStart, K lo, boolean loInclusive, - boolean toEnd, K hi, boolean hiInclusive) { + boolean toEnd, K hi, boolean hiInclusive) { super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); } @@ -1648,7 +1678,7 @@ public class TreeMap } public NavigableMap subMap(K fromKey, boolean fromInclusive, - K toKey, boolean toInclusive) { + K toKey, boolean toInclusive) { if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); if (!inRange(toKey, toInclusive)) @@ -1710,11 +1740,14 @@ public class TreeMap TreeMap.Entry subLower(K key) { return absLower(key); } } + /** + * @serial include + */ static final class DescendingSubMap extends NavigableSubMap { private static final long serialVersionUID = 912986545866120460L; DescendingSubMap(TreeMap m, boolean fromStart, K lo, boolean loInclusive, - boolean toEnd, K hi, boolean hiInclusive) { + boolean toEnd, K hi, boolean hiInclusive) { super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); } @@ -1726,7 +1759,7 @@ public class TreeMap } public NavigableMap subMap(K fromKey, boolean fromInclusive, - K toKey, boolean toInclusive) { + K toKey, boolean toInclusive) { if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); if (!inRange(toKey, toInclusive)) @@ -1794,6 +1827,8 @@ public class TreeMap * support NavigableMap. It translates an old-version SubMap into * a new-version AscendingSubMap. This class is never otherwise * used. + * + * @serial include */ private class SubMap extends AbstractMap implements SortedMap, java.io.Serializable { @@ -1877,7 +1912,7 @@ public class TreeMap public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; - Map.Entry e = (Map.Entry)o; + Map.Entry e = (Map.Entry)o; return valEquals(key,e.getKey()) && valEquals(value,e.getValue()); }