Class FactoryUtils


  • public class FactoryUtils
    extends Object
    FactoryUtils provides reference implementations and utilities for the Factory functor interface. The supplied factories are:
    • Prototype - clones a specified object
    • Reflection - creates objects using reflection
    • Constant - always returns the same object
    • Null - always returns null
    • Exception - always throws an exception
    All the supplied factories are Serializable.
    Since:
    Commons Collections 3.0
    Version:
    $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
    Author:
    Stephen Colebourne
    • Constructor Detail

      • FactoryUtils

        public FactoryUtils()
        This class is not normally instantiated.
    • Method Detail

      • exceptionFactory

        public static Factory exceptionFactory()
        Gets a Factory that always throws an exception. This could be useful during testing as a placeholder.
        Returns:
        the factory
        See Also:
        ExceptionFactory
      • nullFactory

        public static Factory nullFactory()
        Gets a Factory that will return null each time the factory is used. This could be useful during testing as a placeholder.
        Returns:
        the factory
        See Also:
        ConstantFactory
      • constantFactory

        public static Factory constantFactory​(Object constantToReturn)
        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 use the prototype factory.
        Parameters:
        constantToReturn - the constant object to return each time in the factory
        Returns:
        the constant factory.
        See Also:
        ConstantFactory
      • prototypeFactory

        public static Factory prototypeFactory​(Object prototype)
        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):
        • public clone method
        • public copy constructor
        • serialization clone
          Parameters:
          prototype - the object to clone each time in the factory
          Returns:
          the prototype factory
          Throws:
          IllegalArgumentException - if the prototype is null
          IllegalArgumentException - if the prototype cannot be cloned
          See Also:
          PrototypeFactory
        • instantiateFactory

          public static Factory instantiateFactory​(Class classToInstantiate)
          Creates a Factory that can create objects of a specific type using a no-args constructor.
          Parameters:
          classToInstantiate - the Class to instantiate each time in the factory
          Returns:
          the reflection factory
          Throws:
          IllegalArgumentException - if the classToInstantiate is null
          See Also:
          InstantiateFactory
        • instantiateFactory

          public static Factory instantiateFactory​(Class classToInstantiate,
                                                   Class[] paramTypes,
                                                   Object[] args)
          Creates a Factory that can create objects of a specific type using the arguments specified to this method.
          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 reflection factory
          Throws:
          IllegalArgumentException - if the classToInstantiate is null
          IllegalArgumentException - if the paramTypes and args don't match
          IllegalArgumentException - if the constructor doesn't exist
          See Also:
          InstantiateFactory