DATAKV-310 - Guard repository implementations against null values.
SimpleKeyValueRepository and QuerydslKeyValueRepository now reject null arguments as per their interface contract. Original Pull Request: #50
This commit is contained in:
committed by
Christoph Strobl
parent
8bf02c7a4c
commit
1a5a74d2e9
@@ -96,6 +96,8 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
@Override
|
||||
public Optional<T> findOne(Predicate predicate) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
|
||||
try {
|
||||
return Optional.ofNullable(prepareQuery(predicate).fetchOne());
|
||||
} catch (NonUniqueResultException o_O) {
|
||||
@@ -109,6 +111,9 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
*/
|
||||
@Override
|
||||
public Iterable<T> findAll(Predicate predicate) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
|
||||
return prepareQuery(predicate).fetchResults().getResults();
|
||||
}
|
||||
|
||||
@@ -119,6 +124,9 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
@Override
|
||||
public Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
Assert.notNull(orders, "OrderSpecifiers must not be null");
|
||||
|
||||
AbstractCollQuery<T, ?> query = prepareQuery(predicate);
|
||||
query.orderBy(orders);
|
||||
|
||||
@@ -131,6 +139,10 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
*/
|
||||
@Override
|
||||
public Iterable<T> findAll(Predicate predicate, Sort sort) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
Assert.notNull(sort, "Sort must not be null");
|
||||
|
||||
return findAll(predicate, toOrderSpecifier(sort, builder));
|
||||
}
|
||||
|
||||
@@ -141,6 +153,9 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
@Override
|
||||
public Page<T> findAll(Predicate predicate, Pageable pageable) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
Assert.notNull(pageable, "Pageable must not be null");
|
||||
|
||||
AbstractCollQuery<T, ?> query = prepareQuery(predicate);
|
||||
|
||||
if (pageable.isPaged() || pageable.getSort().isSorted()) {
|
||||
@@ -163,6 +178,8 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
@Override
|
||||
public Iterable<T> findAll(OrderSpecifier<?>... orders) {
|
||||
|
||||
Assert.notNull(orders, "OrderSpecifiers must not be null");
|
||||
|
||||
if (ObjectUtils.isEmpty(orders)) {
|
||||
return findAll();
|
||||
}
|
||||
@@ -179,6 +196,9 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
*/
|
||||
@Override
|
||||
public long count(Predicate predicate) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
|
||||
return prepareQuery(predicate).fetchCount();
|
||||
}
|
||||
|
||||
@@ -188,6 +208,9 @@ public class QuerydslKeyValueRepository<T, ID> extends SimpleKeyValueRepository<
|
||||
*/
|
||||
@Override
|
||||
public boolean exists(Predicate predicate) {
|
||||
|
||||
Assert.notNull(predicate, "Predicate must not be null");
|
||||
|
||||
return count(predicate) > 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
*/
|
||||
@Override
|
||||
public Iterable<T> findAll(Sort sort) {
|
||||
|
||||
Assert.notNull(sort, "Sort must not be null!");
|
||||
|
||||
return operations.findAll(sort, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
@@ -111,6 +114,8 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
@Override
|
||||
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
List<S> saved = new ArrayList<>();
|
||||
|
||||
for (S entity : entities) {
|
||||
@@ -126,6 +131,9 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
*/
|
||||
@Override
|
||||
public Optional<T> findById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return operations.findById(id, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
@@ -154,6 +162,8 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
@Override
|
||||
public Iterable<T> findAllById(Iterable<ID> ids) {
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of id's must not be null");
|
||||
|
||||
List<T> result = new ArrayList<>();
|
||||
|
||||
ids.forEach(id -> findById(id).ifPresent(result::add));
|
||||
@@ -176,6 +186,9 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
operations.delete(id, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
@@ -185,6 +198,9 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
*/
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
|
||||
Assert.notNull(entity, "The given entity must not be null");
|
||||
|
||||
deleteById(entityInformation.getRequiredId(entity));
|
||||
}
|
||||
|
||||
@@ -194,6 +210,9 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
entities.forEach(this::delete);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user