Class Container

  • Direct Known Subclasses:
    ReactiveContainer

    public class Container
    extends Object
    This class is used to manage the components of the Java function.
    Version:
    1.0.0
    Author:
    Ramon Rangel Osorio <ramon.rangel@protonmail.com>
    • Constructor Detail

      • Container

        protected Container()
    • Method Detail

      • getInstance

        public static Container getInstance()
        Returns an instance of the Container class.
        Returns:
        an instance of the Container class
      • getComponents

        public Map<Class<?>,​Map<String,​Object>> getComponents()
        Retrieves the components of the Java function.
        Returns:
        a map containing the components of the function, where the keys are the classes and the values are maps containing the component names and their corresponding values
      • getComponent

        public static Object getComponent​(Class<?> clazz)
        Retrieves the component of the specified class from the components map.
        Parameters:
        clazz - the class of the component to retrieve
        Returns:
        the component of the specified class
      • getComponent

        public static Object getComponent​(String identifier)
        Retrieves a component based on the given identifier.
        Parameters:
        identifier - the identifier of the component
        Returns:
        the retrieved component
      • register

        public final void register​(Class<?> clazz,
                                   String identifier)
        Registers an instance of a class with an identifier.
        Parameters:
        clazz - the class to be registered
        identifier - the identifier for the instance (optional)
      • replace

        public final void replace​(Class<?> clazz,
                                  String identifier,
                                  Object instance)
        Replaces an instance in the component map with the given class, identifier, and instance.
        Parameters:
        clazz - the class of the instance to replace
        identifier - the identifier of the instance to replace
        instance - the new instance to replace the existing one
      • resolver

        public final <T> T resolver​(Class<T> clazz,
                                    String identifier)
        Resolves and returns an instance of the specified class based on the given identifier.
        Type Parameters:
        T - the class type returned
        Parameters:
        clazz - the class type of the instance to be resolved
        identifier - the identifier used to lookup the instance (can be blank)
        Returns:
        the resolved instance of the specified class
        Throws:
        IllegalArgumentException - if the class or identifier is not registered as components
      • register

        protected final void register​(Class<?> clazz,
                                      String identifier,
                                      Object instance)
        Registers a class with its identifier and instance in the component registry.
        Parameters:
        clazz - the class to be registered
        identifier - the identifier of the class
        instance - the instance of the class
      • validateClasNameAndIdentifier

        protected void validateClasNameAndIdentifier​(Class<?> clazz,
                                                     String identifier)
        Validates the class name and identifier.
        Parameters:
        clazz - the class to validate
        identifier - the identifier to validate
      • createInstance

        protected Object createInstance​(Class<?> clazz)
        Creates an instance of the specified class using either the default constructor or the parameterized constructor.
        Parameters:
        clazz - the class to create an instance of
        Returns:
        the created instance
      • createInstanceByDefaultConstructor

        protected Object createInstanceByDefaultConstructor​(Constructor<?> constructor)
        Creates an instance of an object using the default constructor.
        Parameters:
        constructor - the constructor used to create the instance
        Returns:
        the created instance
      • createInstanceByParameterizedConstructor

        protected Object createInstanceByParameterizedConstructor​(Constructor<?> constructor)
        Creates an instance using the provided parameterized constructor.
        Parameters:
        constructor - the constructor to be used for creating the instance
        Returns:
        the created instance
      • createParametersInstances

        protected Object[] createParametersInstances​(Parameter... parameters)
        Creates instances for the given parameters.
        Parameters:
        parameters - the parameters for which instances need to be created
        Returns:
        an array of instances corresponding to the parameters