commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestBidiMap.java
Date Fri, 26 Sep 2003 23:28:43 GMT
matth       2003/09/26 16:28:43

  Modified:    collections/src/java/org/apache/commons/collections
                        HashBidiMap.java
               collections/src/test/org/apache/commons/collections
                        TestBidiMap.java
  Log:
  Updated logic in put() to remove pair on duplicate value.  Because of this, TestBidiMap
can no longer extend TestMap since this seems to break the Map contract.
  
  Revision  Changes    Path
  1.2       +18 -12    jakarta-commons/collections/src/java/org/apache/commons/collections/HashBidiMap.java
  
  Index: HashBidiMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/HashBidiMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HashBidiMap.java	23 Sep 2003 20:29:34 -0000	1.1
  +++ HashBidiMap.java	26 Sep 2003 23:28:43 -0000	1.2
  @@ -57,6 +57,7 @@
    */
   package org.apache.commons.collections;
   
  +import java.io.Serializable;
   import java.util.AbstractMap;
   import java.util.AbstractSet;
   import java.util.HashMap;
  @@ -72,10 +73,10 @@
    * 
    * @author Matthew Hawthorne
    */
  -public class HashBidiMap extends AbstractMap implements BidiMap {
  +public class HashBidiMap extends AbstractMap implements BidiMap, Serializable {
   
       /**
  -     * Delegate map array.  The first map contains standards entries, and the 
  +     * Delegate map array.  The first map contains standard entries, and the 
        * second contains inverses.
        */
       final Map[] maps = new Map[] { new HashMap(), new HashMap()};
  @@ -114,31 +115,36 @@
       }
   
       public Object put(Object key, Object value) {
  +        // Removes pair from standard map if a previous inverse entry exists
  +        final Object oldValue = maps[1].put(value, key);
  +        if (oldValue != null) {
  +            maps[0].remove(oldValue);
  +        }
  +        
           final Object obj = maps[0].put(key, value);
  -        maps[1].put(value, key);
           return obj;
       }
   
       public Set entrySet() {
           // The entrySet is the root of most Map methods, care must be taken not 
           // to reference instance methods like size()
  -        
  +
           // Creates anonymous AbstractSet
           return new AbstractSet() {
  -            
  +
               public Iterator iterator() {
                   // Creates anonymous Iterator
                   return new Iterator() {
   
                       // Delegate iterator.
                       final Iterator it = maps[0].entrySet().iterator();
  -                    
  +
                       // Current iterator entry
                       Map.Entry currentEntry;
   
                       public void remove() {
                           // Removes from standard and inverse Maps.
  -                        
  +
                           // Object must be removed using the iterator or a 
                           // ConcurrentModificationException is thrown
                           it.remove();
  @@ -168,7 +174,7 @@
               public int size() {
                   return HashBidiMap.this.maps[0].size();
               }
  -            
  +
           }; // anonymous AbstractSet
   
       } // entrySet()
  @@ -225,8 +231,8 @@
                       }; // anonymous Iterator
                   }
   
  -            };  // anonymous AbstractSet
  -            
  +            }; // anonymous AbstractSet
  +
           } // entrySet()
   
       } // InverseBidiMap
  
  
  
  1.2       +41 -6     jakarta-commons/collections/src/test/org/apache/commons/collections/TestBidiMap.java
  
  Index: TestBidiMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestBidiMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestBidiMap.java	23 Sep 2003 20:29:34 -0000	1.1
  +++ TestBidiMap.java	26 Sep 2003 23:28:43 -0000	1.2
  @@ -59,6 +59,8 @@
   
   import java.util.Map;
   
  +import junit.framework.TestCase;
  +
   /**
    * JUnit tests.
    * 
  @@ -66,7 +68,7 @@
    * @version $Id$
    * @see org.apache.commons.collections.BidiMap
    */
  -public abstract class TestBidiMap extends TestMap {
  +public abstract class TestBidiMap extends TestCase {
   
       // Test data.
       private static final Object KEY = "key1";
  @@ -130,7 +132,36 @@
               inverseMap.getKey(entries[0][0]));
       }
   
  +    /**
  +     * Ensures that calling:
  +     * 
  +     * <pre>
  +     * map.add(a, c)
  +     * map.add(b, c)
  +     * </pre>
  +     * 
  +     * Removes the entry (a, c)
  +     */
  +    public void testAddDuplicateValue() {
  +        final BidiMap map = createBidiMap();
  +
  +        final Object key1 = "key1";
  +        final Object key2 = "key2";
  +        final Object value = "value";
  +
  +        map.put(key1, value);
  +        map.put(key2, value);
  +
  +        assertTrue(
  +            "Key/value pair was not removed on duplicate value.",
  +            !map.containsKey(key1));
  +            
  +        assertEquals("Key/value mismatch", key2, map.getKey(value));
  +    }
  +
  +    // ----------------------------------------------------------------
       // Removal tests
  +    // ----------------------------------------------------------------
   
       public void testClear() {
           BidiMap map = createBidiMapWithData();
  @@ -153,7 +184,7 @@
       public void testRemove() {
           remove(createBidiMapWithData(), KEY);
           remove(createBidiMapWithData().inverseBidiMap(), VALUE);
  -        
  +
           removeKey(createBidiMapWithData(), VALUE);
           removeKey(createBidiMapWithData().inverseBidiMap(), KEY);
       }
  @@ -163,7 +194,7 @@
           assertTrue("Key was not removed.", !map.containsKey(key));
           assertNull("Value was not removed.", map.getKey(value));
       }
  -    
  +
       private final void removeKey(BidiMap map, Object value) {
           final Object key = map.removeKey(value);
           assertTrue("Key was not removed.", !map.containsKey(key));
  @@ -215,11 +246,15 @@
       // Data generation methods
       // ----------------------------------------------------------------
   
  +    /**
  +     * This classes used to extend collections.TestMap, but can't anymore since 
  +     * put() breaks a contract.
  +     */
       protected Map makeEmptyMap() {
           return createBidiMap();
       }
   
  -    private final BidiMap createBidiMapWithData() {
  +    protected final BidiMap createBidiMapWithData() {
           final BidiMap map = createBidiMap();
           fillMap(map);
           return map;
  
  
  

Mime
View raw message