Class InMemoryHexaRepo<T>

java.lang.Object
com.guinetik.hexafun.hexa.InMemoryHexaRepo<T>
Type Parameters:
T - The entity type this repository manages
All Implemented Interfaces:
HexaRepo<T>

public class InMemoryHexaRepo<T> extends Object implements HexaRepo<T>
Generic in-memory implementation of the HexaRepo interface. Uses a HashMap to store entities with string IDs.
  • Constructor Details

    • InMemoryHexaRepo

      public InMemoryHexaRepo(Function<T,String> idExtractor, Function<T,T> idGenerator)
      Creates a new InMemoryHexaRepo with custom ID extraction and generation.
      Parameters:
      idExtractor - Function to extract ID from an entity
      idGenerator - Function to generate a new ID for an entity that doesn't have one
    • InMemoryHexaRepo

      public InMemoryHexaRepo(Function<T,String> idExtractor)
      Creates a new InMemoryHexaRepo with default UUID generation. Note: This constructor assumes the entity has an ID field named "id" and is accessible via reflection or a setter.
      Parameters:
      idExtractor - Function to extract ID from an entity
    • InMemoryHexaRepo

      public InMemoryHexaRepo()
      Creates a new InMemoryHexaRepo with default behavior. The idExtractor returns the ID from storage based on the entity. The idGenerator simply returns the entity as is.
  • Method Details

    • save

      public Result<T> save(T entity)
      Description copied from interface: HexaRepo
      Saves a single entity to the repository.
      Specified by:
      save in interface HexaRepo<T>
      Parameters:
      entity - The entity to save
      Returns:
      A Result containing the saved entity with any generated values
    • saveAll

      public Result<List<T>> saveAll(List<T> entities)
      Description copied from interface: HexaRepo
      Saves multiple entities to the repository in a batch operation.
      Specified by:
      saveAll in interface HexaRepo<T>
      Parameters:
      entities - The list of entities to save
      Returns:
      A Result containing the list of saved entities
    • findById

      public Result<T> findById(String id)
      Description copied from interface: HexaRepo
      Finds an entity by its unique identifier.
      Specified by:
      findById in interface HexaRepo<T>
      Parameters:
      id - The unique identifier of the entity
      Returns:
      A Result containing the found entity or an error if not found
    • findAll

      public Result<List<T>> findAll()
      Description copied from interface: HexaRepo
      Retrieves all entities from the repository.
      Specified by:
      findAll in interface HexaRepo<T>
      Returns:
      A Result containing a list of all entities
    • findBy

      public Result<List<T>> findBy(DirectoryStream.Filter<T> filter)
      Description copied from interface: HexaRepo
      Retrieves entities that match the given filter criteria.
      Specified by:
      findBy in interface HexaRepo<T>
      Parameters:
      filter - The filter to apply
      Returns:
      A Result containing a list of matching entities
    • findAll

      public Result<List<T>> findAll(int offset, int limit)
      Description copied from interface: HexaRepo
      Retrieves a paginated list of entities.
      Specified by:
      findAll in interface HexaRepo<T>
      Parameters:
      offset - The number of entities to skip
      limit - The maximum number of entities to return
      Returns:
      A Result containing a list of entities within the specified range
    • count

      public Result<Long> count()
      Description copied from interface: HexaRepo
      Counts the total number of entities in the repository.
      Specified by:
      count in interface HexaRepo<T>
      Returns:
      A Result containing the count of entities
    • update

      public Result<T> update(String id, T entity)
      Description copied from interface: HexaRepo
      Updates an existing entity identified by its ID.
      Specified by:
      update in interface HexaRepo<T>
      Parameters:
      id - The ID of the entity to update
      entity - The updated entity data
      Returns:
      A Result containing the updated entity
    • deleteById

      public Result<Boolean> deleteById(String id)
      Description copied from interface: HexaRepo
      Deletes an entity by its unique identifier.
      Specified by:
      deleteById in interface HexaRepo<T>
      Parameters:
      id - The unique identifier of the entity to delete
      Returns:
      A Result containing a Boolean indicating success
    • deleteAllById

      public Result<Void> deleteAllById(List<String> ids)
      Description copied from interface: HexaRepo
      Deletes multiple entities by their IDs in a batch operation.
      Specified by:
      deleteAllById in interface HexaRepo<T>
      Parameters:
      ids - The list of entity IDs to delete
      Returns:
      A Result indicating the operation outcome
    • clear

      public Result<Void> clear()
      Description copied from interface: HexaRepo
      Removes all entities from the repository.
      Specified by:
      clear in interface HexaRepo<T>
      Returns:
      A Result indicating the operation outcome