From b02492f873c96ca7a79b39eea180ff93620dfeee Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 24 Jul 2024 14:10:17 +0200 Subject: [PATCH] Polishing. Refine assertions of unwanted objects. See #1499 --- .../core/CassandraBatchTemplate.java | 27 +++++++-------- .../core/ReactiveCassandraBatchTemplate.java | 34 +++++++++++-------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java index 3122181dc..9edec3837 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java @@ -182,7 +182,6 @@ 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(); @@ -190,6 +189,7 @@ public CassandraBatchOperations insert(Iterable entities, WriteOptions option Assert.notNull(entity, "Entity must not be null"); assertNotStatement("insert", entity); + assertNotQueryOptions(entity); BasicCassandraPersistentEntity persistentEntity = mappingContext .getRequiredPersistentEntity(entity.getClass()); @@ -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()); @@ -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()); @@ -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"); } @@ -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)); diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraBatchTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraBatchTemplate.java index c0ca8d86c..a48715aa4 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraBatchTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraBatchTemplate.java @@ -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)); @@ -234,6 +233,7 @@ private Collection doInsert(Iterable entities, WriteOptions Assert.notNull(entity, "Entity must not be null"); assertNotStatement("insert", entity); + assertNotQueryOptions(entity); BasicCassandraPersistentEntity persistentEntity = mappingContext .getRequiredPersistentEntity(entity.getClass()); @@ -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))); @@ -298,6 +297,7 @@ private Collection doUpdate(Iterable entities, WriteOptions Assert.notNull(entity, "Entity must not be null"); assertNotStatement("update", entity); + assertNotQueryOptions(entity); CassandraPersistentEntity persistentEntity = getRequiredPersistentEntity(entity.getClass()); @@ -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))); @@ -353,17 +352,6 @@ public ReactiveCassandraBatchOperations delete(Mono> 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 doDelete(Iterable entities, WriteOptions options) { List deleteQueries = new ArrayList<>(); @@ -372,6 +360,7 @@ private Collection doDelete(Iterable entities, WriteOptions Assert.notNull(entity, "Entity must not be null"); assertNotStatement("delete", entity); + assertNotQueryOptions(entity); CassandraPersistentEntity persistentEntity = getRequiredPersistentEntity(entity.getClass()); @@ -383,4 +372,21 @@ private Collection 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)); + } + } }