Author: skestle
Date: Thu Aug 30 22:13:56 2007
New Revision: 571381

URL: http://svn.apache.org/viewvc?rev=571381&view=rev
Log:
Generified LazyMap

Modified:
    
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java
    
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/LazyMap.java
    
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/AbstractTestMap.java
    
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestAll.java
    
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestLazyMap.java

Modified: 
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java?rev=571381&r1=571380&r2=571381&view=diff
==============================================================================
--- 
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java
 (original)
+++ 
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java
 Thu Aug 30 22:13:56 2007
@@ -64,10 +64,10 @@
      * This could be useful during testing as a placeholder.
      *
      * @see org.apache.commons.collections.functors.ConstantFactory
-     * 
+     * @param <T> the "type" of null object the factory should return.
      * @return the factory
      */
-    public static Factory nullFactory() {
+    public static <T> Factory<T> nullFactory() {
         return ConstantFactory.NULL_INSTANCE;
     }
 

Modified: 
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/LazyMap.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/LazyMap.java?rev=571381&r1=571380&r2=571381&view=diff
==============================================================================
--- 
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/LazyMap.java
 (original)
+++ 
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/LazyMap.java
 Thu Aug 30 22:13:56 2007
@@ -62,15 +62,15 @@
  * @author Stephen Colebourne
  * @author Paul Jack
  */
-public class LazyMap
-        extends AbstractMapDecorator
-        implements Map, Serializable {
+public class LazyMap<K,V>
+        extends AbstractMapDecorator<K,V>
+        implements Map<K,V>, Serializable {
 
     /** Serialization version */
     private static final long serialVersionUID = 7990956402564206740L;
 
     /** The factory to use to construct elements */
-    protected final Transformer factory;
+    protected final Transformer<? super K, ? extends V> factory;
 
     /**
      * Factory method to create a lazily instantiated map.
@@ -78,9 +78,11 @@
      * @param map  the map to decorate, must not be null
      * @param factory  the factory to use, must not be null
      * @throws IllegalArgumentException if map or factory is null
+     * @deprecated use [EMAIL PROTECTED] #getLazyMap(Map, Factory)} instead.
      */
-    public static Map decorate(Map map, Factory factory) {
-        return new LazyMap(map, factory);
+    @Deprecated
+    public static <K,V> Map<K,V> decorate(Map<K,V> map, Factory<? extends V> 
factory) {
+        return getLazyMap(map, factory);
     }
 
     /**
@@ -90,10 +92,34 @@
      * @param factory  the factory to use, must not be null
      * @throws IllegalArgumentException if map or factory is null
      */
-    public static Map decorate(Map map, Transformer factory) {
-        return new LazyMap(map, factory);
+    public static <K, V> LazyMap<K, V> getLazyMap(Map<K, V> map, Factory< ? 
extends V> factory) {
+               return new LazyMap<K,V>(map, factory);
+       }
+
+    /**
+     * Factory method to create a lazily instantiated map.
+     * 
+     * @param map  the map to decorate, must not be null
+     * @param factory  the factory to use, must not be null
+     * @throws IllegalArgumentException if map or factory is null
+     * @deprecated use [EMAIL PROTECTED] #getLazyMap(Map, Transformer)} 
instead.
+     */
+       @Deprecated
+    public static <K,V> Map<K,V> decorate(Map<K,V> map, Transformer<? super K, 
? extends V> factory) {
+        return getLazyMap(map, factory);
     }
 
+       /**
+     * Factory method to create a lazily instantiated map.
+     * 
+     * @param map  the map to decorate, must not be null
+     * @param factory  the factory to use, must not be null
+     * @throws IllegalArgumentException if map or factory is null
+     */
+       public static <V, K> LazyMap<K, V> getLazyMap(Map<K, V> map, 
Transformer<? super K, ? extends V> factory) {
+               return new LazyMap<K,V>(map, factory);
+       }
+
     //-----------------------------------------------------------------------
     /**
      * Constructor that wraps (not copies).
@@ -102,7 +128,7 @@
      * @param factory  the factory to use, must not be null
      * @throws IllegalArgumentException if map or factory is null
      */
-    protected LazyMap(Map map, Factory factory) {
+    protected LazyMap(Map<K,V> map, Factory<? extends V> factory) {
         super(map);
         if (factory == null) {
             throw new IllegalArgumentException("Factory must not be null");
@@ -117,7 +143,7 @@
      * @param factory  the factory to use, must not be null
      * @throws IllegalArgumentException if map or factory is null
      */
-    protected LazyMap(Map map, Transformer factory) {
+    protected LazyMap(Map<K,V> map, Transformer<? super K, ? extends V> 
factory) {
         super(map);
         if (factory == null) {
             throw new IllegalArgumentException("Factory must not be null");
@@ -146,21 +172,35 @@
      * @throws ClassNotFoundException
      * @since Commons Collections 3.1
      */
-    private void readObject(ObjectInputStream in) throws IOException, 
ClassNotFoundException {
+    @SuppressWarnings("unchecked")
+       private void readObject(ObjectInputStream in) throws IOException, 
ClassNotFoundException {
         in.defaultReadObject();
         map = (Map) in.readObject();
     }
 
     //-----------------------------------------------------------------------
-    public Object get(Object key) {
+    @Override
+       public V get(Object key) {
         // create value for key if key is not currently in the map
         if (map.containsKey(key) == false) {
-            Object value = factory.transform(key);
-            map.put(key, value);
+               K castKey = cast(key);
+            V value = factory.transform(castKey);
+            map.put(castKey, value);
             return value;
         }
         return map.get(key);
     }
+
+    /**
+     * Method just to cast [EMAIL PROTECTED] Object}s to K where necessary.  
This is done to ensure that the SuppressWarnings does not 
+     * cover other stuff that it shouldn't
+     * @param key .
+     * @return the cast key.
+     */
+       @SuppressWarnings("unchecked")
+       private K cast(Object key) {
+               return (K) key;
+       }
 
     // no need to wrap keySet, entrySet or values as they are views of
     // existing map entries - you can't do a map-style get on them.

Modified: 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/AbstractTestMap.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/AbstractTestMap.java?rev=571381&r1=571380&r2=571381&view=diff
==============================================================================
--- 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/AbstractTestMap.java
 (original)
+++ 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/AbstractTestMap.java
 Thu Aug 30 22:13:56 2007
@@ -397,7 +397,7 @@
      * 
      * @return the map to be tested
      */
-    public abstract Map makeEmptyMap();
+    public abstract <K,V> Map<K,V> makeEmptyMap();
 
     /**
      * Return a new, populated map.  The mappings in the map should match the

Modified: 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestAll.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestAll.java?rev=571381&r1=571380&r2=571381&view=diff
==============================================================================
--- 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestAll.java
 (original)
+++ 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestAll.java
 Thu Aug 30 22:13:56 2007
@@ -16,9 +16,11 @@
  */
 package org.apache.commons.collections.map;
 
-import junit.framework.Test;
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
 
 /**
  * Entry point for tests.
@@ -27,51 +29,38 @@
  * @version $Revision$ $Date$
  * 
  * @author Stephen Colebourne
+ * @author Stephen Kestle
  */
-public class TestAll extends TestCase {
-    
-    public TestAll(String testName) {
-        super(testName);
-    }
-
-    public static void main(String args[]) {
-        String[] testCaseName = { TestAll.class.getName() };
-        junit.textui.TestRunner.main(testCaseName);
-    }
[EMAIL PROTECTED](Suite.class)
[EMAIL PROTECTED]({
+       TestCaseInsensitiveMap.class,
+    TestCompositeMap.class,
+    TestDefaultedMap.class,
+    TestFlat3Map.class,
+    TestHashedMap.class,
+    TestIdentityMap.class,
+    TestLinkedMap.class,
+    TestLRUMap.class,
+    TestMultiKeyMap.class,
+    TestReferenceMap.class,
+    TestReferenceIdentityMap.class,
+    TestStaticBucketMap.class,
+    TestSingletonMap.class,
     
-    public static Test suite() {
-        TestSuite suite = new TestSuite();
-        
-        suite.addTest(TestCaseInsensitiveMap.suite());
-        suite.addTest(TestCompositeMap.suite());
-        suite.addTest(TestDefaultedMap.suite());
-        suite.addTest(TestFlat3Map.suite());
-        suite.addTest(TestHashedMap.suite());
-        suite.addTest(TestIdentityMap.suite());
-        suite.addTest(TestLinkedMap.suite());
-        suite.addTest(TestLRUMap.suite());
-        suite.addTest(TestMultiKeyMap.suite());
-        suite.addTest(TestReferenceMap.suite());
-        suite.addTest(TestReferenceIdentityMap.suite());
-        suite.addTest(TestStaticBucketMap.suite());
-        suite.addTest(TestSingletonMap.suite());
-        
-        suite.addTest(TestFixedSizeMap.suite());
-        suite.addTest(TestFixedSizeSortedMap.suite());
-        suite.addTest(TestLazyMap.suite());
-        suite.addTest(TestLazySortedMap.suite());
-        suite.addTest(TestListOrderedMap.suite());
-        suite.addTest(TestListOrderedMap2.suite());
-        suite.addTest(TestMultiValueMap.suite());
-        suite.addTest(TestPredicatedMap.suite());
-        suite.addTest(TestPredicatedSortedMap.suite());
-        suite.addTest(TestTransformedMap.suite());
-        suite.addTest(TestTransformedSortedMap.suite());
-        suite.addTest(TestUnmodifiableMap.suite());
-        suite.addTest(TestUnmodifiableOrderedMap.suite());
-        suite.addTest(TestUnmodifiableSortedMap.suite());
-        
-        return suite;
-    }
-
+    TestFixedSizeMap.class,
+    TestFixedSizeSortedMap.class,
+    TestLazyMap.class,
+    TestLazySortedMap.class,
+    TestListOrderedMap.class,
+    TestListOrderedMap2.class,
+    TestMultiValueMap.class,
+    TestPredicatedMap.class,
+    TestPredicatedSortedMap.class,
+    TestTransformedMap.class,
+    TestTransformedSortedMap.class,
+    TestUnmodifiableMap.class,
+    TestUnmodifiableOrderedMap.class,
+    TestUnmodifiableSortedMap.class
+})
+public class TestAll extends TestCase {
 }

Modified: 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestLazyMap.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestLazyMap.java?rev=571381&r1=571380&r2=571381&view=diff
==============================================================================
--- 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestLazyMap.java
 (original)
+++ 
commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/map/TestLazyMap.java
 Thu Aug 30 22:13:56 2007
@@ -16,14 +16,15 @@
  */
 package org.apache.commons.collections.map;
 
+import static org.apache.commons.collections.map.LazyMap.getLazyMap;
+
 import java.util.HashMap;
 import java.util.Map;
 
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
 import org.apache.commons.collections.Factory;
 import org.apache.commons.collections.FactoryUtils;
+import org.apache.commons.collections.Transformer;
+import org.junit.Test;
 
 /**
  * Extension of [EMAIL PROTECTED] TestMap} for exercising the 
@@ -36,55 +37,64 @@
  */
 public class TestLazyMap extends AbstractTestMap {
     
-    protected static final Factory oneFactory = 
FactoryUtils.constantFactory("One");
-    protected static final Factory nullFactory = FactoryUtils.nullFactory();
+       private static final Factory<Integer> oneFactory = 
FactoryUtils.constantFactory(1);
+    private static final Factory<Object> nullFactory = 
FactoryUtils.nullFactory();
     
     public TestLazyMap(String testName) {
         super(testName);
     }
     
-    public static Test suite() {
-        return new TestSuite(TestLazyMap.class);
-    }
-    
     public static void main(String args[]) {
         String[] testCaseName = { TestLazyMap.class.getName()};
         junit.textui.TestRunner.main(testCaseName);
     }
 
-    //-----------------------------------------------------------------------  
  
-    protected Map decorateMap(Map map, Factory factory) {
-        return LazyMap.decorate(map, factory);
+    @Override
+       public <K,V> Map<K,V> makeEmptyMap() {
+        return getLazyMap(new HashMap<K,V>(), FactoryUtils.<V>nullFactory());
     }
     
-    public Map makeEmptyMap() {
-        return decorateMap(new HashMap(), nullFactory);
+    //-----------------------------------------------------------------------
+    @Override
+    public void testMapGet() {
+       //TODO eliminate need for this via superclass - see svn history.
     }
     
-    protected Map makeTestMap(Factory factory) {
-        return decorateMap(new HashMap(), factory);
-    }
-
-    //-----------------------------------------------------------------------  
  
-    public void testMapGet() {
-        Map map = makeTestMap(oneFactory);
+    @Test
+    public void mapGetWithFactory() {
+        Map<Integer, Number> map = getLazyMap(new HashMap<Integer,Number>(), 
oneFactory);
         assertEquals(0, map.size());
-        String s1 = (String) map.get("Five");
-        assertEquals("One", s1);
+        Number i1 = map.get("Five");
+        assertEquals(1, i1);
         assertEquals(1, map.size());
-        String s2 = (String) map.get(new String(new char[] {'F','i','v','e'}));
-        assertEquals("One", s2);
+        Number i2 = map.get(new String(new char[] {'F','i','v','e'}));
+        assertEquals(1, i2);
         assertEquals(1, map.size());
-        assertSame(s1, s2);
+        assertSame(i1, i2);
         
-        map = makeTestMap(nullFactory);
+        map = getLazyMap(new HashMap<Integer,Number>(), 
FactoryUtils.<Long>nullFactory());
         Object o = map.get("Five");
         assertEquals(null,o);
         assertEquals(1, map.size());
-        
     }
     
-    public String getCompatibilityVersion() {
+    @Test
+    public void mapGetWithTransformer() {
+       Transformer<Number, Integer> intConverter = new Transformer<Number, 
Integer>(){
+                       public Integer transform(Number input) {
+                               return input.intValue();
+                       }
+       };
+               Map<Long, Number> map = getLazyMap(new HashMap<Long,Number>(), 
intConverter );
+       assertEquals(0, map.size());
+       Number i1 = map.get(123L);
+       assertEquals(123, i1);
+       assertEquals(1, map.size());
+    }
+    
+    
+    @Override
+       public String getCompatibilityVersion() {
         return "3.1";
     }
 


Reply via email to