Index: modules/luni/src/main/java/java/util/TreeSet.java =================================================================== --- modules/luni/src/main/java/java/util/TreeSet.java (revision 417491) +++ modules/luni/src/main/java/java/util/TreeSet.java (working copy) @@ -33,105 +33,12 @@ private static final long serialVersionUID = -2479143000061671589L; - private transient TreeMap backingMap; + private transient SortedMap backingMap; - private static final class SubSet extends TreeMap.SubMap.SubMapSet - implements SortedSet { - private SubSet(TreeMap map) { - super(map, new MapEntry.Type() { - public E get(MapEntry entry) { - return entry.key; - } - }); - } + private TreeSet(SortedMap map) { + this.backingMap = map; + } - private SubSet(E start, TreeMap map) { - this(map); - hasStart = true; - startKey = start; - } - - private SubSet(E start, TreeMap map, E end) { - this(map); - hasStart = hasEnd = true; - startKey = start; - endKey = end; - } - - private SubSet(TreeMap map, E end) { - this(map); - hasEnd = true; - endKey = end; - } - - public boolean add(E object) { - checkRange(object); - return this.backingMap.put(object, object) != null; - } - - public Comparator comparator() { - return this.backingMap.comparator(); - } - - public boolean contains(Object object) { - if (checkRange((E)object, hasStart, hasEnd)) - return this.backingMap.containsKey(object); - return false; - } - - public E first() { - if (!hasStart) - return this.backingMap.firstKey(); - TreeMap.Entry node = this.backingMap.findAfter(startKey); - if (node != null && checkRange(node.key, false, hasEnd)) - return node.key; - throw new NoSuchElementException(); - } - - public SortedSet headSet(E end) { - checkRange(end); - if (hasStart) - return new SubSet(startKey, this.backingMap, end); - return new SubSet(this.backingMap, end); - } - - public E last() { - if (!hasEnd) - return this.backingMap.lastKey(); - TreeMap.Entry node = this.backingMap.findBefore(endKey); - if (node != null && checkRange(node.key, hasStart, false)) - return node.key; - throw new NoSuchElementException(); - } - - public boolean remove(Object object) { - if (checkRange((E)object, hasStart, hasEnd)) - return this.backingMap.remove(object) != null; - return false; - } - - public SortedSet subSet(E start, E end) { - checkRange(start); - checkRange(end); - Comparator c = this.backingMap.comparator(); - if (c == null) { - if (((Comparable) startKey).compareTo(endKey) <= 0) - return new SubSet(start, this.backingMap, end); - } else { - if (c.compare(startKey, endKey) <= 0) - return new SubSet(start, this.backingMap, end); - } - throw new IllegalArgumentException(); - } - - public SortedSet tailSet(E start) { - checkRange(start); - if (hasEnd) - return new SubSet(start, this.backingMap, endKey); - return new SubSet(start, this.backingMap); - } - } - /** * Constructs a new empty instance of TreeSet which uses natural ordering. * @@ -178,21 +85,9 @@ public TreeSet(SortedSet set) { this(set.comparator()); Iterator it = set.iterator(); - if (it.hasNext()) { - E object = it.next(); - TreeMap.Entry last = new TreeMap.Entry(object, object); - backingMap.root = last; - backingMap.size = 1; - while (it.hasNext()) { - object = it.next(); - TreeMap.Entry x = new TreeMap.Entry(object, object); - x.parent = last; - last.right = x; - backingMap.size++; - backingMap.balance(x); - last = x; - } - } + while (it.hasNext()) { + add(it.next()); + } } /** @@ -253,7 +148,11 @@ public Object clone() { try { TreeSet clone = (TreeSet) super.clone(); - clone.backingMap = (TreeMap) backingMap.clone(); + if (backingMap instanceof TreeMap) { + clone.backingMap = (SortedMap) ((TreeMap) backingMap).clone(); + } else { + clone.backingMap = new TreeMap(backingMap); + } return clone; } catch (CloneNotSupportedException e) { return null; @@ -323,7 +222,7 @@ ((Comparable) end).compareTo(end); else c.compare(end, end); - return new SubSet(backingMap, end); + return new TreeSet(backingMap.headMap(end)); } /** @@ -411,10 +310,10 @@ Comparator c = backingMap.comparator(); if (c == null) { if (((Comparable) start).compareTo(end) <= 0) - return new SubSet(start, backingMap, end); + return new TreeSet(backingMap.subMap(start, end)); } else { if (c.compare(start, end) <= 0) - return new SubSet(start, backingMap, end); + return new TreeSet(backingMap.subMap(start, end)); } throw new IllegalArgumentException(); } @@ -444,39 +343,42 @@ ((Comparable) start).compareTo(start); else c.compare(start, start); - return new SubSet(start, backingMap); + return new TreeSet(backingMap.tailMap(start)); } private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeObject(backingMap.comparator()); - stream.writeInt(backingMap.size); - if (backingMap.size > 0) { - TreeMap.Entry node = TreeMap.minimum(backingMap.root); - while (node != null) { - stream.writeObject(node.key); - node = TreeMap.successor(node); - } + int size = backingMap.size(); + stream.writeInt(size); + if (size > 0) { + Iterator it = backingMap.keySet().iterator(); + while (it.hasNext()) { + stream.writeObject(it.next()); + } } } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); - backingMap = new TreeMap((Comparator) stream.readObject()); - backingMap.size = stream.readInt(); - TreeMap.Entry last = null; - for (int i = backingMap.size; --i >= 0;) { - TreeMap.Entry node = new TreeMap.Entry((E)stream.readObject()); - node.value = node.key; - if (last == null) - backingMap.root = node; - else { - node.parent = last; - last.right = node; - backingMap.balance(node); - } - last = node; - } + TreeMap map = new TreeMap((Comparator) stream.readObject()); + int size = stream.readInt(); + if (size > 0) { + E key = (E)stream.readObject(); + TreeMap.Entry last = new TreeMap.Entry(key,key); + map.root = last; + map.size = 1; + for (int i=1; i x = new TreeMap.Entry(key,key); + x.parent = last; + last.right = x; + map.size++; + map.balance(x); + last = x; + } + } + backingMap = map; } } Index: modules/luni/src/main/java/java/util/TreeMap.java =================================================================== --- modules/luni/src/main/java/java/util/TreeMap.java (revision 417491) +++ modules/luni/src/main/java/java/util/TreeMap.java (working copy) @@ -41,6 +41,8 @@ transient int modCount = 0; + Set> entrySet = null; + /** * Entry is an internal class which is used to hold the entries of a * TreeMap. @@ -77,407 +79,542 @@ return (Comparable)obj; } - private static final class TreeMapIterator implements Iterator { - private final TreeMap backingMap; + private static class AbstractMapIterator { + TreeMap backingMap; + int expectedModCount; + TreeMap.Entry node; + TreeMap.Entry lastNode; - private int expectedModCount; + AbstractMapIterator(TreeMap map, Entry startNode) { + backingMap = map; + expectedModCount = map.modCount; + node = startNode; + } - private final MapEntry.Type type; + public boolean hasNext() { + return node != null; + } - private boolean hasEnd = false; + final public void remove() { + if (expectedModCount == backingMap.modCount) { + if (lastNode != null) { + backingMap.rbDelete(lastNode); + lastNode = null; + expectedModCount++; + } else { + throw new IllegalStateException(); + } + } else { + throw new ConcurrentModificationException(); + } + } - private Entry node, lastNode; + final void makeNext() { + if (expectedModCount != backingMap.modCount) { + throw new ConcurrentModificationException(); + } else if (node == null) { + throw new NoSuchElementException(); + } + lastNode = node; + node = TreeMap.successor(node); + } + } - private K endKey; + private static class UnboundedEntryIterator extends AbstractMapIterator implements Iterator> { - TreeMapIterator(TreeMap map, MapEntry.Type value) { - backingMap = map; - type = value; - expectedModCount = map.modCount; - if (map.root != null) { - node = TreeMap.minimum(map.root); + UnboundedEntryIterator(TreeMap map, Entry startNode) { + super(map, startNode); } - } - TreeMapIterator(TreeMap map, MapEntry.Type value, Entry startNode, - boolean checkEnd, K end) { - backingMap = map; - type = value; - expectedModCount = map.modCount; - node = startNode; - hasEnd = checkEnd; - endKey = end; - } + UnboundedEntryIterator(TreeMap map) { + super(map, map.root == null ? null : TreeMap.minimum(map.root)); + } - public boolean hasNext() { - return node != null; - } + public Map.Entry next() { + makeNext(); + return lastNode; + } + } - public E next() { - if (expectedModCount == backingMap.modCount) { - if (node != null) { - lastNode = node; - node = TreeMap.successor(node); - if (hasEnd && node != null) { - Comparator c = backingMap.comparator(); - if (c == null) { - Comparable cEndKey = toComparable(endKey); - if (cEndKey.compareTo(node.key) <= 0) { - node = null; - } - } else { - if (c.compare(endKey, node.key) <= 0) { - node = null; - } - } - } - return type.get(lastNode); - } else { - throw new NoSuchElementException(); + static class UnboundedKeyIterator extends AbstractMapIterator implements Iterator { + public UnboundedKeyIterator(TreeMap treeMap, Entry entry) { + super(treeMap, entry); + } + + public UnboundedKeyIterator(TreeMap map) { + super(map, map.root == null ? null : TreeMap.minimum(map.root)); + } + + public K next() { + makeNext(); + return lastNode.key; + } + } + + static class UnboundedValueIterator extends AbstractMapIterator implements Iterator { + + public UnboundedValueIterator(TreeMap treeMap, Entry startNode) { + super(treeMap, startNode); + } + + public UnboundedValueIterator(TreeMap map) { + super(map, map.root == null ? null : TreeMap.minimum(map.root)); + } + + public V next() { + makeNext(); + return lastNode.value; + } + } + + private static class ComparatorBoundedIterator extends AbstractMapIterator { + private K endKey; + private Comparator cmp; + + ComparatorBoundedIterator(TreeMap map, + Entry startNode, K end) { + super(map, startNode); + endKey = end; + cmp = map.comparator(); + } + + final void cleanNext() { + if (node != null && cmp.compare(endKey, node.key) <= 0) { + node = null; } - } else { - throw new ConcurrentModificationException(); } - } + } - public void remove() { - if (expectedModCount == backingMap.modCount) { - if (lastNode != null) { - backingMap.rbDelete(lastNode); - lastNode = null; - expectedModCount++; - } else { - throw new IllegalStateException(); + private static class ComparatorBoundedEntryIterator extends ComparatorBoundedIterator implements Iterator> { + + ComparatorBoundedEntryIterator(TreeMap map, + Entry startNode, K end) { + super(map, startNode,end); + } + + public Map.Entry next() { + makeNext(); + cleanNext(); + return lastNode; + } + } + + private static class ComparatorBoundedKeyIterator extends ComparatorBoundedIterator implements Iterator { + + ComparatorBoundedKeyIterator(TreeMap map, + Entry startNode, K end) { + super(map, startNode,end); + } + + public K next() { + makeNext(); + cleanNext(); + return lastNode.key; + } + } + + + private static class ComparatorBoundedValueIterator extends ComparatorBoundedIterator implements Iterator { + + ComparatorBoundedValueIterator(TreeMap map, + Entry startNode, K end) { + super(map, startNode,end); + } + + public V next() { + makeNext(); + cleanNext(); + return lastNode.value; + } + } + + private static class ComparableBoundedIterator extends AbstractMapIterator { + final private Comparable endKey; + + public ComparableBoundedIterator(TreeMap treeMap, Entry entry, Comparable endKey) { + super(treeMap, entry); + this.endKey = endKey; + } + + final void cleanNext() { + if ((node != null) && (endKey.compareTo(node.key) <= 0)) { + node = null; } - } else { - throw new ConcurrentModificationException(); } - } - } + } - static final class SubMap extends AbstractMap implements SortedMap { - private final TreeMap backingMap; + private static class ComparableBoundedEntryIterator extends ComparableBoundedIterator implements Iterator> { - private boolean hasStart, hasEnd; - private K startKey, endKey; + ComparableBoundedEntryIterator(TreeMap map, + Entry startNode, Comparable end) { + super(map, startNode, end); + } - static class SubMapSet extends AbstractSet implements Set { - final TreeMap backingMap; + public Map.Entry next() { + makeNext(); + cleanNext(); + return lastNode; + } - boolean hasStart, hasEnd; + } - K startKey, endKey; + private static class ComparableBoundedKeyIterator extends ComparableBoundedIterator implements Iterator { - final MapEntry.Type type; + ComparableBoundedKeyIterator(TreeMap map, + Entry startNode, Comparable end) { + super(map, startNode,end); + } - SubMapSet(TreeMap map, MapEntry.Type theType) { - backingMap = map; - type = theType; - } + public K next() { + makeNext(); + cleanNext(); + return lastNode.key; + } + } - SubMapSet(boolean starting, K start, TreeMap map, - boolean ending, K end, MapEntry.Type theType) { - this(map, theType); - hasStart = starting; - startKey = start; - hasEnd = ending; - endKey = end; - } + private static class ComparableBoundedValueIterator extends ComparableBoundedIterator implements Iterator { - void checkRange(K key) { - if (backingMap.comparator() == null) { - Comparable object = toComparable(key); - if (hasStart && object.compareTo(startKey) < 0) { + ComparableBoundedValueIterator(TreeMap map, + Entry startNode, Comparable end) { + super(map, startNode,end); + } + + public V next() { + makeNext(); + cleanNext(); + return lastNode.value; + } + } + + static final class SubMap extends AbstractMap implements SortedMap { + private TreeMap backingMap; + + boolean hasStart, hasEnd; + + K startKey, endKey; + + Set> entrySet = null; + + SubMap(K start, TreeMap map) { + backingMap = map; + hasStart = true; + startKey = start; + } + + SubMap(K start, TreeMap map, K end) { + backingMap = map; + hasStart = hasEnd = true; + startKey = start; + endKey = end; + } + + SubMap(TreeMap map, K end) { + backingMap = map; + hasEnd = true; + endKey = end; + } + + private void checkRange(K key) { + Comparator cmp = backingMap.comparator; + if (cmp == null) { + Comparable object = (Comparable) key; + if (hasStart && object.compareTo(startKey) < 0) throw new IllegalArgumentException(); - } - if (hasEnd && object.compareTo(endKey) >= 0) { + if (hasEnd && object.compareTo(endKey) >= 0) throw new IllegalArgumentException(); - } - } else { - if (hasStart - && backingMap.comparator().compare(key, startKey) < 0) { + } else { + if (hasStart + && backingMap.comparator().compare(key, startKey) < 0) throw new IllegalArgumentException(); - } - if (hasEnd - && backingMap.comparator().compare(key, endKey) >= 0) { + if (hasEnd && backingMap.comparator().compare(key, endKey) >= 0) throw new IllegalArgumentException(); - } - } - } + } + } - boolean checkRange(K key, boolean hasStart, boolean hasEnd) { - if (backingMap.comparator() == null) { + private boolean isInRange(K key) { + Comparator cmp = backingMap.comparator; + if (cmp == null) { Comparable object = toComparable(key); - if (hasStart && object.compareTo(startKey) < 0) { + if (hasStart && object.compareTo(startKey) < 0) return false; - } - if (hasEnd && object.compareTo(endKey) >= 0) { + if (hasEnd && object.compareTo(endKey) >= 0) return false; - } - } else { - if (hasStart - && backingMap.comparator().compare(key, startKey) < 0) { + } else { + if (hasStart && cmp.compare(key, startKey) < 0) return false; + if (hasEnd && cmp.compare(key, endKey) >= 0) + return false; + } + return true; + } + + private boolean checkUpperBound(K key) { + if (hasEnd) { + Comparator cmp = backingMap.comparator; + if (cmp == null) { + return (((Comparable) key).compareTo(endKey) < 0); + } else { + return (cmp.compare(key, endKey) < 0); } - if (hasEnd - && backingMap.comparator().compare(key, endKey) >= 0) { - return false; - } - } - return true; - } + } else { + return true; + } + } - @Override - public boolean isEmpty() { - if (hasStart) { - TreeMap.Entry node = backingMap.findAfter(startKey); - return node == null || !checkRange(node.key, false, hasEnd); - } - return backingMap.findBefore(endKey) == null; - } - - @Override - public Iterator iterator() { - TreeMap.Entry startNode; - if (hasStart) { - startNode = backingMap.findAfter(startKey); - if (startNode != null - && !checkRange(startNode.key, false, hasEnd)) { - startNode = null; + private boolean checkLowerBound(K key) { + if (hasStart) { + Comparator cmp = backingMap.comparator; + if (cmp == null) { + return (((Comparable) key).compareTo(endKey) >= 0); + } else { + return (cmp.compare(key, endKey) >= 0); } - } else { - startNode = backingMap.findBefore(endKey); - if (startNode != null) { - startNode = minimum(backingMap.root); - } - } - return new TreeMapIterator(backingMap, type, startNode, hasEnd, - endKey); - } + } else { + return true; + } + } - @Override - public int size() { - int size = 0; - Iterator it = iterator(); - while (it.hasNext()) { - size++; - it.next(); - } - return size; - } - } + public Comparator comparator() { + return backingMap.comparator(); + } - SubMap(K start, TreeMap map, K end) { - backingMap = map; - hasStart = hasEnd = true; - startKey = start; - endKey = end; - } - - SubMap(K start, TreeMap map) { - backingMap = map; - hasStart = true; - startKey = start; - } + public boolean containsKey(Object key) { + if (isInRange((K)key)) + return backingMap.containsKey(key); + return false; + } - SubMap(TreeMap map, K end) { - backingMap = map; - hasEnd = true; - endKey = end; - } + public Set> entrySet() { + if(entrySet==null) { + entrySet = new SubMapEntrySet(this); + } + return entrySet; + } - private void checkRange(K key) { - if (backingMap.comparator() == null) { - Comparable object = toComparable(key); - if (hasStart && object.compareTo(startKey) < 0) { - throw new IllegalArgumentException(); + public K firstKey() { + TreeMap.Entry node = firstEntry(); + if (node != null ) { + return node.key; } - if (hasEnd && object.compareTo(endKey) >= 0) { - throw new IllegalArgumentException(); + throw new NoSuchElementException(); + } + + TreeMap.Entry firstEntry() { + if (!hasStart) { + TreeMap.Entry root = backingMap.root; + return (root == null) ? null : minimum(backingMap.root); } - } else { - if (hasStart - && backingMap.comparator().compare(key, startKey) < 0) { - throw new IllegalArgumentException(); + TreeMap.Entry node = backingMap.findAfter(startKey); + if (node != null && checkUpperBound(node.key)) { + return node; + } else { + return null; } - if (hasEnd && backingMap.comparator().compare(key, endKey) >= 0) { - throw new IllegalArgumentException(); + } + + public V get(Object key) { + if (isInRange((K)key)) + return backingMap.get(key); + return null; + } + + public SortedMap headMap(K endKey) { + checkRange(endKey); + if (hasStart) + return new SubMap(startKey, backingMap, endKey); + return new SubMap(backingMap, endKey); + } + + public boolean isEmpty() { + if (hasStart) { + TreeMap.Entry node = backingMap.findAfter(startKey); + return node == null || !checkUpperBound(node.key); } - } - } + return backingMap.findBefore(endKey) == null; + } - @SuppressWarnings("unchecked") - private boolean checkRange(Object keyObj, boolean hasStart, boolean hasEnd) { - K key = (K)keyObj; - if (backingMap.comparator() == null) { - Comparable object = toComparable(key); - if (hasStart && object.compareTo(startKey) < 0) { - return false; + public Set keySet() { + if (keySet == null) { + keySet = new SubMapKeySet(this); } - if (hasEnd && object.compareTo(endKey) >= 0) { - return false; + return keySet; + } + + public K lastKey() { + if (!hasEnd) + return backingMap.lastKey(); + TreeMap.Entry node = backingMap.findBefore(endKey); + if (node != null && checkLowerBound(node.key)) + return node.key; + throw new NoSuchElementException(); + } + + public V put(K key, V value) { + if (isInRange(key)) + return backingMap.put(key, value); + throw new IllegalArgumentException(); + } + + public V remove(Object key) { + if (isInRange((K)key)) + return backingMap.remove(key); + return null; + } + + public SortedMap subMap(K startKey, K endKey) { + checkRange(startKey); + checkRange(endKey); + Comparator c = backingMap.comparator(); + if (c == null) { + if (((Comparable) startKey).compareTo(endKey) <= 0) + return new SubMap(startKey, backingMap, endKey); + } else { + if (c.compare(startKey, endKey) <= 0) + return new SubMap(startKey, backingMap, endKey); } - } else { - if (hasStart - && backingMap.comparator().compare(key, startKey) < 0) { - return false; + throw new IllegalArgumentException(); + } + + public SortedMap tailMap(K startKey) { + checkRange(startKey); + if (hasEnd) + return new SubMap(startKey, backingMap, endKey); + return new SubMap(startKey, backingMap); + } + + public Collection values() { + if(valuesCollection==null) { + valuesCollection = new SubMapValuesCollection(this); } - if (hasEnd && backingMap.comparator().compare(key, endKey) >= 0) { - return false; - } - } - return true; - } + return valuesCollection; + } + } - public Comparator comparator() { - return backingMap.comparator(); - } + static class SubMapEntrySet extends AbstractSet> implements Set> { + SubMap subMap; - @Override - public boolean containsKey(Object key) { - if (checkRange(key, hasStart, hasEnd)) { - return backingMap.containsKey(key); + SubMapEntrySet(SubMap map) { + subMap = map; } - return false; - } - @Override - public Set> entrySet() { - return new SubMapSet, K, V>(hasStart, startKey, backingMap, hasEnd, - endKey, new MapEntry.Type, K, V>() { - public Map.Entry get(MapEntry entry) { - return entry; - } - }) { - @Override - public boolean contains(Object object) { - if (object instanceof Map.Entry) { - Map.Entry entry = (Map.Entry) object; - Object v1 = get(entry.getKey()), v2 = entry.getValue(); - return v1 == null ? v2 == null : v1.equals(v2); - } - return false; - } - }; - } + public boolean isEmpty() { + return subMap.isEmpty(); + } - public K firstKey() { - if (!hasStart) { - return backingMap.firstKey(); + public Iterator> iterator() { + TreeMap.Entry startNode = subMap.firstEntry(); + if (subMap.hasEnd) { + Comparator cmp = subMap.comparator(); + if (cmp == null) { + return new ComparableBoundedEntryIterator(subMap.backingMap, startNode, (Comparable) subMap.endKey); + } else { + return new ComparatorBoundedEntryIterator(subMap.backingMap, startNode, subMap.endKey); + } + } else { + return new UnboundedEntryIterator(subMap.backingMap, startNode); + } } - TreeMap.Entry node = backingMap.findAfter(startKey); - if (node != null && checkRange(node.key, false, hasEnd)) { - return node.key; + + public int size() { + int size = 0; + Iterator> it = iterator(); + while (it.hasNext()) { + size++; + it.next(); + } + return size; } - throw new NoSuchElementException(); - } - @Override - public V get(Object key) { - if (checkRange(key, hasStart, hasEnd)) { - return backingMap.get(key); + public boolean contains(Object object) { + if (object instanceof Map.Entry) { + Map.Entry entry = (Map.Entry) object; + K key = entry.getKey(); + if (subMap.isInRange(key)) { + V v1 = subMap.get(key), v2 = entry.getValue(); + return v1 == null ? v2 == null : v1.equals(v2); + } + } + return false; } - return null; - } - public SortedMap headMap(K endKey) { - checkRange(endKey); - if (hasStart) { - return new SubMap(startKey, backingMap, endKey); + } + + static class SubMapKeySet extends AbstractSet implements Set { + SubMap subMap; + + SubMapKeySet(SubMap map) { + subMap = map; } - return new SubMap(backingMap, endKey); - } - @Override - public boolean isEmpty() { - if (hasStart) { - TreeMap.Entry node = backingMap.findAfter(startKey); - return node == null || !checkRange(node.key, false, hasEnd); - } - return backingMap.findBefore(endKey) == null; - } + public boolean contains(Object object) { + return subMap.containsKey(object); + } - @Override - public Set keySet() { - if (keySet == null) { - keySet = new SubMapSet(hasStart, startKey, backingMap, hasEnd, - endKey, new MapEntry.Type() { - public K get(MapEntry entry) { - return entry.key; - } - }) { - @Override - public boolean contains(Object object) { - return containsKey(object); - } - }; - } - return keySet; - } + public boolean isEmpty() { + return subMap.isEmpty(); + } - public K lastKey() { - if (!hasEnd) { - return backingMap.lastKey(); + public int size() { + int size = 0; + Iterator it = iterator(); + while (it.hasNext()) { + size++; + it.next(); + } + return size; } - TreeMap.Entry node = backingMap.findBefore(endKey); - if (node != null && checkRange(node.key, hasStart, false)) { - return node.key; + + public Iterator iterator() { + TreeMap.Entry startNode = subMap.firstEntry(); + if (subMap.hasEnd) { + Comparator cmp = subMap.comparator(); + if (cmp == null) { + return new ComparableBoundedKeyIterator(subMap.backingMap, startNode, (Comparable) subMap.endKey); + } else { + return new ComparatorBoundedKeyIterator(subMap.backingMap, startNode, subMap.endKey); + } + } else { + return new UnboundedKeyIterator(subMap.backingMap, startNode); + } } - throw new NoSuchElementException(); - } + } - @Override - public V put(K key, V value) { - if (checkRange(key, hasStart, hasEnd)) { - return backingMap.put(key, value); + static class SubMapValuesCollection extends AbstractCollection { + SubMap subMap; + + public SubMapValuesCollection(SubMap subMap) { + this.subMap = subMap; } - throw new IllegalArgumentException(); - } - @Override - public V remove(Object key) { - if (checkRange(key, hasStart, hasEnd)) { - return backingMap.remove(key); + public boolean isEmpty() { + return subMap.isEmpty(); } - return null; - } - public SortedMap subMap(K startKey, K endKey) { - checkRange(startKey); - checkRange(endKey); - Comparator c = backingMap.comparator(); - if (c == null) { - if (toComparable(startKey).compareTo(endKey) <= 0) { - return new SubMap(startKey, backingMap, endKey); + public Iterator iterator() { + TreeMap.Entry startNode = subMap.firstEntry(); + if (subMap.hasEnd) { + Comparator cmp = subMap.comparator(); + if (cmp == null) { + return new ComparableBoundedValueIterator(subMap.backingMap, startNode, (Comparable) subMap.endKey); + } else { + return new ComparatorBoundedValueIterator(subMap.backingMap, startNode, subMap.endKey); + } + } else { + return new UnboundedValueIterator(subMap.backingMap, startNode); } - } else { - if (c.compare(startKey, endKey) <= 0) { - return new SubMap(startKey, backingMap, endKey); + } + + public int size() { + int cnt = 0; + for (Iterator it = iterator(); it.hasNext();) { + it.next(); + cnt++; } - } - throw new IllegalArgumentException(); - } - - public SortedMap tailMap(K startKey) { - checkRange(startKey); - if (hasEnd) { - return new SubMap(startKey, backingMap, endKey); + return cnt; } - return new SubMap(startKey, backingMap); - } + } - @Override - public Collection values() { - return new SubMapSet(hasStart, startKey, backingMap, hasEnd, - endKey, new MapEntry.Type() { - public V get(MapEntry entry) { - return entry.value; - } - }); - } - } - /** * Constructs a new empty instance of TreeMap. * @@ -607,6 +744,7 @@ public Object clone() { try { TreeMap clone = (TreeMap) super.clone(); + clone.entrySet = null; if (root != null) { clone.root = root.clone(null); } @@ -686,37 +824,36 @@ */ @Override public Set> entrySet() { - return new AbstractSet>() { - @Override - public int size() { - return size; - } + if (entrySet == null) { + entrySet = new AbstractSet>() { + @Override + public int size() { + return size; + } - @Override - public void clear() { - TreeMap.this.clear(); - } + @Override + public void clear() { + TreeMap.this.clear(); + } - @Override - public boolean contains(Object object) { - if (object instanceof Map.Entry) { - Map.Entry entry = (Map.Entry) object; - Object v1 = get(entry.getKey()), v2 = entry.getValue(); - return v1 == null ? v2 == null : v1.equals(v2); - } - return false; - } + @Override + public boolean contains(Object object) { + if (object instanceof Map.Entry) { + Map.Entry entry = (Map.Entry) object; + Object v1 = get(entry.getKey()), v2 = entry.getValue(); + return v1 == null ? v2 == null : v1.equals(v2); + } + return false; + } - @Override - public Iterator> iterator() { - return new TreeMapIterator, K, V>(TreeMap.this, new MapEntry.Type, K, V>() { - public Map.Entry get(MapEntry entry) { - return entry; - } - }); - } - }; - } + @Override + public Iterator> iterator() { + return new UnboundedEntryIterator(TreeMap.this); + } + }; + } + return entrySet; + } @SuppressWarnings("unchecked") private Entry find(Object keyObj) { @@ -948,12 +1085,7 @@ @Override public Iterator iterator() { - return new TreeMapIterator(TreeMap.this, - new MapEntry.Type() { - public K get(MapEntry entry) { - return entry.key; - } - }); + return new UnboundedKeyIterator (TreeMap.this); } }; } @@ -1279,12 +1411,7 @@ @Override public Iterator iterator() { - return new TreeMapIterator(TreeMap.this, - new MapEntry.Type() { - public V get(MapEntry entry) { - return entry.value; - } - }); + return new UnboundedValueIterator (TreeMap.this); } }; }