Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Refine assertions of unwanted objects.

See #1499
  • Loading branch information
mp911de committed Jul 24, 2024
1 parent 1a72451 commit b02492f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ public CassandraBatchOperations insert(Iterable<?> entities, WriteOptions option
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotStatement("insert", entities);
assertNotQueryOptions(entities);

CassandraMappingContext mappingContext = getMappingContext();

for (Object entity : entities) {

Assert.notNull(entity, "Entity must not be null");
assertNotStatement("insert", entity);
assertNotQueryOptions(entity);

BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(entity.getClass());
Expand Down Expand Up @@ -222,12 +222,12 @@ public CassandraBatchOperations update(Iterable<?> entities, WriteOptions option
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());

Expand Down Expand Up @@ -259,12 +259,12 @@ public CassandraBatchOperations delete(Iterable<?> entities, WriteOptions option
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());

Expand All @@ -277,17 +277,6 @@ public CassandraBatchOperations delete(Iterable<?> entities, WriteOptions option
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");
}
Expand All @@ -296,7 +285,17 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ public ReactiveCassandraBatchOperations insert(Iterable<?> entities, WriteOption
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
assertNotQueryOptions(entities);

addStatements(doInsert(entities, options));

Expand Down Expand Up @@ -234,6 +233,7 @@ private Collection<SimpleStatement> doInsert(Iterable<?> entities, WriteOptions

Assert.notNull(entity, "Entity must not be null");
assertNotStatement("insert", entity);
assertNotQueryOptions(entity);

BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(entity.getClass());
Expand Down Expand Up @@ -271,7 +271,6 @@ public ReactiveCassandraBatchOperations update(Iterable<?> entities, WriteOption
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)));

Expand All @@ -298,6 +297,7 @@ private Collection<SimpleStatement> doUpdate(Iterable<?> entities, WriteOptions

Assert.notNull(entity, "Entity must not be null");
assertNotStatement("update", entity);
assertNotQueryOptions(entity);

CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());

Expand Down Expand Up @@ -334,7 +334,6 @@ public ReactiveCassandraBatchOperations delete(Iterable<?> entities, WriteOption
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)));

Expand All @@ -353,17 +352,6 @@ public ReactiveCassandraBatchOperations delete(Mono<? extends Iterable<?>> entit
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<>();
Expand All @@ -372,6 +360,7 @@ private Collection<SimpleStatement> doDelete(Iterable<?> entities, WriteOptions

Assert.notNull(entity, "Entity must not be null");
assertNotStatement("delete", entity);
assertNotQueryOptions(entity);

CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());

Expand All @@ -383,4 +372,21 @@ private Collection<SimpleStatement> doDelete(Iterable<?> entities, WriteOptions

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));
}
}
}

0 comments on commit b02492f

Please sign in to comment.