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/12/03 13:59:37 UTC

cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/bidimap AbstractBidiMapDecorator.java UnmodifiableBidiMap.java

scolebourne    2003/12/03 04:59:37

  Modified:    collections/src/test/org/apache/commons/collections/bidimap
                        AbstractTestBidiMap.java TestAll.java
  Added:       collections/src/test/org/apache/commons/collections/bidimap
                        TestUnmodifiableBidiMap.java
               collections/src/java/org/apache/commons/collections/bidimap
                        AbstractBidiMapDecorator.java
                        UnmodifiableBidiMap.java
  Log:
  Add unmodifiable BidiMap decorator
  
  Revision  Changes    Path
  1.6       +17 -3     jakarta-commons/collections/src/test/org/apache/commons/collections/bidimap/AbstractTestBidiMap.java
  
  Index: AbstractTestBidiMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/bidimap/AbstractTestBidiMap.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- AbstractTestBidiMap.java	1 Dec 2003 22:49:00 -0000	1.5
  +++ AbstractTestBidiMap.java	3 Dec 2003 12:59:36 -0000	1.6
  @@ -92,7 +92,7 @@
               new Object[] { "value2", "key2" },
               new Object[] { "value3", "key3" }
       };
  -    private final Object[][] entries;
  +    protected final Object[][] entries;
   
       public AbstractTestBidiMap(String testName) {
           super(testName);
  @@ -149,6 +149,8 @@
       // BidiPut
       //-----------------------------------------------------------------------
       public void testBidiPut() {
  +        if (isPutAddSupported() == false || isPutChangeSupported() == false) return;
  +
           BidiMap map = makeEmptyBidiMap();
           BidiMap inverse = map.inverseBidiMap();
           assertEquals(0, map.size());
  @@ -275,6 +277,8 @@
   
       //-----------------------------------------------------------------------
       public void testBidiClear() {
  +        if (isRemoveSupported() == false) return;
  +
           BidiMap map = makeFullBidiMap();
           map.clear();
           assertTrue("Map was not cleared.", map.isEmpty());
  @@ -290,6 +294,8 @@
   
       //-----------------------------------------------------------------------
       public void testBidiRemove() {
  +        if (isRemoveSupported() == false) return;
  +        
           remove(makeFullBidiMap(), entries[0][0]);
           remove(makeFullBidiMap().inverseBidiMap(), entries[0][1]);
   
  @@ -325,6 +331,8 @@
   
       //-----------------------------------------------------------------------
       public void testBidiRemoveByKeySet() {
  +        if (isRemoveSupported() == false) return;
  +        
           removeByKeySet(makeFullBidiMap(), entries[0][0], entries[0][1]);
           removeByKeySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]);
       }
  @@ -345,6 +353,8 @@
   
       //-----------------------------------------------------------------------
       public void testBidiRemoveByEntrySet() {
  +        if (isRemoveSupported() == false) return;
  +        
           removeByEntrySet(makeFullBidiMap(), entries[0][0], entries[0][1]);
           removeByEntrySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]);
       }
  @@ -365,6 +375,7 @@
               !map.inverseBidiMap().containsKey(value));
       }
   
  +    //-----------------------------------------------------------------------
       public BulkTest bulkTestMapEntrySet() {
           return new TestBidiMapEntrySet();
       }
  @@ -447,6 +458,9 @@
           }
           public BidiMap makeFullBidiMap() {
               return main.makeFullBidiMap().inverseBidiMap();
  +        }
  +        public Map makeFullMap() {
  +            return ((BidiMap) main.makeFullMap()).inverseBidiMap();
           }
           public Object[] getSampleKeys() {
               return main.getSampleValues();
  
  
  
  1.2       +4 -2      jakarta-commons/collections/src/test/org/apache/commons/collections/bidimap/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/bidimap/TestAll.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestAll.java	16 Nov 2003 20:35:46 -0000	1.1
  +++ TestAll.java	3 Dec 2003 12:59:36 -0000	1.2
  @@ -87,6 +87,8 @@
           suite.addTest(TestDualTreeBidiMap.suite());
           suite.addTest(TestTreeBidiMap.suite());
           
  +        suite.addTest(TestUnmodifiableBidiMap.suite());
  +        
           return suite;
       }
           
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/bidimap/TestUnmodifiableBidiMap.java
  
  Index: TestUnmodifiableBidiMap.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/bidimap/TestUnmodifiableBidiMap.java,v 1.1 2003/12/03 12:59:36 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.bidimap;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import junit.framework.Test;
  import junit.textui.TestRunner;
  
  import org.apache.commons.collections.BidiMap;
  import org.apache.commons.collections.BulkTest;
  
  /**
   * JUnit tests.
   * 
   * @version $Revision: 1.1 $ $Date: 2003/12/03 12:59:36 $
   * 
   * @author Stephen Colebourne
   */
  public class TestUnmodifiableBidiMap extends AbstractTestBidiMap {
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
      
      public static Test suite() {
          return BulkTest.makeSuite(TestUnmodifiableBidiMap.class);
      }
  
      public TestUnmodifiableBidiMap(String testName) {
          super(testName);
      }
  
      public BidiMap makeEmptyBidiMap() {
          return UnmodifiableBidiMap.decorate(new DualHashBidiMap());
      }
      public BidiMap makeFullBidiMap() {
          BidiMap bidi = new DualHashBidiMap();
          for (int i = 0; i < entries.length; i++) {
              bidi.put(entries[i][0], entries[i][1]);
          }
          return UnmodifiableBidiMap.decorate(bidi);
      }
      public Map makeFullMap() {
          BidiMap bidi = new DualHashBidiMap();
          addSampleMappings(bidi);
          return UnmodifiableBidiMap.decorate(bidi);
      }
      
      public Map makeConfirmedMap() {
          return new HashMap();
      }
  
      /**
       * Override to prevent infinite recursion of tests.
       */
      public String[] ignoredTests() {
          return new String[] {"TestUnmodifiableBidiMap.bulkTestInverseMap.bulkTestInverseMap"};
      }
  
      public boolean isPutAddSupported() {
          return false;
      }
      public boolean isPutChangeSupported() {
          return false;
      }
      public boolean isRemoveSupported() {
          return false;
      }
      
  }
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/bidimap/AbstractBidiMapDecorator.java
  
  Index: AbstractBidiMapDecorator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/bidimap/AbstractBidiMapDecorator.java,v 1.1 2003/12/03 12:59:37 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.bidimap;
  
  import org.apache.commons.collections.BidiMap;
  import org.apache.commons.collections.MapIterator;
  import org.apache.commons.collections.map.AbstractMapDecorator;
  
  /** 
   * Provides a base decorator that enables additional functionality to be added
   * to a BidiMap via decoration.
   * <p>
   * Methods are forwarded directly to the decorated map.
   * <p>
   * This implementation does not perform any special processing with the map views.
   * Instead it simply returns the set/collection from the wrapped map. This may be
   * undesirable, for example if you are trying to write a validating implementation
   * it would provide a loophole around the validation.
   * But, you might want that loophole, so this class is kept simple.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/12/03 12:59:37 $
   * 
   * @author Stephen Colebourne
   */
  public abstract class AbstractBidiMapDecorator extends AbstractMapDecorator implements BidiMap {
      
      /**
       * Constructor that wraps (not copies).
       *
       * @param map  the map to decorate, must not be null
       * @throws IllegalArgumentException if the collection is null
       */
      public AbstractBidiMapDecorator(BidiMap map) {
          super(map);
      }
  
      /**
       * Gets the map being decorated.
       * 
       * @return the decorated map
       */
      protected BidiMap getBidiMap() {
          return (BidiMap) map;
      }
  
      //-----------------------------------------------------------------------
      public MapIterator mapIterator() {
          return getBidiMap().mapIterator();
      }
  
      public Object getKey(Object value) {
          return getBidiMap().getKey(value);
      }
  
      public Object removeValue(Object value) {
          return getBidiMap().removeValue(value);
      }
  
      public BidiMap inverseBidiMap() {
          return getBidiMap().inverseBidiMap();
      }
  
  }
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java
  
  Index: UnmodifiableBidiMap.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java,v 1.1 2003/12/03 12:59:37 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.bidimap;
  
  import java.util.Collection;
  import java.util.Map;
  import java.util.Set;
  
  import org.apache.commons.collections.BidiMap;
  import org.apache.commons.collections.MapIterator;
  import org.apache.commons.collections.Unmodifiable;
  import org.apache.commons.collections.collection.UnmodifiableCollection;
  import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
  import org.apache.commons.collections.map.UnmodifiableEntrySet;
  import org.apache.commons.collections.set.UnmodifiableSet;
  
  /**
   * Decorates another <code>BidiMap</code> to ensure it can't be altered.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/12/03 12:59:37 $
   * 
   * @author Stephen Colebourne
   */
  public final class UnmodifiableBidiMap extends AbstractBidiMapDecorator implements Unmodifiable {
      
      /** The inverse unmodifiable map */
      private UnmodifiableBidiMap inverse;
  
      /**
       * Factory method to create an unmodifiable map.
       * 
       * @param map  the map to decorate, must not be null
       * @throws IllegalArgumentException if map is null
       */
      public static BidiMap decorate(BidiMap map) {
          if (map instanceof Unmodifiable) {
              return map;
          }
          return new UnmodifiableBidiMap(map);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Constructor that wraps (not copies).
       * 
       * @param map  the map to decorate, must not be null
       * @throws IllegalArgumentException if map is null
       */
      private UnmodifiableBidiMap(BidiMap map) {
          super(map);
      }
  
      //-----------------------------------------------------------------------
      public void clear() {
          throw new UnsupportedOperationException();
      }
  
      public Object put(Object key, Object value) {
          throw new UnsupportedOperationException();
      }
  
      public void putAll(Map mapToCopy) {
          throw new UnsupportedOperationException();
      }
  
      public Object remove(Object key) {
          throw new UnsupportedOperationException();
      }
  
      public Set entrySet() {
          Set set = super.entrySet();
          return UnmodifiableEntrySet.decorate(set);
      }
  
      public Set keySet() {
          Set set = super.keySet();
          return UnmodifiableSet.decorate(set);
      }
  
      public Collection values() {
          Collection coll = super.values();
          return UnmodifiableCollection.decorate(coll);
      }
  
      //-----------------------------------------------------------------------
      public Object removeValue(Object value) {
          throw new UnsupportedOperationException();
      }
  
      public MapIterator mapIterator() {
          MapIterator it = getBidiMap().mapIterator();
          return UnmodifiableMapIterator.decorate(it);
      }
  
      public BidiMap inverseBidiMap() {
          if (inverse == null) {
              inverse = new UnmodifiableBidiMap(getBidiMap().inverseBidiMap());
              inverse.inverse = this;
          }
          return inverse;
      }
      
  }
  
  
  

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