Top Description Constructors Methods
org.apache.commons.collections4

public Class FactoryUtils

extends Object
Class Inheritance
Imports
org.apache.commons.collections4.functors.ConstantFactory, .ExceptionFactory, .InstantiateFactory, .PrototypeFactory

FactoryUtils provides reference implementations and utilities for the Factory functor interface. The supplied factories are:

Since v4.1 only factories which are considered to be safe are Serializable. Factories considered to be unsafe for serialization are:

Since
3.0

Constructor Summary

AccessConstructor and Description
private
FactoryUtils()

This class is not normally instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the type that the factory creates
T
>
Factory<T>

Returns:

the constant factory.
constantFactory
(final T
the constant object to return each time in the factory
constantToReturn
)

Creates a Factory that will return the same object each time the factory is used.

public static <
the type that the factory creates
T
>
Factory<T>

Returns:

the factory
exceptionFactory
()

Gets a Factory that always throws an exception.

public static <
the type that the factory creates
T
>
Factory<T>

Returns:

the reflection factory
instantiateFactory
(final Class<T>
the Class to instantiate each time in the factory
classToInstantiate
)

Creates a Factory that can create objects of a specific type using a no-args constructor.

public static <
the type that the factory creates
T
>
Factory<T>

Returns:

the reflection factory
instantiateFactory
(final Class<T>
the Class to instantiate each time in the factory
classToInstantiate
,
final Class<?>[]
parameter types for the constructor, can be null
paramTypes
,
final Object[]
the arguments to pass to the constructor, can be null
args
)

Creates a Factory that can create objects of a specific type using the arguments specified to this method.

public static <
the "type" of null object the factory should return.
T
>
Factory<T>

Returns:

the factory
nullFactory
()

Gets a Factory that will return null each time the factory is used.

public static <
the type that the factory creates
T
>
Factory<T>

Returns:

the prototype factory, or a ConstantFactory#NULL_INSTANCE if the prototype is null
prototypeFactory
(final T
the object to clone each time in the factory
prototype
)

Creates a Factory that will return a clone of the same prototype object each time the factory is used.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

FactoryUtilsback to summary
private FactoryUtils()

This class is not normally instantiated.

Method Detail

constantFactoryback to summary
public static <T> Factory<T> constantFactory(final T 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
<T>
the type that the factory creates
constantToReturn:T

the constant object to return each time in the factory

Returns:Factory<T>

the constant factory.

See Also
org.apache.commons.collections4.functors.ConstantFactory
exceptionFactoryback to summary
public static <T> Factory<T> exceptionFactory()

Gets a Factory that always throws an exception. This could be useful during testing as a placeholder.

Parameters
<T>
the type that the factory creates
Returns:Factory<T>

the factory

See Also
org.apache.commons.collections4.functors.ExceptionFactory
instantiateFactoryback to summary
public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate)

Creates a Factory that can create objects of a specific type using a no-args constructor.

Parameters
<T>
the type that the factory creates
classToInstantiate:Class<T>

the Class to instantiate each time in the factory

Returns:Factory<T>

the reflection factory

Exceptions
NullPointerException:
if the classToInstantiate is null
See Also
org.apache.commons.collections4.functors.InstantiateFactory
instantiateFactoryback to summary
public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, final Object[] args)

Creates a Factory that can create objects of a specific type using the arguments specified to this method.

Parameters
<T>
the type that the factory creates
classToInstantiate:Class<T>

the Class to instantiate each time in the factory

paramTypes:Class<?>[]

parameter types for the constructor, can be null

args:Object[]

the arguments to pass to the constructor, can be null

Returns:Factory<T>

the reflection factory

Exceptions
NullPointerException:
if the classToInstantiate is null
IllegalArgumentException:
  • if the paramTypes and args don't match
  • if the constructor doesn't exist
See Also
org.apache.commons.collections4.functors.InstantiateFactory
nullFactoryback to summary
public static <T> Factory<T> nullFactory()

Gets a Factory that will return null each time the factory is used. This could be useful during testing as a placeholder.

Parameters
<T>
the "type" of null object the factory should return.
Returns:Factory<T>

the factory

See Also
org.apache.commons.collections4.functors.ConstantFactory
prototypeFactoryback to summary
public static <T> Factory<T> prototypeFactory(final T 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
<T>
the type that the factory creates
prototype:T

the object to clone each time in the factory

Returns:Factory<T>

the prototype factory, or a ConstantFactory#NULL_INSTANCE if the prototype is null

Exceptions
IllegalArgumentException:
if the prototype cannot be cloned
See Also
org.apache.commons.collections4.functors.PrototypeFactory