Package cdc.args

Class Factories

java.lang.Object
cdc.args.Factories

public final class Factories extends Object
Registry of factories.
Author:
Damien Carbonne
  • Field Details

    • PRINTER

      public static final cdc.util.debug.Printable PRINTER
  • Method Details

    • 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 Arg convertToStringValues(Arg arg)
    • 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 - when factory is 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 for cls.
      Returns:
      The factory associated to cls or  null.
      Throws:
      cdc.util.lang.NotFoundException - When reaction is FAIL and no factory is associated to cls.
    • getFactory

      public static <T> Factory<T> getFactory(Class<T> cls)
    • 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 - When args are 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 - When Args.NO_ARGS are 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 - When args are 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 - When Args.NO_ARGS are are not compliant with factory expectations.