Interface StateManager

All Superinterfaces:
DescribableComponent
All Known Implementing Classes:
HierarchicalStateManager, SimpleStateManager, TracingStateManager

public interface StateManager extends DescribableComponent
The StateManager enables applications to load entities based on the type of the entity and an id, and to persist them. Implementations may specify whether they load entities through Repository.load(Object, ProcessingContext) or Repository.loadOrCreate(Object, ProcessingContext).

Entities are registered by their type in combination with their id. The combination of entity type and id type of all repositories must be unique and unambiguous. This means you cannot register a repository if another conflicting repository already exists. If you do, a RepositoryAlreadyRegisteredException will be thrown. Note that superclasses and subclasses of each other are considered conflicting.

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Method Details

    • register

      <ID, T> StateManager register(Repository<ID,T> repository)
      Registers an Repository for use with this StateManager. The combination of Repository.entityType() and Repository.idType() must be unique for all registered repositories.
      Type Parameters:
      ID - the type of id
      T - the type of the entity
      Parameters:
      repository - the Repository to use for loading state
      Returns:
      this StateManager for fluent interfacing
      Throws:
      RepositoryAlreadyRegisteredException - if a repository with the same entity type and id type is already registered
    • register

      default <ID, T> StateManager register(Class<ID> idType, Class<T> entityType, SimpleRepositoryEntityLoader<ID,T> loader, SimpleRepositoryEntityPersister<ID,T> persister)
      Registers a load and save function for state type T with id of type ID. Creates a SimpleRepository for the given type with the given load and save functions.
      Type Parameters:
      ID - the type of id
      T - the type of state
      Parameters:
      idType - the type of the identifier
      entityType - the type of the state
      loader - the function to load state
      persister - the function to persist state
      Returns:
      this StateManager for fluent interfacing
      Throws:
      RepositoryAlreadyRegisteredException - if a repository with the same entity type and id type is already registered
    • loadEntity

      default <I, T> CompletableFuture<@Nullable T> loadEntity(Class<T> type, I id, ProcessingContext context)
      Retrieves an entity of the given type and id. The CompletableFuture will resolve to the entity, or complete exceptionally if it could not be resolved.

      If multiple repositories are registered for the given entityType that can handle the given id (through superclass registration), the most specific repository is used.

      Type Parameters:
      I - the type of the identifier of the entity
      T - the type of state to retrieve
      Parameters:
      type - the type of state to retrieve
      id - the id of the state to retrieve
      context - the context to load the entity in
      Returns:
      a CompletableFuture which resolves to the entity instance, or completes exceptionally with a MissingRepositoryException when this StateManager does not control the Repository to load the entity from
    • loadManagedEntity

      <ID, T> CompletableFuture<ManagedEntity<ID,T>> loadManagedEntity(Class<T> type, ID id, ProcessingContext context)
      Retrieves a ManagedEntity of the given type and id. The CompletableFuture will resolve to a ManagedEntity, or complete exceptionally if it could not be resolved.
      Type Parameters:
      ID - the type of the identifier of the entity
      T - the type of the entity
      Parameters:
      type - the type of state to retrieve
      id - the id of the state to retrieve
      context - the context to load the entity in
      Returns:
      a CompletableFuture which resolves to the entity instance, or completes exceptionally with a MissingRepositoryException when this StateManager does not control the Repository to load the ManagedEntity from
    • registeredEntities

      Set<Class<?>> registeredEntities()
      The types of entities that are registered with this StateManager.
      Returns:
      the types of entities that are registered with this StateManager
    • registeredIdsFor

      Set<Class<?>> registeredIdsFor(Class<?> entityType)
      The types of identifiers that are registered with this StateManager for the given entityType.
      Parameters:
      entityType - the type of the entity
      Returns:
      the types of identifiers that are registered with this StateManager for the given entityType
    • repository

      <ID, T> @Nullable Repository<ID,T> repository(Class<T> entityType, Class<ID> idType)
      Returns the Repository for the given type.

      Returns null if no repository is registered for the given type and id.

      Unlike loadManagedEntity(Class, Object, ProcessingContext), which resolves a Repository registered for a supertype when asked for a subtype, this method only matches an exactly registered (entityType, idType) pair. Hence, there is no supertype or subtype resolution.

      Type Parameters:
      ID - the type of the identifier of the entity
      T - the type of the entity
      Parameters:
      entityType - the type of the entity
      idType - the type of the identifier of the entity
      Returns:
      the Repository for the given idType and entityType