scolebourne    2003/11/20 14:35:50

  Modified:    collections/src/test/org/apache/commons/collections/map
                        TestUnmodifiableMap.java TestAll.java
               collections/src/java/org/apache/commons/collections/map
                        UnmodifiableMap.java UnmodifiableSortedMap.java
  Added:       collections/src/test/org/apache/commons/collections/map
                        TestUnmodifiableSortedMap.java
                        TestUnmodifiableOrderedMap.java
               collections/src/java/org/apache/commons/collections/map
                        UnmodifiableOrderedMap.java
  Log:
  Complete unmodifiable map implementations and tests
  
  Revision  Changes    Path
  1.4       +15 -256   
jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestUnmodifiableMap.java
  
  Index: TestUnmodifiableMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestUnmodifiableMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestUnmodifiableMap.java  18 Nov 2003 22:37:17 -0000      1.3
  +++ TestUnmodifiableMap.java  20 Nov 2003 22:35:50 -0000      1.4
  @@ -57,16 +57,14 @@
    */
   package org.apache.commons.collections.map;
   
  -import java.util.ArrayList;
  -import java.util.Collection;
   import java.util.HashMap;
  -import java.util.Iterator;
   import java.util.Map;
  -import java.util.Set;
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.collections.Unmodifiable;
  +
   /**
    * Extension of [EMAIL PROTECTED] AbstractTestMap} for exercising the 
    * [EMAIL PROTECTED] UnmodifiableMap} implementation.
  @@ -115,259 +113,20 @@
           return UnmodifiableMap.decorate(m);
       }
       
  -    //--------------------------------------------------------------------
  -    protected UnmodifiableMap map = null;
  -    protected ArrayList array = null;
  -    
  -    protected void setupMap() {
  -        map = (UnmodifiableMap) makeFullMap();
  -        array = new ArrayList();
  -        array.add("one");
  -    }
  -    
  -    public void testUnmodifiableBase() {
  -        setupMap();
  -        try {
  -            map.put("key", "value");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            map.putAll(new HashMap());
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            map.clear();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            map.remove("x");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -    }
  -    
  -    /**
  -     * Verifies that the keyset is not modifiable -- effectively tests
  -     * protection of UnmodifiableSet decorator
  -     */
  -    public void testUnmodifiableKeySet() {
  -        setupMap();
  -        Set keys = map.keySet();
  -        try {
  -            keys.add("x");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            keys.addAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            keys.clear();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            keys.remove("one");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            keys.removeAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            keys.retainAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            Iterator iterator = keys.iterator();
  -            iterator.next();
  -            iterator.remove();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -    }
  -        
  -    /**
  -     * Verifies that the values collection is not modifiable -- effectively tests
  -     * protection of UnmodifiableCollection decorator
  -     */
  -    public void testUnmodifiableValues() {
  -        setupMap();
  -        Collection values = map.values();
  -        try {
  -            values.add("x");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            values.addAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            values.clear();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            values.remove("one");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            values.removeAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            values.retainAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            Iterator iterator = values.iterator();
  -            iterator.next();
  -            iterator.remove();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  +    //-----------------------------------------------------------------------
  +    public void testUnmodifiable() {
  +        assertTrue(makeEmptyMap() instanceof Unmodifiable);
  +        assertTrue(makeFullMap() instanceof Unmodifiable);
       }
       
  -    /**
  -     * Verifies that the entrySet is not modifiable -- effectively tests
  -     * protection of UnmodifiableEntrySet 
  -     */
  -    public void testUnmodifiableEntries() {
  -        setupMap();
  -        Set entries = map.entrySet();
  -        try {
  -            entries.add("x");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            entries.addAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            entries.clear();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            entries.remove("one");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            entries.removeAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            entries.retainAll(array);
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  -        try {
  -            Iterator iterator = entries.iterator();
  -            iterator.next();
  -            iterator.remove();
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }
  +    public void testDecorateFactory() {
  +        Map map = makeFullMap();
  +        assertSame(map, UnmodifiableMap.decorate(map));
           
           try {
  -            Iterator iterator = entries.iterator();
  -            Map.Entry entry = (Map.Entry) iterator.next();
  -            entry.setValue("x");
  -            fail("Expecting UnsupportedOperationException.");
  -        } catch (UnsupportedOperationException e) {
  -            // expected
  -        }                 
  +            UnmodifiableMap.decorate(null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
  -    
  -    /**
  -     * Verifies that entries consists of Map.Entries corresponding to the parallel
  -     * keys and values arrays  (not necessarily in order)
  -     */
  -    protected void checkEntries(Object[] keys, Object[] values, Object[] entries,
  -        boolean checkLengths) {
  -        if (checkLengths) {
  -            assertEquals(keys.length, entries.length);
  -        }
  -        for (int i = 0; i < keys.length; i++) {
  -            Map.Entry entry = (Map.Entry) entries[i];
  -            boolean found = false;
  -            // Can't assume entries are in insertion order, so have
  -            // to search for the key
  -            for (int j = 0; j < keys.length; j++) {
  -                if (entry.getKey() == keys[j]) {
  -                    found = true;
  -                    assertEquals(entry.getValue(), values[j]);
  -                    break;
  -                }
  -            }
  -            assertTrue(found);
  -        }
  -    }
  -    
  -    /**
  -     * Tests EntrySet toArray() implementation
  -     */
  -    public void testToArray() {
  -        setupMap();
  -        Object[] keys = getSampleKeys();
  -        Object[] values = getSampleValues();
  -        Object[] entries = map.entrySet().toArray();
  -        assertTrue(keys.length == entries.length);
  -        checkEntries(keys, values, entries, true);
  -        entries = map.entrySet().toArray(entries);
  -        checkEntries(keys, values, entries, true);
  -        Object[] testArray = new Object[2];
  -        entries = map.entrySet().toArray(testArray);
  -        checkEntries(keys, values, entries, true);
  -        testArray = new Object[50];
  -        entries = map.entrySet().toArray(testArray);
  -        checkEntries(keys, values, entries, false);
  -        assertEquals(testArray[map.size()], null);
  -        testArray = new Object[0];
  -        Object[] resultArray = new Object[0];
  -        resultArray = map.entrySet().toArray(testArray);
  -        checkEntries(keys, values, resultArray, true);   
  -    }         
  +
   }
  
  
  
  1.3       +4 -2      
jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestAll.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestAll.java      18 Nov 2003 23:23:05 -0000      1.2
  +++ TestAll.java      20 Nov 2003 22:35:50 -0000      1.3
  @@ -94,6 +94,8 @@
           suite.addTest(TestPredicatedMap.suite());
           suite.addTest(TestPredicatedSortedMap.suite());
           suite.addTest(TestUnmodifiableMap.suite());
  +        suite.addTest(TestUnmodifiableOrderedMap.suite());
  +        suite.addTest(TestUnmodifiableSortedMap.suite());
           
           return suite;
       }
  
  
  
  1.1                  
jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestUnmodifiableSortedMap.java
  
  Index: TestUnmodifiableSortedMap.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestUnmodifiableSortedMap.java,v
 1.1 2003/11/20 22:35:50 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 [EMAIL PROTECTED]
   *
   * 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.map;
  
  import java.util.Map;
  import java.util.SortedMap;
  import java.util.TreeMap;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.Unmodifiable;
  
  /**
   * Extension of [EMAIL PROTECTED] AbstractTestSortedMap} for exercising the 
   * [EMAIL PROTECTED] UnmodifiableSortedMap} implementation.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/20 22:35:50 $
   * 
   * @author Stephen Colebourne
   */
  public class TestUnmodifiableSortedMap extends AbstractTestSortedMap {
      
      public TestUnmodifiableSortedMap(String testName) {
          super(testName);
      }
      
      public static Test suite() {
          return new TestSuite(TestUnmodifiableSortedMap.class);
      }
      
      public static void main(String args[]) {
          String[] testCaseName = { TestUnmodifiableSortedMap.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
      
      //-------------------------------------------------------------------
      
      public Map makeEmptyMap() {
          return UnmodifiableSortedMap.decorate(new TreeMap());
      }
      
      public boolean isPutChangeSupported() {
          return false;
      }
      
      public boolean isPutAddSupported() {
          return false;
      }
      
      public boolean isRemoveSupported() {
          return false;
      }
      
      public Map makeFullMap() {
          SortedMap m = new TreeMap();
          addSampleMappings(m);
          return UnmodifiableSortedMap.decorate(m);
      }
      
      //-----------------------------------------------------------------------
      public void testUnmodifiable() {
          assertTrue(makeEmptyMap() instanceof Unmodifiable);
          assertTrue(makeFullMap() instanceof Unmodifiable);
      }
      
      public void testDecorateFactory() {
          Map map = makeFullMap();
          assertSame(map, UnmodifiableSortedMap.decorate((SortedMap) map));
          
          try {
              UnmodifiableSortedMap.decorate(null);
              fail();
          } catch (IllegalArgumentException ex) {}
      }
  
  }
  
  
  1.1                  
jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestUnmodifiableOrderedMap.java
  
  Index: TestUnmodifiableOrderedMap.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestUnmodifiableOrderedMap.java,v
 1.1 2003/11/20 22:35:50 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 [EMAIL PROTECTED]
   *
   * 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.map;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.Unmodifiable;
  
  /**
   * Extension of [EMAIL PROTECTED] AbstractTestOrderedMap} for exercising the 
   * [EMAIL PROTECTED] UnmodifiableOrderedMap} implementation.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/20 22:35:50 $
   * 
   * @author Stephen Colebourne
   */
  public class TestUnmodifiableOrderedMap extends AbstractTestOrderedMap {
      
      public TestUnmodifiableOrderedMap(String testName) {
          super(testName);
      }
      
      public static Test suite() {
          return new TestSuite(TestUnmodifiableOrderedMap.class);
      }
      
      public static void main(String args[]) {
          String[] testCaseName = { TestUnmodifiableOrderedMap.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
      
      //-------------------------------------------------------------------
      
      public Map makeEmptyMap() {
          return UnmodifiableOrderedMap.decorate(ListOrderedMap.decorate(new 
HashMap()));
      }
      
      public boolean isPutChangeSupported() {
          return false;
      }
      
      public boolean isPutAddSupported() {
          return false;
      }
      
      public boolean isRemoveSupported() {
          return false;
      }
      
      public Map makeFullMap() {
          OrderedMap m = ListOrderedMap.decorate(new HashMap());
          addSampleMappings(m);
          return UnmodifiableOrderedMap.decorate(m);
      }
      
      //-----------------------------------------------------------------------
      public void testUnmodifiable() {
          assertTrue(makeEmptyMap() instanceof Unmodifiable);
          assertTrue(makeFullMap() instanceof Unmodifiable);
      }
      
      public void testDecorateFactory() {
          Map map = makeFullMap();
          assertSame(map, UnmodifiableOrderedMap.decorate((OrderedMap) map));
          
          try {
              UnmodifiableOrderedMap.decorate(null);
              fail();
          } catch (IllegalArgumentException ex) {}
      }
  
  }
  
  
  1.2       +7 -3      
jakarta-commons/collections/src/java/org/apache/commons/collections/map/UnmodifiableMap.java
  
  Index: UnmodifiableMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/map/UnmodifiableMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UnmodifiableMap.java      16 Nov 2003 00:05:45 -0000      1.1
  +++ UnmodifiableMap.java      20 Nov 2003 22:35:50 -0000      1.2
  @@ -63,6 +63,7 @@
   import java.util.Map;
   import java.util.Set;
   
  +import org.apache.commons.collections.Unmodifiable;
   import org.apache.commons.collections.collection.UnmodifiableCollection;
   import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
   import org.apache.commons.collections.pairs.AbstractMapEntryDecorator;
  @@ -76,7 +77,7 @@
    * 
    * @author Stephen Colebourne
    */
  -public class UnmodifiableMap extends AbstractMapDecorator {
  +public final class UnmodifiableMap extends AbstractMapDecorator implements 
Unmodifiable {
   
       /**
        * Factory method to create an unmodifiable map.
  @@ -85,6 +86,9 @@
        * @throws IllegalArgumentException if map is null
        */
       public static Map decorate(Map map) {
  +        if (map instanceof Unmodifiable) {
  +            return map;
  +        }
           return new UnmodifiableMap(map);
       }
   
  
  
  
  1.2       +44 -11    
jakarta-commons/collections/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java
  
  Index: UnmodifiableSortedMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UnmodifiableSortedMap.java        16 Nov 2003 00:05:45 -0000      1.1
  +++ UnmodifiableSortedMap.java        20 Nov 2003 22:35:50 -0000      1.2
  @@ -57,10 +57,17 @@
    */
   package org.apache.commons.collections.map;
   
  +import java.util.Collection;
   import java.util.Comparator;
   import java.util.Map;
  +import java.util.Set;
   import java.util.SortedMap;
   
  +import org.apache.commons.collections.Unmodifiable;
  +import org.apache.commons.collections.collection.UnmodifiableCollection;
  +import org.apache.commons.collections.map.UnmodifiableMap.UnmodifiableEntrySet;
  +import org.apache.commons.collections.set.UnmodifiableSet;
  +
   /**
    * Decorates another <code>SortedMap</code> to ensure it can't be altered.
    *
  @@ -69,7 +76,7 @@
    * 
    * @author Stephen Colebourne
    */
  -public class UnmodifiableSortedMap extends UnmodifiableMap implements SortedMap {
  +public final class UnmodifiableSortedMap extends AbstractSortedMapDecorator 
implements Unmodifiable {
   
       /**
        * Factory method to create an unmodifiable sorted map.
  @@ -78,6 +85,9 @@
        * @throws IllegalArgumentException if map is null
        */
       public static SortedMap decorate(SortedMap map) {
  +        if (map instanceof Unmodifiable) {
  +            return map;
  +        }
           return new UnmodifiableSortedMap(map);
       }
   
  @@ -88,17 +98,40 @@
        * @param map  the map to decorate, must not be null
        * @throws IllegalArgumentException if map is null
        */
  -    protected UnmodifiableSortedMap(Map map) {
  +    protected UnmodifiableSortedMap(SortedMap map) {
           super(map);
       }
   
  -    /**
  -     * Gets the map being decorated.
  -     * 
  -     * @return the decorated map
  -     */
  -    protected SortedMap getSortedMap() {
  -        return (SortedMap) 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 new UnmodifiableEntrySet(set);
  +    }
  +
  +    public Set keySet() {
  +        Set set = super.keySet();
  +        return UnmodifiableSet.decorate(set);
  +    }
  +
  +    public Collection values() {
  +        Collection coll = super.values();
  +        return UnmodifiableCollection.decorate(coll);
       }
   
       //-----------------------------------------------------------------------
  
  
  
  1.1                  
jakarta-commons/collections/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java
  
  Index: UnmodifiableOrderedMap.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java,v
 1.1 2003/11/20 22:35:50 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 [EMAIL PROTECTED]
   *
   * 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.map;
  
  import java.util.Collection;
  import java.util.Map;
  import java.util.Set;
  
  import org.apache.commons.collections.Unmodifiable;
  import org.apache.commons.collections.collection.UnmodifiableCollection;
  import org.apache.commons.collections.iterators.MapIterator;
  import org.apache.commons.collections.iterators.OrderedMapIterator;
  import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
  import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator;
  import org.apache.commons.collections.map.UnmodifiableMap.UnmodifiableEntrySet;
  import org.apache.commons.collections.set.UnmodifiableSet;
  
  /**
   * Decorates another <code>OrderedMap</code> to ensure it can't be altered.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/20 22:35:50 $
   * 
   * @author Stephen Colebourne
   */
  public final class UnmodifiableOrderedMap extends AbstractOrderedMapDecorator 
implements Unmodifiable {
  
      /**
       * Factory method to create an unmodifiable sorted map.
       * 
       * @param map  the map to decorate, must not be null
       * @throws IllegalArgumentException if map is null
       */
      public static OrderedMap decorate(OrderedMap map) {
          if (map instanceof Unmodifiable) {
              return map;
          }
          return new UnmodifiableOrderedMap(map);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Constructor that wraps (not copies).
       * 
       * @param map  the map to decorate, must not be null
       * @throws IllegalArgumentException if map is null
       */
      protected UnmodifiableOrderedMap(OrderedMap map) {
          super(map);
      }
  
      //-----------------------------------------------------------------------
      public MapIterator mapIterator() {
          MapIterator it = getOrderedMap().mapIterator();
          return UnmodifiableMapIterator.decorate(it);
      }
  
      public OrderedMapIterator orderedMapIterator() {
          OrderedMapIterator it = getOrderedMap().orderedMapIterator();
          return UnmodifiableOrderedMapIterator.decorate(it);
      }
  
      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 new UnmodifiableEntrySet(set);
      }
  
      public Set keySet() {
          Set set = super.keySet();
          return UnmodifiableSet.decorate(set);
      }
  
      public Collection values() {
          Collection coll = super.values();
          return UnmodifiableCollection.decorate(coll);
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to