Class FactoryUtils
java.lang.Object
org.apache.commons.collections4.FactoryUtils
FactoryUtils provides reference implementations and utilities
 for the Factory functor interface. The supplied factories are:
 - Prototype - clones a specified object
- Instantiate - creates objects using reflection
- Constant - always returns the same object
- Null - always returns null
- Exception - always throws an exception
Since v4.1 only factories which are considered to be safe are Serializable. Factories considered to be unsafe for serialization are:
- Prototype
- Instantiate
- Since:
- 3.0
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> Factory<T> constantFactory(T constantToReturn) Creates a Factory that will return the same object each time the factory is used.static <T> Factory<T> Gets a Factory that always throws an exception.static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate) Creates a Factory that can create objects of a specific type using a no-args constructor.static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate, Class<?>[] paramTypes, Object[] args) Creates a Factory that can create objects of a specific type using the arguments specified to this method.static <T> Factory<T> Gets a Factory that will return null each time the factory is used.static <T> Factory<T> prototypeFactory(T prototype) Creates a Factory that will return a clone of the same prototype object each time the factory is used.
- 
Method Details- 
constantFactoryCreates 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 use the prototype factory.- Type Parameters:
- T- the type that the factory creates
- Parameters:
- constantToReturn- the constant object to return each time in the factory
- Returns:
- the constantfactory.
- See Also:
 
- 
exceptionFactoryGets a Factory that always throws an exception. This could be useful during testing as a placeholder.- Type Parameters:
- T- the type that the factory creates
- Returns:
- the factory
- See Also:
 
- 
instantiateFactoryCreates a Factory that can create objects of a specific type using a no-args constructor.- Type Parameters:
- T- the type that the factory creates
- Parameters:
- classToInstantiate- the Class to instantiate each time in the factory
- Returns:
- the reflectionfactory
- Throws:
- NullPointerException- if the classToInstantiate is null
- See Also:
 
- 
instantiateFactorypublic static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate, Class<?>[] paramTypes, Object[] args) Creates a Factory that can create objects of a specific type using the arguments specified to this method.- Type Parameters:
- T- the type that the factory creates
- Parameters:
- classToInstantiate- the Class to instantiate each time in the factory
- paramTypes- parameter types for the constructor, can be null
- args- the arguments to pass to the constructor, can be null
- Returns:
- the reflectionfactory
- Throws:
- NullPointerException- if the classToInstantiate is null
- IllegalArgumentException- if the paramTypes and args don't match
- IllegalArgumentException- if the constructor doesn't exist
- See Also:
 
- 
nullFactoryGets a Factory that will return null each time the factory is used. This could be useful during testing as a placeholder.- Type Parameters:
- T- the "type" of null object the factory should return.
- Returns:
- the factory
- See Also:
 
- 
prototypeFactoryCreates 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):- public clone method
- public copy constructor
- serialization clone
 - Type Parameters:
- T- the type that the factory creates
- Parameters:
- prototype- the object to clone each time in the factory
- Returns:
- the prototypefactory, or aConstantFactory.NULL_INSTANCEif theprototypeisnull
- Throws:
- IllegalArgumentException- if the prototype cannot be cloned
- See Also:
 
 
-