Class IoC


  • public final class IoC
    extends java.lang.Object
    An
    Inversion of Control
    container. It is responsible for registering and resolving services and custom mappers.
    Author:
    Collin Alpert
    • Constructor Detail

      • IoC

        public IoC()
    • Method Detail

      • createInstance

        public static <E> E createInstance​(java.lang.Class<E> clazz)
        Creates an instance of any class with an empty constructor. It is used to create a new instance of the entity classes, so they can be filled with values. This is why every entity class needs an empty constructor.
        Type Parameters:
        E - The type of the entity.
        Parameters:
        clazz - The class to be constructed.
        Returns:
        An instance of this class.
        Throws:
        java.lang.IllegalArgumentException - if the class cannot be constructed for any reason. This can occur if there is no public parameterless constructor available.
      • resolveService

        public static <S extends BaseService> S resolveService​(java.lang.Class<S> clazz)
        Resolves a service class. This is to preserve the singleton pattern. Only one instance of a service is needed in the lifecycle of an application.
        Type Parameters:
        S - The type of the service class.
        Parameters:
        clazz - The service class to retrieve.
        Returns:
        The previously registered instance of a service class.
      • resolveMapper

        public static <E extends BaseEntityIMapper<E> resolveMapper​(java.lang.Class<E> clazz)
        Resolves a mapper class. This is to preserve the singleton pattern. Only one instance of a mapper is needed in the lifecycle of an application.
        Type Parameters:
        E - The type of the entity.
        Parameters:
        clazz - The entity that the mapper was registered for.
        Returns:
        The previously registered instance of a service class.
      • resolveMapper

        public static <E extends BaseEntityIMapper<E> resolveMapper​(java.lang.Class<E> clazz,
                                                                      IMapper<E> defaultMapper)
        Resolves a mapper class. If an instance of this mapper has not been registered yet, a backup mapper is used and also registered.
        Type Parameters:
        E - The type of the mapper.
        Parameters:
        clazz - The type of the corresponding mapper.
        defaultMapper - The default mapper, in case a custom mapper is not registered for this type.
        Returns:
        The mapper for the entity. If is does not exists, the default mapper is returned.
      • resolveServiceByEntity

        public static <E extends BaseEntity,​S extends BaseService<E>> S resolveServiceByEntity​(java.lang.Class<E> clazz)
        Resolves a service class by the entity it was registered with.
        Type Parameters:
        E - The type of the entity.
        S - The type of the service.
        Parameters:
        clazz - The entity class corresponding to a service class.
        Returns:
        An instance of a previously registered service class.
      • resolveServiceByEntity

        public static <E extends BaseEntity,​S extends BaseService<E>> S resolveServiceByEntity​(java.lang.Class<E> clazz,
                                                                                                     S defaultService)
        Resolves a service class by the entity it was registered with. If an instance of this service has not been registered yet, a backup service is used and also registered.
        Type Parameters:
        E - The type of the entity.
        S - The type of the service.
        Parameters:
        clazz - The entity class corresponding to a service class.
        defaultService - The backup to use in case a service for the supplied class has not been registered yet.
        Returns:
        An instance of a previously registered service class.
      • registerService

        public static <E extends BaseEntity,​S extends BaseService<E>> void registerService​(java.lang.Class<E> clazz,
                                                                                                 S service)
        Registers an instance of a service class.
        Type Parameters:
        E - The type of the entity.
        S - The type of the service class.
        Parameters:
        clazz - An entity class.
        service - The service class this entity corresponds to.
      • registerMapper

        public static <E extends BaseEntity,​M extends IMapper<E>> void registerMapper​(java.lang.Class<E> clazz,
                                                                                            M mapper)
        Registers an instance of a mapper class.
        Type Parameters:
        E - The type of the entity.
        M - The type of the mapper class.
        Parameters:
        clazz - An entity class.
        mapper - The mapper class for the entity.
      • isServiceRegistered

        public static <E extends BaseEntity> boolean isServiceRegistered​(java.lang.Class<E> clazz)
        Checks if a service has already been registered.
        Type Parameters:
        E - The type of the service.
        Parameters:
        clazz - The class of the entity a service was registered for.
        Returns:
        True if an instance of the service is registered, false if not.
      • isMapperRegistered

        public static <E extends BaseEntity> boolean isMapperRegistered​(java.lang.Class<E> clazz)
        Checks if a mapper has already been registered.
        Type Parameters:
        E - The type of the entity.
        Parameters:
        clazz - The class of the entity a mapper was registered for.
        Returns:
        True if an instance of the mapper is registered, false if not.