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 newInMemoryCrudRepository
with no primary key generator.AbstractInMemoryCrudRepository
(PrimaryKeyGenerator<ID> primaryKeyGenerator) Creates a newInMemoryCrudRepository
with the provided primary key generator. -
Method Summary
Modifier and TypeMethodDescriptionlong
count()
<S extends T>
longcount
(org.springframework.data.domain.Example<S> example) void
void
void
void
deleteAllById
(Iterable<? extends ID> ids) void
deleteById
(ID id) <S extends T>
booleanexists
(org.springframework.data.domain.Example<S> example) boolean
existsById
(ID id) protected final void
finalize()
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) protected @Nullable ID
Gives the value of the ID field or method of the given entity.getIdClass
(Object entity) 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
isIdMethod
(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 void
Sets 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, wait
Methods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
findBy
-
Constructor Details
-
AbstractInMemoryCrudRepository
public AbstractInMemoryCrudRepository()Creates a newInMemoryCrudRepository
with no primary key generator. If an entity with anull
primary key is saved, an exception is thrown. -
AbstractInMemoryCrudRepository
Creates a newInMemoryCrudRepository
with the provided primary key generator. If an entity with anull
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 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
e
such thatvalue.equals(e.fieldName)
- Throws:
IllegalArgumentException
- iffieldName
isnull
-
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
e
such thatvalue.equals(e.fieldName)
, ornull
if 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:
count
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
deleteById
- Specified by:
deleteById
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
deleteAll
- Specified by:
deleteAll
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
deleteAllById
- Specified by:
deleteAllById
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
delete
- Specified by:
delete
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
deleteAll
public void deleteAll()- Specified by:
deleteAll
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
existsById
- Specified by:
existsById
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
findAll
- Specified by:
findAll
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
findAllById
- Specified by:
findAllById
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
findById
- Specified by:
findById
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
save
- Specified by:
save
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
saveAll
- Specified by:
saveAll
in interfaceorg.springframework.data.repository.CrudRepository<T,
ID extends Comparable<ID>>
-
findAll
- Specified by:
findAll
in 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:
findAll
in 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:
true
iffield
is an ID field,false
otherwise
-
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:
true
ifmethod
is an ID method,false
otherwise
-
getIdClass
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:
findOne
in 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:
findAll
in 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:
findAll
in 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:
findAll
in 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:
count
in 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:
exists
in 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).
-