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 repositoryID- 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
ConstructorsConstructorDescriptionCreates a newInMemoryCrudRepositorywith no primary key generator.AbstractInMemoryCrudRepository(PrimaryKeyGenerator<ID> primaryKeyGenerator) Creates a newInMemoryCrudRepositorywith the provided primary key generator. -
Method Summary
Modifier and TypeMethodDescriptionlongcount()<S extends T>
longcount(org.springframework.data.domain.Example<S> example) voidvoidvoidvoiddeleteAllById(Iterable<? extends ID> ids) voiddeleteById(ID id) <S extends T>
booleanexists(org.springframework.data.domain.Example<S> example) booleanexistsById(ID id) protected final voidfinalize()Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).findAll()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) 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) findAllById(Iterable<ID> ids) findByField(String fieldName, F value) Finds entities with a field matching a value.findByUniqueField(String fieldName, F value) Finds a unique entity with a field matching a value.findOne(org.springframework.data.domain.Example<S> example) getCompositeIdClass(Object entity) Override this in concrete subclass to give the ID class representing a composite primary key for an entity, if any.protected @Nullable IDGives the value of the ID field or method of the given entity.protected abstract booleanOverride this in a concrete subclass to decide if a given field is an ID field of an entity.protected abstract booleanisIdMethod(Method method) Override this in a concrete subclass to decide if a given method is a method giving the ID of an entity.<S extends T>
Ssave(S entity) protected voidSets the value of the ID field, or calls the ID setter method, for the given entity.toString()Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
findBy
-
Constructor Details
-
AbstractInMemoryCrudRepository
public AbstractInMemoryCrudRepository()Creates a newInMemoryCrudRepositorywith no primary key generator. If an entity with anullprimary key is saved, an exception is thrown. -
AbstractInMemoryCrudRepository
Creates a newInMemoryCrudRepositorywith the provided primary key generator. If an entity with anullprimary 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 benull
-
-
Method Details
-
findByField
Finds entities with a field matching a value.- Type Parameters:
F- the type ofvalue- Parameters:
fieldName- the name of the field to use when searchingvalue- the value to search for- Returns:
- a list of entities
esuch thatvalue.equals(e.fieldName) - Throws:
IllegalArgumentException- iffieldNameisnull
-
findByUniqueField
Finds a unique entity with a field matching a value.- Type Parameters:
F- the type ofvalue- Parameters:
fieldName- the name of the field to use when searchingvalue- the value to search for- Returns:
- the unique entity
esuch thatvalue.equals(e.fieldName), ornullif no such entity is found - Throws:
IllegalArgumentException- if either argument isnull, or if more than one entity with the given value is found
-
count
public long count()- Specified by:
countin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
deleteById
- Specified by:
deleteByIdin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
deleteAll
- Specified by:
deleteAllin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
deleteAllById
- Specified by:
deleteAllByIdin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
delete
- Specified by:
deletein interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
deleteAll
public void deleteAll()- Specified by:
deleteAllin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
existsById
- Specified by:
existsByIdin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
findAll
- Specified by:
findAllin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
findAllById
- Specified by:
findAllByIdin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
findById
- Specified by:
findByIdin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
save
- Specified by:
savein interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
saveAll
- Specified by:
saveAllin interfaceorg.springframework.data.repository.CrudRepository<T,ID extends Comparable<ID>>
-
findAll
- Specified by:
findAllin interfaceorg.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:
findAllin interfaceorg.springframework.data.repository.PagingAndSortingRepository<T,ID extends Comparable<ID>>
-
getId
Gives the value of the ID field or method of the given entity.- Parameters:
entity- the entity to examine, should not benull- Returns:
- the value of the ID field or method of
entity, may benull
-
setId
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 IDid- the new ID value
-
isIdField
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:
trueiffieldis an ID field,falseotherwise
-
isIdMethod
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:
trueifmethodis an ID method,falseotherwise
-
getCompositeIdClass
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
-
findOne
This method is not yet implemented, so it always throws an exception.
- Specified by:
findOnein interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>- Throws:
UnsupportedOperationException- always
-
findAll
This method is not yet implemented, so it always throws an exception.
- Specified by:
findAllin interfaceorg.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:
findAllin interfaceorg.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:
findAllin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>- Throws:
UnsupportedOperationException- always
-
count
This method is not yet implemented, so it always throws an exception.
- Specified by:
countin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>- Throws:
UnsupportedOperationException- always
-
exists
This method is not yet implemented, so it always throws an exception.
- Specified by:
existsin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>- Throws:
UnsupportedOperationException- always
-
finalize
Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).
-