Polishing.

Refine assertions of unwanted objects.

See #1499
This commit is contained in:
Mark Paluch
2024-07-24 14:10:17 +02:00
parent 1a72451c34
commit b02492f873
2 changed files with 33 additions and 28 deletions

View File

@@ -182,7 +182,6 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotStatement("insert", entities);
assertNotQueryOptions(entities);
CassandraMappingContext mappingContext = getMappingContext();
@@ -190,6 +189,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
Assert.notNull(entity, "Entity must not be null");
assertNotStatement("insert", entity);
assertNotQueryOptions(entity);
BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(entity.getClass());
@@ -222,12 +222,12 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotQueryOptions(entities);
for (Object entity : entities) {
Assert.notNull(entity, "Entity must not be null");
assertNotStatement("update", entity);
assertNotQueryOptions(entity);
CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
@@ -259,12 +259,12 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotQueryOptions(entities);
for (Object entity : entities) {
Assert.notNull(entity, "Entity must not be null");
assertNotStatement("delete", entity);
assertNotQueryOptions(entity);
CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
@@ -277,17 +277,6 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
return this;
}
private void assertNotQueryOptions(Iterable<?> entities) {
for (Object entity : entities) {
if (entity instanceof QueryOptions) {
throw new IllegalArgumentException(
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
ClassUtils.getDescriptiveType(entity), ClassUtils.getShortName(entity.getClass())));
}
}
}
private void assertNotExecuted() {
Assert.state(!this.executed.get(), "This Cassandra Batch was already executed");
}
@@ -296,7 +285,17 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
return getMappingContext().getRequiredPersistentEntity(ClassUtils.getUserClass(entityType));
}
private static void assertNotQueryOptions(Object o) {
if (o instanceof QueryOptions) {
throw new IllegalArgumentException(
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
ClassUtils.getDescriptiveType(o), ClassUtils.getShortName(o.getClass())));
}
}
private static void assertNotStatement(String operation, Object o) {
if (o instanceof Statement<?>) {
throw new IllegalArgumentException(String.format("%s cannot use a Statement: %s. Use only entities for %s",
StringUtils.capitalize(operation), ClassUtils.getDescriptiveType(o), operation));

View File

@@ -206,7 +206,6 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotQueryOptions(entities);
addStatements(doInsert(entities, options));
@@ -234,6 +233,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
Assert.notNull(entity, "Entity must not be null");
assertNotStatement("insert", entity);
assertNotQueryOptions(entity);
BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(entity.getClass());
@@ -271,7 +271,6 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotQueryOptions(entities);
addStatements(Mono.just(doUpdate(entities, options)));
@@ -298,6 +297,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
Assert.notNull(entity, "Entity must not be null");
assertNotStatement("update", entity);
assertNotQueryOptions(entity);
CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
@@ -334,7 +334,6 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotQueryOptions(entities);
addStatements(Mono.just(doDelete(entities, options)));
@@ -353,17 +352,6 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
return this;
}
private void assertNotQueryOptions(Iterable<?> entities) {
for (Object entity : entities) {
if (entity instanceof QueryOptions) {
throw new IllegalArgumentException(
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
ClassUtils.getDescriptiveType(entity), ClassUtils.getShortName(entity.getClass())));
}
}
}
private Collection<SimpleStatement> doDelete(Iterable<?> entities, WriteOptions options) {
List<SimpleStatement> deleteQueries = new ArrayList<>();
@@ -372,6 +360,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
Assert.notNull(entity, "Entity must not be null");
assertNotStatement("delete", entity);
assertNotQueryOptions(entity);
CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
@@ -383,4 +372,21 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
return deleteQueries;
}
private static void assertNotQueryOptions(Object o) {
if (o instanceof QueryOptions) {
throw new IllegalArgumentException(
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
ClassUtils.getDescriptiveType(o), ClassUtils.getShortName(o.getClass())));
}
}
private static void assertNotStatement(String operation, Object o) {
if (o instanceof Statement<?>) {
throw new IllegalArgumentException(String.format("%s cannot use a Statement: %s. Use only entities for %s",
StringUtils.capitalize(operation), ClassUtils.getDescriptiveType(o), operation));
}
}
}