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

    • getTable

      public @Nullable OrmTable getTable(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
    • getTableOrThrow

      public OrmTable getTableOrThrow(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
    • getTableInContext

      public Optional<@Nullable OrmTable> getTableInContext(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
    • getTableInContextOrThrow

      public OrmTable getTableInContextOrThrow(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
    • getTable

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

      public @Nullable OrmTable getTable(String schema, String table)
      Retrieves the OrmTable associated with the specified schema and table name.
      Parameters:
      schema - the schema name
      table - the table name
      Returns:
      the OrmTable associated with the specified schema and table name, or null if not found
    • getTable

      public @Nullable OrmTable getTable(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
    • containsTable

      public boolean containsTable(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 table)
      Adds a table to the registry.
      Parameters:
      dtoClass - the DTO class for which the table is to be associated
      table - the table to register
    • tableStream

      public Stream<OrmTable> tableStream()
      Returns a stream of all registered tables.
      Returns:
      a stream of all registered tables
    • getOrCreateSpiTable

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