You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/10/10 23:09:50 UTC

cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections AbstractDualBidiMap.java

scolebourne    2003/10/10 14:09:50

  Modified:    collections/src/java/org/apache/commons/collections
                        AbstractDualBidiMap.java
  Log:
  Ensure that remove works correctly on views
  
  Revision  Changes    Path
  1.3       +19 -5     jakarta-commons/collections/src/java/org/apache/commons/collections/AbstractDualBidiMap.java
  
  Index: AbstractDualBidiMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/AbstractDualBidiMap.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractDualBidiMap.java	9 Oct 2003 20:21:32 -0000	1.2
  +++ AbstractDualBidiMap.java	10 Oct 2003 21:09:49 -0000	1.3
  @@ -335,7 +335,8 @@
       protected static class KeySetIterator extends AbstractIteratorDecorator {
           
           private final AbstractDualBidiMap map;
  -        private Object last;
  +        private Object last = null;
  +        private boolean canRemove = false;
           
           protected KeySetIterator(Iterator iterator, AbstractDualBidiMap map) {
               super(iterator);
  @@ -344,14 +345,19 @@
           
           public Object next() {
               last = super.next();
  +            canRemove = true;
               return last;
           }
           
           public void remove() {
  +            if (canRemove == false) {
  +                throw new IllegalStateException("Iterator remove() can only be called once after next()");
  +            }
               Object value = map.maps[0].get(last);
               super.remove();
               map.maps[1].remove(value);
               last = null;
  +            canRemove = false;
           }
       }
   
  @@ -388,7 +394,8 @@
       protected static class EntrySetIterator extends AbstractIteratorDecorator {
           
           private final AbstractDualBidiMap map;
  -        private Map.Entry last;
  +        private Map.Entry last = null;
  +        private boolean canRemove = false;
           
           protected EntrySetIterator(Iterator iterator, AbstractDualBidiMap map) {
               super(iterator);
  @@ -397,13 +404,20 @@
           
           public Object next() {
               last = new MapEntry((Map.Entry) super.next(), map);
  +            canRemove = true;
               return last;
           }
           
           public void remove() {
  +            if (canRemove == false) {
  +                throw new IllegalStateException("Iterator remove() can only be called once after next()");
  +            }
  +            // store value as remove may change the entry in the decorator (eg.TreeMap)
  +            Object value = last.getValue();
               super.remove();
  -            map.maps[1].remove(last.getValue());
  +            map.maps[1].remove(value);
               last = null;
  +            canRemove = false;
           }
       }
   
  
  
  

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