Package cdc.args
Class Factories
- java.lang.Object
-
- cdc.args.Factories
-
public final class Factories extends Object
Registry of factories.- Author:
- Damien Carbonne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFactories.ArgsConverterInterface used to convert arguments.protected static classFactories.Printer
-
Field Summary
Fields Modifier and Type Field Description static cdc.util.debug.PrintablePRINTER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ArgconvertToStringValues(Arg arg)static ArgsconvertToStringValues(Args args)Converts arguments to strings.static Objectcreate(Class<?> cls)Creates an object.static Objectcreate(Class<?> cls, Args args)Creates an object.static Objectcreate(String className)Creates an object.static Objectcreate(String className, Args args)Creates an object.static Set<Class<?>>getClasses()static <T> Factory<T>getFactory(Class<T> cls)static <T> Factory<T>getFactory(Class<T> cls, cdc.util.lang.FailureReaction reaction)Returns the factory associated to a class.static voidregister(Factory<?> factory)Registers a factory.static voidsetConverter(Factories.ArgsConverter converter)Sets the arguments converter.
-
-
-
Method Detail
-
setConverter
public static void setConverter(Factories.ArgsConverter converter)
Sets the arguments converter.This is useful when passed arguments don't have the expected types by can be converted to.
This is typically the case when arguments are loaded from a file as strings, and must be converted to something else (Integer, Float, ...) to match factory expectations.- Parameters:
converter- The converter.
-
convertToStringValues
public static Args convertToStringValues(Args args)
Converts arguments to strings.- Parameters:
args- The arguments.- Returns:
- Arguments with same names, but with all values converted to strings.
-
register
public static void register(Factory<?> factory)
Registers a factory.- Parameters:
factory- The factory.- Throws:
IllegalArgumentException- whenfactoryis invalid or another factory is already associated to the built class.
-
getClasses
public static Set<Class<?>> getClasses()
- Returns:
- A set of classes for which a factory is registered.
-
getFactory
public static <T> Factory<T> getFactory(Class<T> cls, cdc.util.lang.FailureReaction reaction)
Returns the factory associated to a class.If not factory was registered for the desired class, we try:
- To detect and register a declared one. It is searched as a Factory instance in the class.
- To create a new factory and register it. The is possible for singletons. This does not permit to attach meta parameters to the created factory.
- Type Parameters:
T- The object type.- Parameters:
cls- The class.reaction- The reaction to adopt when no factory is registered forcls.- Returns:
- The factory associated to
clsornull. - Throws:
cdc.util.lang.NotFoundException- When reaction is FAIL and no factory is associated tocls.
-
create
public static Object create(Class<?> cls, Args args)
Creates an object.- Parameters:
cls- The class of the object to create.args- The creation arguments.- Returns:
- The created object.
- Throws:
cdc.util.lang.NotFoundException- When no adequate factory is found.IllegalArgumentException- Whenargsare are not compliant with factory expectations.
-
create
public static Object create(Class<?> cls)
Creates an object.- Parameters:
cls- The class of the object to create.- Returns:
- The created object.
- Throws:
cdc.util.lang.NotFoundException- When no adequate factory is found.IllegalArgumentException- WhenArgs.NO_ARGSare are not compliant with factory expectations.
-
create
public static Object create(String className, Args args)
Creates an object.- Parameters:
className- The class name of the object to create.args- The creation arguments.- Returns:
- The created object.
- Throws:
cdc.util.lang.NotFoundException- When no adequate factory is found.IllegalArgumentException- Whenargsare are not compliant with factory expectations.
-
create
public static Object create(String className)
Creates an object.- Parameters:
className- The class name of the object to create.- Returns:
- The created object.
- Throws:
cdc.util.lang.NotFoundException- When no adequate factory is found.IllegalArgumentException- WhenArgs.NO_ARGSare are not compliant with factory expectations.
-
-