Class TableRegistry

java.lang.Object
org.litebridge.orm.persistence.TableRegistry

public final class TableRegistry extends Object
The TableRegistry class is a centralized registry responsible for managing the relationship between Data Transfer Object (DTO) classes and their corresponding ORM table representations (OrmTable). This registry allows for efficient management, querying, and consistency of ORM table mappings within Litebridge.

This class provides methods to: - Add and register OrmTable instances associated with DTO classes. - Retrieve OrmTable instances based on DTO class, schema, or table names. - Check for the existence of tables mapped to specific DTO classes. - Retrieve or create lightweight SPI table representations for database interactions.

Instances of this class are immutable with respect to their internal maps, ensuring thread safety and consistent state across multiple threads.

  • Constructor Details

    • TableRegistry

      public TableRegistry()
  • Method Details

    • getOrmTable

      public @Nullable OrmTable getOrmTable(Class<?> dtoClass)
      Retrieves the OrmTable associated with the specified DTO class.
      Parameters:
      dtoClass - the DTO class for which the corresponding OrmTable is to be retrieved; must not be null
      Returns:
      the OrmTable associated with the specified DTO class, or null if no table is mapped to the class
    • getOrmTableOrThrow

      public OrmTable getOrmTableOrThrow(Class<?> dtoClass) throws IllegalArgumentException
      Retrieves the OrmTable associated with the specified DTO class, or throws an exception if no table is mapped to the class.
      Parameters:
      dtoClass - the DTO class for which the corresponding OrmTable is to be retrieved; must not be null
      Returns:
      the OrmTable associated with the specified DTO class
      Throws:
      IllegalArgumentException - if no table is mapped to the class
    • getOrmTableInContext

      public @Nullable OrmTable getOrmTableInContext(Class<?> dtoClass, Class<?> contextClass)
      Retrieves an OrmTable associated with the specified DTO class within a given context class.

      This method first retrieves the OrmTable for the context class. If the context's table exists, it attempts to retrieve the OrmTable for the DTO class from the context table's registry. If no context table exists or the registry does not contain the table, the method will return an empty Optional.

      Parameters:
      dtoClass - the DTO class for which the corresponding OrmTable is to be retrieved; must not be null
      contextClass - the class representing the context within which the OrmTable is being resolved; must not be null
      Returns:
      an Optional containing the OrmTable associated with the specified DTO class in the context of the given context class, or an empty Optional if no such table is found
    • getOrmTableInContextOrThrow

      public OrmTable getOrmTableInContextOrThrow(Class<?> dtoClass, Class<?> contextClass)
      Retrieves the OrmTable associated with the specified DTO class within the context of a given context class.

      This method first resolves the OrmTable for the context class and then fetches the OrmTable for the DTO class from the context table's registry. If no table is mapped to either class, an exception is thrown.

      Parameters:
      dtoClass - the DTO class for which the corresponding OrmTable is to be retrieved; must not be null
      contextClass - the class representing the context within which the OrmTable is to be resolved; must not be null
      Returns:
      the OrmTable associated with the specified DTO class in the context of the given context class
      Throws:
      IllegalArgumentException - if no OrmTable is mapped to the context class or the DTO class
    • getOrmTable

      public @Nullable OrmTable getOrmTable(String table)
      Retrieves the OrmTable associated with the specified table name.
      Parameters:
      table - the table name in the format "schema.table" or unqualified "table"
      Returns:
      the OrmTable associated with the specified table name, or null if not found
    • getOrmTableOrThrow

      public OrmTable getOrmTableOrThrow(String table)
      Retrieves the OrmTable associated with the specified table name, throwing an exception if not found.
      Parameters:
      table - the table name, optionally schema-qualified
      Returns:
      the associated OrmTable
      Throws:
      NullPointerException - if the table is not found
    • getOrmTable

      public @Nullable OrmTable getOrmTable(@Nullable Table table)
      Retrieves the OrmTable associated with the specified table.
      Parameters:
      table - the table
      Returns:
      the OrmTable associated with the specified table, or null if not found
    • getOrmTableOrThrow

      public OrmTable getOrmTableOrThrow(Table table)
      Retrieves the OrmTable associated with the specified SPI table, throwing an exception if not found.
      Parameters:
      table - the SPI table
      Returns:
      the associated OrmTable
      Throws:
      NullPointerException - if the table is not found
    • containsOrmTable

      public boolean containsOrmTable(Class<?> dtoClass)
      Checks if a table associated with the specified DTO class exists in the registry.
      Parameters:
      dtoClass - the DTO class for which the presence of an associated table is to be checked; must not be null
      Returns:
      true if a table is mapped to the specified DTO class, false otherwise
    • addTable

      public void addTable(Class<?> dtoClass, OrmTable ormTable)
      Adds an ORM table to the registry.
      Parameters:
      dtoClass - the DTO class for which the table is to be associated
      ormTable - the ORM table to register
    • addTable

      public void addTable(OrmTable ormTable)
      Adds a table to the registry, but do not associate it with a root DTO class.

      The resulting ORM table will only be able to be queried by name from this registry.

      Parameters:
      ormTable - the table to register
    • getOrCreateSpiTable

      public Table getOrCreateSpiTable(String table)
      Returns the SPI table for the specified table name.
      Parameters:
      table - the table name
      Returns:
      the SPI table