commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skes...@apache.org
Subject svn commit: r570378 - in /commons/proper/collections/branches/collections_jdk5_branch/src: java/org/apache/commons/collections/ java/org/apache/commons/collections/functors/ test/org/apache/commons/collections/
Date Tue, 28 Aug 2007 11:03:41 GMT
Author: skestle
Date: Tue Aug 28 04:03:40 2007
New Revision: 570378

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

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/functors/InstantiateFactory.java
    commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestFactoryUtils.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=570378&r1=570377&r2=570378&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
Tue Aug 28 04:03:40 2007
@@ -117,7 +117,7 @@
      * @return the <code>reflection</code> factory
      * @throws IllegalArgumentException if the classToInstantiate is null
      */
-    public static Factory instantiateFactory(Class classToInstantiate) {
+    public static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate)
{
         return InstantiateFactory.getInstance(classToInstantiate, null, null);
     }
 
@@ -135,7 +135,7 @@
      * @throws IllegalArgumentException if the paramTypes and args don't match
      * @throws IllegalArgumentException if the constructor doesn't exist
      */
-    public static Factory instantiateFactory(Class classToInstantiate, Class[] paramTypes,
Object[] args) {
+    public static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate,
Class[] paramTypes, Object[] args) {
         return InstantiateFactory.getInstance(classToInstantiate, paramTypes, args);
     }
 

Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/functors/InstantiateFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/functors/InstantiateFactory.java?rev=570378&r1=570377&r2=570378&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/functors/InstantiateFactory.java
(original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/functors/InstantiateFactory.java
Tue Aug 28 04:03:40 2007
@@ -31,19 +31,19 @@
  *
  * @author Stephen Colebourne
  */
-public class InstantiateFactory implements Factory, Serializable {
+public class InstantiateFactory<T> implements Factory<T>, Serializable {
 
     /** The serial version */
     private static final long serialVersionUID = -7732226881069447957L;
 
     /** The class to create */
-    private final Class iClassToInstantiate;
+    private final Class<T> iClassToInstantiate;
     /** The constructor parameter types */
     private final Class[] iParamTypes;
     /** The constructor arguments */
     private final Object[] iArgs;
     /** The constructor */
-    private transient Constructor iConstructor = null;
+    private transient Constructor<T> iConstructor = null;
 
     /**
      * Factory method that performs validation.
@@ -53,7 +53,7 @@
      * @param args  the constructor arguments
      * @return a new instantiate factory
      */
-    public static Factory getInstance(Class classToInstantiate, Class[] paramTypes, Object[]
args) {
+    public static <T> Factory<T> getInstance(Class<T> classToInstantiate,
Class[] paramTypes, Object[] args) {
         if (classToInstantiate == null) {
             throw new IllegalArgumentException("Class to instantiate must not be null");
         }
@@ -64,12 +64,11 @@
         }
 
         if (paramTypes == null || paramTypes.length == 0) {
-            return new InstantiateFactory(classToInstantiate);
-        } else {
-            paramTypes = (Class[]) paramTypes.clone();
-            args = (Object[]) args.clone();
-            return new InstantiateFactory(classToInstantiate, paramTypes, args);
+            return new InstantiateFactory<T>(classToInstantiate);
         }
+        paramTypes = paramTypes.clone();
+        args = args.clone();
+        return new InstantiateFactory<T>(classToInstantiate, paramTypes, args);
     }
 
     /**
@@ -78,7 +77,7 @@
      * 
      * @param classToInstantiate  the class to instantiate
      */
-    public InstantiateFactory(Class classToInstantiate) {
+    public InstantiateFactory(Class<T> classToInstantiate) {
         super();
         iClassToInstantiate = classToInstantiate;
         iParamTypes = null;
@@ -94,7 +93,7 @@
      * @param paramTypes  the constructor parameter types, not cloned
      * @param args  the constructor arguments, not cloned
      */
-    public InstantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args)
{
+    public InstantiateFactory(Class<T> classToInstantiate, Class[] paramTypes, Object[]
args) {
         super();
         iClassToInstantiate = classToInstantiate;
         iParamTypes = paramTypes;
@@ -119,7 +118,7 @@
      * 
      * @return the new object
      */
-    public Object create() {
+    public T create() {
         // needed for post-serialization
         if (iConstructor == null) {
             findConstructor();

Modified: commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestFactoryUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestFactoryUtils.java?rev=570378&r1=570377&r2=570378&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestFactoryUtils.java
(original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestFactoryUtils.java
Tue Aug 28 04:03:40 2007
@@ -26,11 +26,8 @@
 import java.util.Date;
 import java.util.TimeZone;
 
-import junit.framework.Test;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
 import org.apache.commons.collections.functors.ConstantFactory;
+import org.junit.Test;
 
 /**
  * Tests the org.apache.commons.collections.FactoryUtils class.
@@ -50,21 +47,6 @@
     }
 
     /**
-     * Main.
-     * @param args
-     */    
-    public static void main(String[] args) {
-        TestRunner.run(suite());
-    }
-
-    /**
-     * Return class as a test suite.
-     */
-    public static Test suite() {
-        return new TestSuite(TestFactoryUtils.class);
-    }
-
-    /**
      * Set up instance variables required by this test case.
      */
     public void setUp() {
@@ -263,54 +245,40 @@
     // instantiateFactory
     //------------------------------------------------------------------
     
-    public void testInstantiateFactoryNull() {
-        try {
-            Factory factory = FactoryUtils.instantiateFactory(null);
-            
-        } catch (IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+    @Test(expected=IllegalArgumentException.class)
+    public void instantiateFactoryNull() {
+        FactoryUtils.instantiateFactory(null);
     }
 
-    public void testInstantiateFactorySimple() {
-        Factory factory = FactoryUtils.instantiateFactory(Mock3.class);
+    @Test
+    public void instantiateFactorySimple() {
+        Factory<Mock3> factory = FactoryUtils.instantiateFactory(Mock3.class);
         assertNotNull(factory);
-        Object created = factory.create();
-        assertEquals(0, ((Mock3) created).getValue());
+        Mock3 created = factory.create();
+        assertEquals(0, created.getValue());
         created = factory.create();
-        assertEquals(1, ((Mock3) created).getValue());
+        assertEquals(1, created.getValue());
     }
 
-    public void testInstantiateFactoryMismatch() {
-        try {
-            Factory factory = FactoryUtils.instantiateFactory(Date.class, null, new Object[]
{null});
-            
-        } catch (IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+    @Test(expected=IllegalArgumentException.class)
+    public void instantiateFactoryMismatch() {
+        FactoryUtils.instantiateFactory(Date.class, null, new Object[] {null});
     }
 
-    public void testInstantiateFactoryNoConstructor() {
-        try {
-            Factory factory = FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class},
new Object[] {null});
-            
-        } catch (IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+    @Test(expected=IllegalArgumentException.class)
+    public void instantiateFactoryNoConstructor() {
+        FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class}, new Object[]
{null});
     }
 
-    public void testInstantiateFactoryComplex() {
+    @Test
+    public void instantiateFactoryComplex() {
         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
         // 2nd Jan 1970
-        Factory factory = FactoryUtils.instantiateFactory(Date.class,
+        Factory<Date> factory = FactoryUtils.instantiateFactory(Date.class,
             new Class[] {Integer.TYPE, Integer.TYPE, Integer.TYPE},
             new Object[] {new Integer(70), new Integer(0), new Integer(2)});
         assertNotNull(factory);
-        Object created = factory.create();
-        assertTrue(created instanceof Date);
+        Date created = factory.create();
         // long time of 1 day (== 2nd Jan 1970)
         assertEquals(new Date(1000 * 60 * 60 * 24), created);
     }



Mime
View raw message