commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pj...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestFastArrayList1.java TestFastHashMap1.java TestFastTreeMap.java TestFastTreeMap1.java
Date Tue, 13 Aug 2002 04:34:09 GMT
pjack       2002/08/12 21:34:09

  Modified:    collections/src/java/org/apache/commons/collections
                        FastArrayList.java FastHashMap.java
                        FastTreeMap.java
               collections/src/test/org/apache/commons/collections
                        TestFastArrayList1.java TestFastHashMap1.java
                        TestFastTreeMap.java TestFastTreeMap1.java
  Log:
  The collection views of FastArrayList, FastTreeMap and FastHashMap are
  now backed by the parent collection, even in fast mode, and those
  classes now pass all unit tests in fast mode.  The unit tests were
  altered so that they actually test the classes with setFast(true).
  PR:7924
  
  Revision  Changes    Path
  1.7       +516 -28   jakarta-commons/collections/src/java/org/apache/commons/collections/FastArrayList.java
  
  Index: FastArrayList.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FastArrayList.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FastArrayList.java	10 Aug 2002 00:49:45 -0000	1.6
  +++ FastArrayList.java	13 Aug 2002 04:34:08 -0000	1.7
  @@ -66,6 +66,7 @@
   import java.io.Serializable;
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.ConcurrentModificationException;
   import java.util.Iterator;
   import java.util.List;
   import java.util.ListIterator;
  @@ -532,15 +533,11 @@
        * failing due to concurrent modifications.
        */
       public Iterator iterator() {
  -
           if (fast) {
  -            return (list.iterator());
  +            return new ListIter(0);
           } else {
  -            synchronized (list) {
  -                return (list.iterator());
  -            }
  +            return list.iterator();
           }
  -
       }
   
   
  @@ -569,15 +566,11 @@
        * See the implementation note on <code>iterator()</code>.
        */
       public ListIterator listIterator() {
  -
           if (fast) {
  -            return (list.listIterator());
  +            return new ListIter(0);
           } else {
  -            synchronized (list) {
  -                return (list.listIterator());
  -            }
  +            return list.listIterator();
           }
  -
       }
   
   
  @@ -591,15 +584,11 @@
        * @exception IndexOutOfBoundsException if the index is out of range
        */
       public ListIterator listIterator(int index) {
  -
           if (fast) {
  -            return (list.listIterator(index));
  +            return new ListIter(index);
           } else {
  -            synchronized (list) {
  -                return (list.listIterator(index));
  -            }
  +            return list.listIterator(index);
           }
  -
       }
   
   
  @@ -762,15 +751,11 @@
        * @exception IndexOutOfBoundsException if an index is out of range
        */
       public List subList(int fromIndex, int toIndex) {
  -
           if (fast) {
  -            return (list.subList(fromIndex, toIndex));
  +            return new SubList(fromIndex, toIndex);
           } else {
  -            synchronized (list) {
  -                return (list.subList(fromIndex, toIndex));
  -            }
  +            return list.subList(fromIndex, toIndex);
           }
  -
       }
   
   
  @@ -851,4 +836,507 @@
       }
   
   
  +
  +    private class SubList implements List {
  +
  +        private int first;
  +        private int last;
  +        private List expected;
  +
  +
  +        public SubList(int first, int last) {
  +            this.first = first;
  +            this.last = last;
  +            this.expected = list;
  +        }
  +
  +        private List get(List l) {
  +            if (list != expected) {
  +                throw new ConcurrentModificationException();
  +            }
  +            return l.subList(first, last);
  +        }
  +
  +        public void clear() {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    get(temp).clear();
  +                    last = first;
  +                    list = temp;
  +                    expected = temp;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    get(expected).clear();
  +                }
  +            }
  +        }
  +
  +        public boolean remove(Object o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    boolean r = get(temp).remove(o);
  +                    if (r) last--;
  +                    list = temp;
  +                    expected = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).remove(o);
  +                }
  +            }
  +        }
  +
  +        public boolean removeAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    List sub = get(temp);
  +                    boolean r = sub.removeAll(o);
  +                    if (r) last = first + sub.size();
  +                    list = temp;
  +                    expected = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).removeAll(o);
  +                }
  +            }
  +        }
  +
  +        public boolean retainAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    List sub = get(temp);
  +                    boolean r = sub.retainAll(o);
  +                    if (r) last = first + sub.size();
  +                    list = temp;
  +                    expected = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).retainAll(o);
  +                }
  +            }
  +        }
  +
  +        public int size() {
  +            if (fast) {
  +                return get(expected).size();
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).size();
  +                }
  +            }
  +        }
  +
  +
  +        public boolean isEmpty() {
  +            if (fast) {
  +                return get(expected).isEmpty();
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).isEmpty();
  +                }
  +            }
  +        }
  +
  +        public boolean contains(Object o) {
  +            if (fast) {
  +                return get(expected).contains(o);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).contains(o);
  +                }
  +            }
  +        }
  +
  +        public boolean containsAll(Collection o) {
  +            if (fast) {
  +                return get(expected).containsAll(o);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).containsAll(o);
  +                }
  +            }
  +        }
  +
  +        public Object[] toArray(Object[] o) {
  +            if (fast) {
  +                return get(expected).toArray(o);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).toArray(o);
  +                }
  +            }
  +        }
  +
  +        public Object[] toArray() {
  +            if (fast) {
  +                return get(expected).toArray();
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).toArray();
  +                }
  +            }
  +        }
  +
  +
  +        public boolean equals(Object o) {
  +            if (o == this) return true;
  +            if (fast) {
  +                return get(expected).equals(o);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).equals(o);
  +                }
  +            }
  +        }
  +
  +        public int hashCode() {
  +            if (fast) {
  +                return get(expected).hashCode();
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).hashCode();
  +                }
  +            }
  +        }
  +
  +        public boolean add(Object o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    boolean r = get(temp).add(o);
  +                    if (r) last++;
  +                    list = temp;
  +                    expected = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).add(o);
  +                }
  +            }
  +        }
  +
  +        public boolean addAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    boolean r = get(temp).addAll(o);
  +                    if (r) last += o.size();
  +                    list = temp;
  +                    expected = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).addAll(o);
  +                }
  +            }
  +        }
  +
  +        public void add(int i, Object o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    get(temp).add(i, o);
  +                    last++;
  +                    list = temp;
  +                    expected = temp;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    get(expected).add(i, o);
  +                }
  +            }
  +        }
  +
  +        public boolean addAll(int i, Collection o) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    boolean r = get(temp).addAll(i, o);
  +                    list = temp;
  +                    if (r) last += o.size();
  +                    expected = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).addAll(i, o);
  +                }
  +            }
  +        }
  +
  +        public Object remove(int i) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    Object o = get(temp).remove(i);
  +                    last--;
  +                    list = temp;
  +                    expected = temp;
  +                    return o;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).remove(i);
  +                }
  +            }
  +        }
  +
  +        public Object set(int i, Object a) {
  +            if (fast) {
  +                synchronized (FastArrayList.this) {
  +                    ArrayList temp = (ArrayList) list.clone();
  +                    Object o = get(temp).set(i, a);
  +                    list = temp;
  +                    expected = temp;
  +                    return o;
  +                }
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).set(i, a);
  +                }
  +            }
  +        }
  +
  +
  +        public Iterator iterator() {
  +            return new SubListIter(0);
  +        }
  +
  +        public ListIterator listIterator() {
  +            return new SubListIter(0);
  +        }
  +
  +        public ListIterator listIterator(int i) {
  +            return new SubListIter(i);
  +        }
  +
  +
  +        public Object get(int i) {
  +            if (fast) {
  +                return get(expected).get(i);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).get(i);
  +                }
  +            }
  +        }
  +
  +        public int indexOf(Object o) {
  +            if (fast) {
  +                return get(expected).indexOf(o);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).indexOf(o);
  +                }
  +            }
  +        }
  +
  +
  +        public int lastIndexOf(Object o) {
  +            if (fast) {
  +                return get(expected).lastIndexOf(o);
  +            } else {
  +                synchronized (list) {
  +                    return get(expected).lastIndexOf(o);
  +                }
  +            }
  +        }
  +
  +
  +        public List subList(int f, int l) {
  +            if (list != expected) {
  +                throw new ConcurrentModificationException();
  +            }
  +            return new SubList(first + f, f + l);
  +        }
  +
  +
  +    private class SubListIter implements ListIterator {
  +
  +        private List expected;
  +        private ListIterator iter;
  +        private int lastReturnedIndex = -1;
  +
  +
  +        public SubListIter(int i) {
  +            this.expected = list;
  +            this.iter = SubList.this.get(expected).listIterator(i);
  +        }
  +
  +        private void checkMod() {
  +            if (list != expected) {
  +                throw new ConcurrentModificationException();
  +            }
  +        }
  +
  +        List get() {
  +            return SubList.this.get(expected);
  +        }
  +
  +        public boolean hasNext() {
  +            checkMod();
  +            return iter.hasNext();     
  +        }
  +
  +        public Object next() {
  +            checkMod();
  +            lastReturnedIndex = iter.nextIndex();
  +            return iter.next();
  +        }
  +
  +        public boolean hasPrevious() {
  +            checkMod();
  +            return iter.hasPrevious();
  +        }
  +
  +        public Object previous() {
  +            checkMod();
  +            lastReturnedIndex = iter.previousIndex();
  +            return iter.previous();
  +        }
  +
  +        public int previousIndex() {
  +            checkMod();
  +            return iter.previousIndex();
  +        }
  +
  +        public int nextIndex() {
  +            checkMod();
  +            return iter.nextIndex();
  +        }
  +
  +        public void remove() {
  +            checkMod();
  +            if (lastReturnedIndex < 0) {
  +                throw new IllegalStateException();
  +            }
  +            get().remove(lastReturnedIndex);
  +            last--;
  +            expected = list;
  +            iter = get().listIterator(previousIndex());
  +            lastReturnedIndex = -1;
  +        }
  +
  +        public void set(Object o) {
  +            checkMod();
  +            if (lastReturnedIndex < 0) {
  +                throw new IllegalStateException();
  +            }
  +            get().set(lastReturnedIndex, o);
  +            expected = list;
  +            iter = get().listIterator(previousIndex() + 1);
  +        } 
  +
  +        public void add(Object o) {
  +            checkMod();
  +            int i = nextIndex();
  +            get().add(i, o);
  +            last++;
  +            iter = get().listIterator(i + 1);
  +            lastReturnedIndex = 1;
  +        }
  +
  +   }
  +
  +
  +    }
  +
  +
  +
  +    private class ListIter implements ListIterator {
  +
  +        private List expected;
  +        private ListIterator iter;
  +        private int lastReturnedIndex = -1;
  +
  +
  +        public ListIter(int i) {
  +            this.expected = list;
  +            this.iter = get().listIterator(i);
  +        }
  +
  +        private void checkMod() {
  +            if (list != expected) {
  +                throw new ConcurrentModificationException();
  +            }
  +        }
  +
  +        List get() {
  +            return expected;
  +        }
  +
  +        public boolean hasNext() {
  +            checkMod();
  +            return iter.hasNext();     
  +        }
  +
  +        public Object next() {
  +            checkMod();
  +            lastReturnedIndex = iter.nextIndex();
  +            return iter.next();
  +        }
  +
  +        public boolean hasPrevious() {
  +            checkMod();
  +            return iter.hasPrevious();
  +        }
  +
  +        public Object previous() {
  +            checkMod();
  +            lastReturnedIndex = iter.previousIndex();
  +            return iter.previous();
  +        }
  +
  +        public int previousIndex() {
  +            checkMod();
  +            return iter.previousIndex();
  +        }
  +
  +        public int nextIndex() {
  +            checkMod();
  +            return iter.nextIndex();
  +        }
  +
  +        public void remove() {
  +            checkMod();
  +            if (lastReturnedIndex < 0) {
  +                throw new IllegalStateException();
  +            }
  +            get().remove(lastReturnedIndex);
  +            expected = list;
  +            iter = get().listIterator(previousIndex());
  +            lastReturnedIndex = -1;
  +        }
  +
  +        public void set(Object o) {
  +            checkMod();
  +            if (lastReturnedIndex < 0) {
  +                throw new IllegalStateException();
  +            }
  +            get().set(lastReturnedIndex, o);
  +            expected = list;
  +            iter = get().listIterator(previousIndex() + 1);
  +        } 
  +
  +        public void add(Object o) {
  +            checkMod();
  +            int i = nextIndex();
  +            get().add(i, o);
  +            iter = get().listIterator(i + 1);
  +            lastReturnedIndex = 1;
  +        }
  +
  +   }
   }
  
  
  
  1.8       +254 -28   jakarta-commons/collections/src/java/org/apache/commons/collections/FastHashMap.java
  
  Index: FastHashMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FastHashMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FastHashMap.java	10 Aug 2002 00:49:45 -0000	1.7
  +++ FastHashMap.java	13 Aug 2002 04:34:08 -0000	1.8
  @@ -65,6 +65,7 @@
   
   import java.io.Serializable;
   import java.util.Collection;
  +import java.util.ConcurrentModificationException;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Map;
  @@ -280,15 +281,7 @@
        * element in the returned collection is a <code>Map.Entry</code>.
        */
       public Set entrySet() {
  -
  -        if (fast) {
  -            return (map.entrySet());
  -        } else {
  -            synchronized (map) {
  -                return (map.entrySet());
  -            }
  -        }
  -
  +        return new EntrySet();
       }
   
   
  @@ -418,15 +411,7 @@
        * Return a set view of the keys contained in this map.
        */
       public Set keySet() {
  -
  -        if (fast) {
  -            return (map.keySet());
  -        } else {
  -            synchronized (map) {
  -                return (map.keySet());
  -            }
  -        }
  -
  +        return new KeySet();
       }
   
   
  @@ -523,16 +508,257 @@
        * Return a collection view of the values contained in this map.
        */
       public Collection values() {
  +        return new Values();
  +    }
   
  -        if (fast) {
  -            return (map.values());
  -        } else {
  -            synchronized (map) {
  -                return (map.values());
  +
  +    private abstract class CollectionView implements Collection {
  +
  +        public CollectionView() {
  +        }
  +
  +        protected abstract Collection get(Map map);
  +        protected abstract Object iteratorNext(Map.Entry entry);
  +
  +
  +        public void clear() {
  +            if (fast) {
  +                synchronized (FastHashMap.this) {
  +                    HashMap temp = (HashMap) map.clone();
  +                    get(temp).clear();
  +                    map = temp;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    get(map).clear();
  +                }
               }
           }
   
  -    }
  +        public boolean remove(Object o) {
  +            if (fast) {
  +                synchronized (FastHashMap.this) {
  +                    HashMap temp = (HashMap) map.clone();
  +                    boolean r = get(temp).remove(o);
  +                    map = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    return get(map).remove(o);
  +                }
  +            }
  +        }
  +
  +        public boolean removeAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastHashMap.this) {
  +                    HashMap temp = (HashMap) map.clone();
  +                    boolean r = get(temp).removeAll(o);
  +                    map = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    return get(map).removeAll(o);
  +                }
  +            }
  +        }
  +
  +        public boolean retainAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastHashMap.this) {
  +                    HashMap temp = (HashMap) map.clone();
  +                    boolean r = get(temp).retainAll(o);
  +                    map = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    return get(map).retainAll(o);
  +                }
  +            }
  +        }
  +
  +        public int size() {
  +            if (fast) {
  +                return get(map).size();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).size();
  +                }
  +            }
  +        }
  +
  +
  +        public boolean isEmpty() {
  +            if (fast) {
  +                return get(map).isEmpty();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).isEmpty();
  +                }
  +            }
  +        }
  +
  +        public boolean contains(Object o) {
  +            if (fast) {
  +                return get(map).contains(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).contains(o);
  +                }
  +            }
  +        }
  +
  +        public boolean containsAll(Collection o) {
  +            if (fast) {
  +                return get(map).containsAll(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).containsAll(o);
  +                }
  +            }
  +        }
  +
  +        public Object[] toArray(Object[] o) {
  +            if (fast) {
  +                return get(map).toArray(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).toArray(o);
  +                }
  +            }
  +        }
  +
  +        public Object[] toArray() {
  +            if (fast) {
  +                return get(map).toArray();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).toArray();
  +                }
  +            }
  +        }
  +
  +
  +        public boolean equals(Object o) {
  +            if (o == this) return true;
  +            if (fast) {
  +                return get(map).equals(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).equals(o);
  +                }
  +            }
  +        }
  +
  +        public int hashCode() {
  +            if (fast) {
  +                return get(map).hashCode();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).hashCode();
  +                }
  +            }
  +        }
  +
  +        public boolean add(Object o) {
  +            throw new UnsupportedOperationException();
  +        }
  +
  +        public boolean addAll(Collection c) {
  +            throw new UnsupportedOperationException();
  +        }
  +
  +        public Iterator iterator() {
  +            return new CollectionViewIterator();
  +        }
  +
  +        private class CollectionViewIterator implements Iterator {
  +
  +            private Map expected;
  +            private Map.Entry lastReturned = null;
  +            private Iterator iterator;
  +
  +            public CollectionViewIterator() {
  +                this.expected = map;
  +                this.iterator = expected.entrySet().iterator();
  +            }
  + 
  +            public boolean hasNext() {
  +                if (expected != map) {
  +                    throw new ConcurrentModificationException();
  +                }
  +                return iterator.hasNext();
  +            }
  +
  +            public Object next() {
  +                if (expected != map) {
  +                    throw new ConcurrentModificationException();
  +                }
  +                lastReturned = (Map.Entry)iterator.next();
  +                return iteratorNext(lastReturned);
  +            }
  +
  +            public void remove() {
  +                if (lastReturned == null) {
  +                    throw new IllegalStateException();
  +                }
  +                if (fast) {
  +                    synchronized (FastHashMap.this) {
  +                        if (expected != map) {
  +                            throw new ConcurrentModificationException();
  +                        }
  +                        FastHashMap.this.remove(lastReturned.getKey());
  +                        lastReturned = null;
  +                        expected = map;
  +                    }
  +                } else {
  +                    iterator.remove();
  +                    lastReturned = null;
  +                }
  +            }
  +        }
  +   }
  +
  +
  +   private class KeySet extends CollectionView implements Set {
  +
  +       protected Collection get(Map map) {
  +           return map.keySet();
  +       }
  +
  +       protected Object iteratorNext(Map.Entry entry) {
  +           return entry.getKey();
  +       }       
  +
  +   }
  +
  +
  +   private class Values extends CollectionView {
  +
  +       protected Collection get(Map map) {
  +           return map.values();
  +       }
  +
  +       protected Object iteratorNext(Map.Entry entry) {
  +           return entry.getValue();
  +       }
  +   }
  +
  +
  +   private class EntrySet extends CollectionView implements Set {
  +
  +       protected Collection get(Map map) {
  +           return map.entrySet();
  +       }
  +
  +
  +       protected Object iteratorNext(Map.Entry entry) {
  +           return entry;
  +       }
   
  +   }
   
   }
  
  
  
  1.8       +256 -28   jakarta-commons/collections/src/java/org/apache/commons/collections/FastTreeMap.java
  
  Index: FastTreeMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FastTreeMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FastTreeMap.java	10 Aug 2002 00:49:45 -0000	1.7
  +++ FastTreeMap.java	13 Aug 2002 04:34:08 -0000	1.8
  @@ -66,6 +66,7 @@
   import java.io.Serializable;
   import java.util.Collection;
   import java.util.Comparator;
  +import java.util.ConcurrentModificationException;
   import java.util.Iterator;
   import java.util.Map;
   import java.util.Map.Entry;
  @@ -300,15 +301,7 @@
        * element in the returned collection is a <code>Map.Entry</code>.
        */
       public Set entrySet() {
  -
  -        if (fast) {
  -            return (map.entrySet());
  -        } else {
  -            synchronized (map) {
  -                return (map.entrySet());
  -            }
  -        }
  -
  +        return new EntrySet();
       }
   
   
  @@ -473,15 +466,7 @@
        * Return a set view of the keys contained in this map.
        */
       public Set keySet() {
  -
  -        if (fast) {
  -            return (map.keySet());
  -        } else {
  -            synchronized (map) {
  -                return (map.keySet());
  -            }
  -        }
  -
  +        return new KeySet();
       }
   
   
  @@ -633,16 +618,259 @@
        * Return a collection view of the values contained in this map.
        */
       public Collection values() {
  +        return new Values();
  +    }
   
  -        if (fast) {
  -            return (map.values());
  -        } else {
  -            synchronized (map) {
  -                return (map.values());
  +
  +
  +    private abstract class CollectionView implements Collection {
  +
  +        public CollectionView() {
  +        }
  +
  +        protected abstract Collection get(Map map);
  +        protected abstract Object iteratorNext(Map.Entry entry);
  +
  +
  +        public void clear() {
  +            if (fast) {
  +                synchronized (FastTreeMap.this) {
  +                    TreeMap temp = (TreeMap) map.clone();
  +                    get(temp).clear();
  +                    map = temp;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    get(map).clear();
  +                }
               }
           }
   
  -    }
  +        public boolean remove(Object o) {
  +            if (fast) {
  +                synchronized (FastTreeMap.this) {
  +                    TreeMap temp = (TreeMap) map.clone();
  +                    boolean r = get(temp).remove(o);
  +                    map = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    return get(map).remove(o);
  +                }
  +            }
  +        }
  +
  +        public boolean removeAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastTreeMap.this) {
  +                    TreeMap temp = (TreeMap) map.clone();
  +                    boolean r = get(temp).removeAll(o);
  +                    map = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    return get(map).removeAll(o);
  +                }
  +            }
  +        }
  +
  +        public boolean retainAll(Collection o) {
  +            if (fast) {
  +                synchronized (FastTreeMap.this) {
  +                    TreeMap temp = (TreeMap) map.clone();
  +                    boolean r = get(temp).retainAll(o);
  +                    map = temp;
  +                    return r;
  +                }
  +            } else {
  +                synchronized (map) {
  +                    return get(map).retainAll(o);
  +                }
  +            }
  +        }
  +
  +        public int size() {
  +            if (fast) {
  +                return get(map).size();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).size();
  +                }
  +            }
  +        }
  +
  +
  +        public boolean isEmpty() {
  +            if (fast) {
  +                return get(map).isEmpty();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).isEmpty();
  +                }
  +            }
  +        }
  +
  +        public boolean contains(Object o) {
  +            if (fast) {
  +                return get(map).contains(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).contains(o);
  +                }
  +            }
  +        }
  +
  +        public boolean containsAll(Collection o) {
  +            if (fast) {
  +                return get(map).containsAll(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).containsAll(o);
  +                }
  +            }
  +        }
  +
  +        public Object[] toArray(Object[] o) {
  +            if (fast) {
  +                return get(map).toArray(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).toArray(o);
  +                }
  +            }
  +        }
  +
  +        public Object[] toArray() {
  +            if (fast) {
  +                return get(map).toArray();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).toArray();
  +                }
  +            }
  +        }
  +
  +
  +        public boolean equals(Object o) {
  +            if (o == this) return true;
  +            if (fast) {
  +                return get(map).equals(o);
  +            } else {
  +                synchronized (map) {
  +                    return get(map).equals(o);
  +                }
  +            }
  +        }
  +
  +        public int hashCode() {
  +            if (fast) {
  +                return get(map).hashCode();
  +            } else {
  +                synchronized (map) {
  +                    return get(map).hashCode();
  +                }
  +            }
  +        }
  +
  +        public boolean add(Object o) {
  +            throw new UnsupportedOperationException();
  +        }
  +
  +        public boolean addAll(Collection c) {
  +            throw new UnsupportedOperationException();
  +        }
  +
  +        public Iterator iterator() {
  +            return new CollectionViewIterator();
  +        }
  +
  +        private class CollectionViewIterator implements Iterator {
  +
  +            private Map expected;
  +            private Map.Entry lastReturned = null;
  +            private Iterator iterator;
  +
  +            public CollectionViewIterator() {
  +                this.expected = map;
  +                this.iterator = expected.entrySet().iterator();
  +            }
  + 
  +            public boolean hasNext() {
  +                if (expected != map) {
  +                    throw new ConcurrentModificationException();
  +                }
  +                return iterator.hasNext();
  +            }
  +
  +            public Object next() {
  +                if (expected != map) {
  +                    throw new ConcurrentModificationException();
  +                }
  +                lastReturned = (Map.Entry)iterator.next();
  +                return iteratorNext(lastReturned);
  +            }
  +
  +            public void remove() {
  +                if (lastReturned == null) {
  +                    throw new IllegalStateException();
  +                }
  +                if (fast) {
  +                    synchronized (FastTreeMap.this) {
  +                        if (expected != map) {
  +                            throw new ConcurrentModificationException();
  +                        }
  +                        FastTreeMap.this.remove(lastReturned.getKey());
  +                        lastReturned = null;
  +                        expected = map;
  +                    }
  +                } else {
  +                    iterator.remove();
  +                    lastReturned = null;
  +                }
  +            }
  +        }
  +   }
  +
  +
  +   private class KeySet extends CollectionView implements Set {
  +
  +       protected Collection get(Map map) {
  +           return map.keySet();
  +       }
  +
  +       protected Object iteratorNext(Map.Entry entry) {
  +           return entry.getKey();
  +       }       
  +
  +   }
  +
  +
  +   private class Values extends CollectionView {
  +
  +       protected Collection get(Map map) {
  +           return map.values();
  +       }
  +
  +       protected Object iteratorNext(Map.Entry entry) {
  +           return entry.getValue();
  +       }
  +   }
  +
  +
  +   private class EntrySet extends CollectionView implements Set {
  +
  +       protected Collection get(Map map) {
  +           return map.entrySet();
  +       }
  +
  +
  +       protected Object iteratorNext(Map.Entry entry) {
  +           return entry;
  +       }
  +
  +   }
   
   
   }
  
  
  
  1.3       +7 -6      jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastArrayList1.java
  
  Index: TestFastArrayList1.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastArrayList1.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestFastArrayList1.java	21 Jun 2002 03:33:28 -0000	1.2
  +++ TestFastArrayList1.java	13 Aug 2002 04:34:09 -0000	1.3
  @@ -93,14 +93,15 @@
   
       public void setUp()
       {
  -        list = (ArrayList) makeList();
  +        list = (ArrayList) makeEmptyList();
       }
   
  -    public List makeList()
  +    public List makeEmptyList()
       {
           FastArrayList fal = new FastArrayList();
           fal.setFast(true);
           return (fal);
       }
  +
   
   }
  
  
  
  1.4       +6 -36     jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastHashMap1.java
  
  Index: TestFastHashMap1.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastHashMap1.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestFastHashMap1.java	18 Jun 2002 05:35:58 -0000	1.3
  +++ TestFastHashMap1.java	13 Aug 2002 04:34:09 -0000	1.4
  @@ -91,45 +91,15 @@
           junit.textui.TestRunner.main(testCaseName);
       }
   
  -    public Map makeMap() {
  +    public Map makeEmptyMap() {
           FastHashMap fhm = new FastHashMap();
           fhm.setFast(true);
           return (fhm);
       }
   
  -    /**
  -     *  When the fast hash map is in fast mode, the underlying hash map is
  -     *  cloned on modification (i.e. on a put).  Because of that, any
  -     *  previously existing entry set will be representing the old (pre-clone)
  -     *  map and will not reflect changes made to the map after the clone.  So,
  -     *  we must override this test.
  -     **/
  -    public void testEntrySetChangesWithMapPut() {
  -    }
  -
  -    /**
  -     *  When the fast hash map is in fast mode, the underlying hash map is
  -     *  cloned on modification (i.e. on a remove).  Because of that, any
  -     *  previously existing entry set will be representing the old (pre-clone)
  -     *  map and will not reflect changes made to the map after the clone.  So,
  -     *  we must override this test.
  -     **/
  -    public void testEntrySetChangesWithMapRemove() {
  -    }
  -
  -    /**
  -     *  When the fast hash map is in fast mode, the underlying hash map is
  -     *  cloned on modification (i.e. on a put).  Because of that, any
  -     *  previously existing entry set will be representing the old (pre-clone)
  -     *  map, so changes to the set will not be seen in the map. So, we must
  -     *  override this test.
  -     **/
  -    public void testEntrySetRemoveCausesMapModification() {
  -    }
  -
       public void setUp()
       {
  -        map = (HashMap) makeMap();
  +        map = (HashMap) makeEmptyMap();
       }
   
   }
  
  
  
  1.8       +8 -4      jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap.java
  
  Index: TestFastTreeMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestFastTreeMap.java	12 Aug 2002 18:00:46 -0000	1.7
  +++ TestFastTreeMap.java	13 Aug 2002 04:34:09 -0000	1.8
  @@ -94,6 +94,10 @@
           ftm.setFast(false);
           return (ftm);
       }
  +
  +    public Map makeConfirmedEmptyMap() {
  +        return new TreeMap();
  +    }
     
       /**
        *  The comparator for the fast tree map does not support null keys.
  
  
  
  1.4       +6 -35     jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap1.java
  
  Index: TestFastTreeMap1.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap1.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestFastTreeMap1.java	18 Jun 2002 05:35:58 -0000	1.3
  +++ TestFastTreeMap1.java	13 Aug 2002 04:34:09 -0000	1.4
  @@ -91,7 +91,7 @@
           junit.textui.TestRunner.main(testCaseName);
       }
   
  -    public Map makeMap() {
  +    public Map makeEmptyMap() {
           FastTreeMap ftm = new FastTreeMap();
           ftm.setFast(true);
           return (ftm);
  @@ -99,36 +99,7 @@
   
       public void setUp()
       {
  -        map = (TreeMap) makeMap();
  +        map = (TreeMap) makeEmptyMap();
       }
   
  -    /**
  -     *  When the fast tree map is in fast mode, the underlying tree map is
  -     *  cloned on modification (i.e. on a put).  Because of that, any
  -     *  previously existing entry set will be representing the old (pre-clone)
  -     *  map and will not reflect changes made to the map after the clone.  So,
  -     *  we must override this test.
  -     **/
  -    public void testMapEntrySetChangesWithMapPut() {
  -    }
  -
  -    /**
  -     *  When the fast tree map is in fast mode, the underlying tree map is
  -     *  cloned on modification (i.e. on a put).  Because of that, any
  -     *  previously existing entry set will be representing the old (pre-clone)
  -     *  map and will not reflect changes made to the map after the clone.  So,
  -     *  we must override this test.
  -     **/
  -    public void testMapEntrySetChangesWithMapRemove() {
  -    }
  -
  -    /**
  -     *  When the fast tree map is in fast mode, the underlying tree map is
  -     *  cloned on modification (i.e. on a put).  Because of that, any
  -     *  previously existing entry set will be representing the old (pre-clone)
  -     *  map, so changes to the set will not be seen in the map. So, we must
  -     *  override this test.
  -     **/
  -    public void testMapEntrySetRemoveCausesMapModification() {
  -    }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message