Interface StateManager

All Known Implementing Classes:
HierarchicalStateManager, SimpleStateManager

public interface StateManager
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(@Nonnull 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(@Nonnull Class<ID> idType, @Nonnull Class<T> entityType, @Nonnull SimpleRepositoryEntityLoader<ID,T> loader, @Nonnull 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

      @Nonnull default <I, T> CompletableFuture<T> loadEntity(@Nonnull Class<T> type, @Nonnull I id, @Nonnull 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.
    • loadManagedEntity

      <ID, T> CompletableFuture<ManagedEntity<ID,T>> loadManagedEntity(@Nonnull Class<T> type, @Nonnull ID id, @Nonnull 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.
    • 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(@Nonnull 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> Repository<ID,T> repository(@Nonnull Class<T> entityType, @Nonnull Class<ID> idType)
      Returns the Repository for the given type. Returns null if no repository is registered for the given type and id.
      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.