Class AbstractInMemoryCrudRepository<T,ID extends Comparable<ID>>

java.lang.Object
com.reallifedeveloper.tools.test.database.inmemory.AbstractInMemoryCrudRepository<T,ID>
Type Parameters:
T - the type of the entities handled by this repository
ID - the type of the entities' primary keys
All Implemented Interfaces:
org.springframework.data.repository.CrudRepository<T,ID>, org.springframework.data.repository.PagingAndSortingRepository<T,ID>, org.springframework.data.repository.query.QueryByExampleExecutor<T>, org.springframework.data.repository.Repository<T,ID>
Direct Known Subclasses:
InMemoryJpaRepository

public abstract class AbstractInMemoryCrudRepository<T,ID extends Comparable<ID>> extends Object implements org.springframework.data.repository.CrudRepository<T,ID>, org.springframework.data.repository.PagingAndSortingRepository<T,ID>, org.springframework.data.repository.query.QueryByExampleExecutor<T>
An abstract helper class that implements the CrudRepository interface using an in-memory map instead of a database.

Contains useful methods for sub-classes implementing in-memory versions of repositories, such as findByField(String, Object) and findByUniqueField(String, Object).

Author:
RealLifeDeveloper
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new InMemoryCrudRepository with no primary key generator.
    Creates a new InMemoryCrudRepository with the provided primary key generator.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    <S extends T>
    long
    count(org.springframework.data.domain.Example<S> example)
    void
    delete(T entity)
    void
    void
    deleteAll(Iterable<? extends T> entitiesToDelete)
    void
    deleteAllById(Iterable<? extends ID> ids)
    void
    <S extends T>
    boolean
    exists(org.springframework.data.domain.Example<S> example)
    boolean
    protected final void
    Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).
    <S extends T>
    List<S>
    findAll(org.springframework.data.domain.Example<S> example)
    <S extends T>
    org.springframework.data.domain.Page<S>
    findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Pageable pageable)
    <S extends T>
    List<S>
    findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Sort sort)
    org.springframework.data.domain.Page<T>
    findAll(org.springframework.data.domain.Pageable pageable)
    findAll(org.springframework.data.domain.Sort sort)
    protected <F> List<@NonNull T>
    findByField(String fieldName, F value)
    Finds entities with a field matching a value.
    protected <F> Optional<T>
    findByUniqueField(String fieldName, F value)
    Finds a unique entity with a field matching a value.
    <S extends T>
    Optional<S>
    findOne(org.springframework.data.domain.Example<S> example)
    protected @Nullable ID
    getId(T entity)
    Gives the value of the ID field or method of the given entity.
    protected abstract Optional<Class<ID>>
    Override this in concrete subclass to give the ID class representing a composite primary key for an entity, if any.
    protected abstract boolean
    Override this in a concrete subclass to decide if a given field is an ID field of an entity.
    protected abstract boolean
    Override this in a concrete subclass to decide if a given method is a method giving the ID of an entity.
    <S extends T>
    S
    save(S entity)
    <S extends T>
    List<S>
    saveAll(Iterable<S> entitiesToSave)
    protected void
    setId(T entity, ID id)
    Sets the value of the ID field, or calls the ID setter method, for the given entity.
     

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor

    findBy
  • Constructor Details

    • AbstractInMemoryCrudRepository

      public AbstractInMemoryCrudRepository()
      Creates a new InMemoryCrudRepository with no primary key generator. If an entity with a null primary key is saved, an exception is thrown.
    • AbstractInMemoryCrudRepository

      public AbstractInMemoryCrudRepository(PrimaryKeyGenerator<ID> primaryKeyGenerator)
      Creates a new InMemoryCrudRepository with the provided primary key generator. If an entity with a null primary key is saved, the generator is used to create a new primary key that is stored in the entity before saving.
      Parameters:
      primaryKeyGenerator - the primary key generator to use, must not be null
  • Method Details

    • findByField

      protected <F> List<@NonNull T> findByField(String fieldName, F value)
      Finds entities with a field matching a value.
      Type Parameters:
      F - the type of value
      Parameters:
      fieldName - the name of the field to use when searching
      value - the value to search for
      Returns:
      a list of entities e such that value.equals(e.fieldName)
      Throws:
      IllegalArgumentException - if fieldName is null
    • findByUniqueField

      protected <F> Optional<T> findByUniqueField(String fieldName, F value)
      Finds a unique entity with a field matching a value.
      Type Parameters:
      F - the type of value
      Parameters:
      fieldName - the name of the field to use when searching
      value - the value to search for
      Returns:
      the unique entity e such that value.equals(e.fieldName), or null if no such entity is found
      Throws:
      IllegalArgumentException - if either argument is null, or if more than one entity with the given value is found
    • count

      public long count()
      Specified by:
      count in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • deleteById

      public void deleteById(ID id)
      Specified by:
      deleteById in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • deleteAll

      public void deleteAll(Iterable<? extends T> entitiesToDelete)
      Specified by:
      deleteAll in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • deleteAllById

      public void deleteAllById(Iterable<? extends ID> ids)
      Specified by:
      deleteAllById in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • delete

      public void delete(T entity)
      Specified by:
      delete in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • deleteAll

      public void deleteAll()
      Specified by:
      deleteAll in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • existsById

      public boolean existsById(ID id)
      Specified by:
      existsById in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • findAll

      public List<T> findAll()
      Specified by:
      findAll in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • findAllById

      public List<T> findAllById(Iterable<ID> ids)
      Specified by:
      findAllById in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • findById

      public Optional<T> findById(ID id)
      Specified by:
      findById in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • save

      public <S extends T> S save(S entity)
      Specified by:
      save in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • saveAll

      public <S extends T> List<S> saveAll(Iterable<S> entitiesToSave)
      Specified by:
      saveAll in interface org.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
    • findAll

      public List<T> findAll(org.springframework.data.domain.Sort sort)
      Specified by:
      findAll in interface org.springframework.data.repository.PagingAndSortingRepository<T,ID extends Comparable<ID>>
    • findAll

      public org.springframework.data.domain.Page<T> findAll(org.springframework.data.domain.Pageable pageable)
      Specified by:
      findAll in interface org.springframework.data.repository.PagingAndSortingRepository<T,ID extends Comparable<ID>>
    • getId

      protected @Nullable ID getId(T entity)
      Gives the value of the ID field or method of the given entity.
      Parameters:
      entity - the entity to examine, should not be null
      Returns:
      the value of the ID field or method of entity, may be null
    • setId

      protected void setId(T entity, ID id)
      Sets the value of the ID field, or calls the ID setter method, for the given entity.
      Parameters:
      entity - the entity for which to set the ID
      id - the new ID value
    • isIdField

      protected abstract boolean isIdField(Field field)
      Override this in a concrete subclass to decide if a given field is an ID field of an entity.
      Parameters:
      field - the field to examine
      Returns:
      true if field is an ID field, false otherwise
    • isIdMethod

      protected abstract boolean isIdMethod(Method method)
      Override this in a concrete subclass to decide if a given method is a method giving the ID of an entity.
      Parameters:
      method - the method to examine
      Returns:
      true if method is an ID method, false otherwise
    • getIdClass

      protected abstract Optional<Class<ID>> getIdClass(Object entity)
      Override this in concrete subclass to give the ID class representing a composite primary key for an entity, if any.
      Parameters:
      entity - the entity to examine
      Returns:
      the ID class representing the composite primary key of entity, or an empty optional if there is no such class
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • findOne

      public <S extends T> Optional<S> findOne(org.springframework.data.domain.Example<S> example)

      This method is not yet implemented, so it always throws an exception.

      Specified by:
      findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
      Throws:
      UnsupportedOperationException - always
    • findAll

      public <S extends T> List<S> findAll(org.springframework.data.domain.Example<S> example)

      This method is not yet implemented, so it always throws an exception.

      Specified by:
      findAll in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
      Throws:
      UnsupportedOperationException - always
    • findAll

      public <S extends T> org.springframework.data.domain.Page<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Pageable pageable)

      This method is not yet implemented, so it always throws an exception.

      Specified by:
      findAll in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
      Throws:
      UnsupportedOperationException - always
    • findAll

      public <S extends T> List<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Sort sort)

      This method is not yet implemented, so it always throws an exception.

      Specified by:
      findAll in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
      Throws:
      UnsupportedOperationException - always
    • count

      public <S extends T> long count(org.springframework.data.domain.Example<S> example)

      This method is not yet implemented, so it always throws an exception.

      Specified by:
      count in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
      Throws:
      UnsupportedOperationException - always
    • exists

      public <S extends T> boolean exists(org.springframework.data.domain.Example<S> example)

      This method is not yet implemented, so it always throws an exception.

      Specified by:
      exists in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
      Throws:
      UnsupportedOperationException - always
    • finalize

      protected final void finalize() throws Throwable
      Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).
      Overrides:
      finalize in class Object
      Throws:
      Throwable
      See Also: