Generified InstantiateFactory
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/collections_jdk5_branch@570378 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
70f2da7d57
commit
f51f239178
|
@ -117,7 +117,7 @@ public class FactoryUtils {
|
|||
* @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 @@ public class FactoryUtils {
|
|||
* @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);
|
||||
}
|
||||
|
||||
|
|
|
@ -31,19 +31,19 @@ import org.apache.commons.collections.FunctorException;
|
|||
*
|
||||
* @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 @@ public class InstantiateFactory implements Factory, Serializable {
|
|||
* @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 @@ public class InstantiateFactory implements Factory, Serializable {
|
|||
}
|
||||
|
||||
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 @@ public class InstantiateFactory implements Factory, Serializable {
|
|||
*
|
||||
* @param classToInstantiate the class to instantiate
|
||||
*/
|
||||
public InstantiateFactory(Class classToInstantiate) {
|
||||
public InstantiateFactory(Class<T> classToInstantiate) {
|
||||
super();
|
||||
iClassToInstantiate = classToInstantiate;
|
||||
iParamTypes = null;
|
||||
|
@ -94,7 +93,7 @@ public class InstantiateFactory implements Factory, Serializable {
|
|||
* @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 @@ public class InstantiateFactory implements Factory, Serializable {
|
|||
*
|
||||
* @return the new object
|
||||
*/
|
||||
public Object create() {
|
||||
public T create() {
|
||||
// needed for post-serialization
|
||||
if (iConstructor == null) {
|
||||
findConstructor();
|
||||
|
|
|
@ -26,11 +26,8 @@ import java.io.Serializable;
|
|||
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.
|
||||
|
@ -49,21 +46,6 @@ public class TestFactoryUtils extends junit.framework.TestCase {
|
|||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
@ -263,54 +245,40 @@ public class TestFactoryUtils extends junit.framework.TestCase {
|
|||
// 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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue