scolebourne 2002/07/20 07:45:07
Modified: pattern/src/java/org/apache/commons/pattern/factory
FactoryUtils.java
Log:
Add ExceptionFactory
Javadoc
File tidy
Revision Changes Path
1.7 +93 -60 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/factory/FactoryUtils.java
Index: FactoryUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/factory/FactoryUtils.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- FactoryUtils.java 17 Jul 2002 22:32:42 -0000 1.6
+++ FactoryUtils.java 20 Jul 2002 14:45:07 -0000 1.7
@@ -64,10 +64,11 @@
* <code>FactoryUtils</code> provides reference implementations and utilities
* for the Factory pattern interface. The supplied factories are:
* <ul>
- * <li>NullFactory - A factory that always returns null
- * <li>ConstantFactory - A factory that always returns the same object
- * <li>PrototypeFactory - A factory that clones a specified object
- * <li>ReflectionFactory - A factory that creates objects using reflection
+ * <li>Prototype - clones a specified object
+ * <li>Reflection - creates objects using reflection
+ * <li>Constant - always returns the same object
+ * <li>Null - always returns null
+ * <li>Exception - always throws an exception
* </ul>
* All the supplied factories are Serializable.
*
@@ -75,11 +76,15 @@
* @version $Id$
*/
public class FactoryUtils {
-
+
+ /**
+ * A factory that always throws an exception
+ */
+ private static final Factory EXCEPTION_FACTORY = new ExceptionFactory();
/**
* A factory that always returns null
*/
- public static final Factory NULL_FACTORY = new ConstantFactory(null);
+ private static final Factory NULL_FACTORY = new ConstantFactory(null);
/**
* Restructive constructor
@@ -88,16 +93,26 @@
super();
}
- /**
- * Creates a Factory that will return null each time the factory is used.
+ /**
+ * Gets a Factory that always throws an exception.
+ * This could be useful during testing as a placeholder.
*
- * @return the factory.
+ * @return the factory
+ */
+ public static Factory exceptionFactory() {
+ return EXCEPTION_FACTORY;
+ }
+
+ /**
+ * Gets a Factory that will return null each time the factory is used.
+ *
+ * @return the factory
*/
public static Factory nullFactory() {
return NULL_FACTORY;
}
- /**
+ /**
* Creates a Factory that will return the same object each time the factory
* is used. No check is made that the object is immutable. In general, only
* immutable objects should use the constant factory. Mutable objects should
@@ -110,7 +125,7 @@
return new ConstantFactory(constantToReturn);
}
- /**
+ /**
* Creates a Factory that will return a clone of the same prototype object
* each time the factory is used. The prototype will be cloned using one of these
* techniques (in order):
@@ -120,8 +135,8 @@
* <li>serialization clone
* <ul>
*
- * @param constantToReturn the constant object to return each time in the factory
- * @return the factory.
+ * @param prototype the object to clone each time in the factory
+ * @return the factory
* @throws IllegalArgumentException if the prototype is null
* @throws IllegalArgumentException if the prototype cannot be cloned
*/
@@ -133,13 +148,12 @@
try {
prototype.getClass().getMethod("clone", null);
return new PrototypeCloneFactory(prototype);
-
+
} catch (NoSuchMethodException ex) {
try {
- prototype.getClass().getConstructor(new Class[] {prototype.getClass()});
- return new ReflectionFactory(prototype.getClass(),
- new Class[] {prototype.getClass()}, new Object[] {prototype});
-
+ prototype.getClass().getConstructor(new Class[] { prototype.getClass()});
+ return new ReflectionFactory(prototype.getClass(), new Class[] { prototype.getClass()},
new Object[] { prototype });
+
} catch (NoSuchMethodException ex2) {
if (prototype instanceof Serializable) {
return new PrototypeSerializationFactory((Serializable) prototype);
@@ -149,45 +163,67 @@
throw new IllegalArgumentException("PrototypeFactory: The prototype must be cloneable");
}
- /**
+ /**
* Creates a Factory that can create objects of a specific type using
* a no-args constructor.
*
* @param classToInstantiate the Class to instantiate each time in the factory
- * @return the factory.
+ * @return the factory
* @throws IllegalArgumentException if the classToInstantiate is null
*/
public static Factory reflectionFactory(Class classToInstantiate) {
return new ReflectionFactory(classToInstantiate);
}
- /**
+ /**
* Creates a Factory that can create objects of a specific type using
* the arguments specified to this method.
*
* @param classToInstantiate the Class to instantiate each time in the factory
- * @param paramTypes parameter types for the constructor
- * @param args the arguments to pass to the constructor
- * @return the factory.
+ * @param paramTypes parameter types for the constructor, can be null
+ * @param args the arguments to pass to the constructor, can be null
+ * @return the factory
* @throws IllegalArgumentException if the classToInstantiate is null
* @throws IllegalArgumentException if the paramTypes and args don't match
* @throws IllegalArgumentException if the constructor doesn't exist
*/
- public static Factory reflectionFactory(
- Class classToInstantiate, Class[] paramTypes, Object[] args) {
+ public static Factory reflectionFactory(Class classToInstantiate, Class[] paramTypes,
Object[] args) {
return new ReflectionFactory(classToInstantiate, paramTypes, args);
}
+ // ExceptionFactory
+ //----------------------------------------------------------------------------------
+
+ /**
+ * ExceptionFactory always throws an exception
+ */
+ private static class ExceptionFactory implements Factory, Serializable {
+
+ /**
+ * Constructor
+ */
+ private ExceptionFactory() {
+ super();
+ }
+
+ /**
+ * Always throw an exception
+ */
+ public Object create() {
+ throw new FactoryException("ExceptionFactory invoked");
+ }
+ }
+
// ConstantFactory
//----------------------------------------------------------------------------------
-
+
/**
* ConstantFactory returns the same instance each time.
*/
private static class ConstantFactory implements Factory, Serializable {
-
+
private final Object iConstant;
-
+
/**
* Constructor to store constant
*/
@@ -195,7 +231,7 @@
super();
iConstant = constant;
}
-
+
/**
* Always return constant
*/
@@ -203,18 +239,17 @@
return iConstant;
}
}
-
-
+
// PrototypeCloneFactory
//----------------------------------------------------------------------------------
-
+
/**
* PrototypeCloneFactory creates objects by copying a prototype using the clone method.
*/
private static class PrototypeCloneFactory implements Factory, Serializable {
private final Object iPrototype;
private transient Method iCloneMethod;
-
+
/**
* Constructor to store prototype
*/
@@ -224,7 +259,7 @@
throw new IllegalArgumentException("PrototypeCloneFactory: The prototype
must not be null");
}
iPrototype = prototype;
-
+
findCloneMethod();
}
@@ -234,12 +269,12 @@
private void findCloneMethod() {
try {
iCloneMethod = iPrototype.getClass().getMethod("clone", null);
-
+
} catch (NoSuchMethodException ex) {
throw new IllegalArgumentException("PrototypeCloneFactory: The clone method
must exist and be public ");
}
}
-
+
/**
* Return clone of prototype
*/
@@ -251,7 +286,7 @@
try {
return iCloneMethod.invoke(iPrototype, null);
-
+
} catch (IllegalAccessException ex) {
throw new FactoryException("ReflectionFactory: Clone method must be public",
ex);
} catch (InvocationTargetException ex) {
@@ -259,18 +294,17 @@
}
}
}
-
-
+
// PrototypeSerializationFactory
//----------------------------------------------------------------------------------
-
+
/**
* PrototypeSerializationFactory creates objects by cloning a prototype using serialization.
*/
private static class PrototypeSerializationFactory implements Factory, Serializable
{
-
+
private final Serializable iPrototype;
-
+
/**
* Constructor to store prototype
*/
@@ -281,29 +315,28 @@
}
iPrototype = prototype;
}
-
+
/**
* Return clone of prototype by serialization
*/
public Object create() {
try {
return SerializationUtils.clone(iPrototype);
-
+
} catch (SerializationException ex) {
throw new FactoryException("PrototypeSerializationFactory: Unable to clone
by serialization", ex);
}
}
}
-
-
+
// ReflectionFactory
//----------------------------------------------------------------------------------
-
+
/**
* ReflectionFactory creates objects using reflection.
*/
private static class ReflectionFactory implements Factory, Serializable {
-
+
private final Class iClassToInstantiate;
private final Class[] iParamTypes;
private final Object[] iArgs;
@@ -322,18 +355,18 @@
public ReflectionFactory(Class classToInstantiate, Class[] paramTypes, Object[]
args) {
super();
if (classToInstantiate == null) {
- throw new IllegalArgumentException("ReflectionFactory: The classToInstantiate
must not be null");
+ throw new IllegalArgumentException("ReflectionFactory: The class to instantiate
must not be null");
}
- if (((paramTypes == null) && (args != null)) ||
- ((paramTypes != null) && (args == null)) ||
- ((paramTypes != null) && (args != null) && (paramTypes.length
!= args.length))) {
- throw new IllegalArgumentException("ReflectionFactory: The paramTypes must
match the args");
+ if (((paramTypes == null) && (args != null))
+ || ((paramTypes != null) && (args == null))
+ || ((paramTypes != null) && (args != null) && (paramTypes.length
!= args.length))) {
+ throw new IllegalArgumentException("ReflectionFactory: The parameter types
must match the arguments");
}
-
+
iClassToInstantiate = classToInstantiate;
iParamTypes = paramTypes;
iArgs = args;
-
+
findConstructor();
}
@@ -343,12 +376,12 @@
private void findConstructor() {
try {
iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
-
+
} catch (NoSuchMethodException ex) {
throw new IllegalArgumentException("ReflectionFactory: The constructor
must exist and be public ");
}
}
-
+
/**
* Create the object using a constructor
*/
@@ -360,7 +393,7 @@
try {
return iConstructor.newInstance(iArgs);
-
+
} catch (InstantiationException ex) {
throw new FactoryException("ReflectionFactory: InstantiationException",
ex);
} catch (IllegalAccessException ex) {
@@ -370,5 +403,5 @@
}
}
}
-
+
}
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
|