Skip to content

Commit b02492f

Browse files
committed
Polishing.
Refine assertions of unwanted objects. See #1499
1 parent 1a72451 commit b02492f

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

Diff for: spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ public CassandraBatchOperations insert(Iterable<?> entities, WriteOptions option
182182
Assert.notNull(entities, "Entities must not be null");
183183
Assert.notNull(options, "WriteOptions must not be null");
184184
assertNotStatement("insert", entities);
185-
assertNotQueryOptions(entities);
186185

187186
CassandraMappingContext mappingContext = getMappingContext();
188187

189188
for (Object entity : entities) {
190189

191190
Assert.notNull(entity, "Entity must not be null");
192191
assertNotStatement("insert", entity);
192+
assertNotQueryOptions(entity);
193193

194194
BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
195195
.getRequiredPersistentEntity(entity.getClass());
@@ -222,12 +222,12 @@ public CassandraBatchOperations update(Iterable<?> entities, WriteOptions option
222222
assertNotExecuted();
223223
Assert.notNull(entities, "Entities must not be null");
224224
Assert.notNull(options, "WriteOptions must not be null");
225-
assertNotQueryOptions(entities);
226225

227226
for (Object entity : entities) {
228227

229228
Assert.notNull(entity, "Entity must not be null");
230229
assertNotStatement("update", entity);
230+
assertNotQueryOptions(entity);
231231

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

@@ -259,12 +259,12 @@ public CassandraBatchOperations delete(Iterable<?> entities, WriteOptions option
259259
assertNotExecuted();
260260
Assert.notNull(entities, "Entities must not be null");
261261
Assert.notNull(options, "WriteOptions must not be null");
262-
assertNotQueryOptions(entities);
263262

264263
for (Object entity : entities) {
265264

266265
Assert.notNull(entity, "Entity must not be null");
267266
assertNotStatement("delete", entity);
267+
assertNotQueryOptions(entity);
268268

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

@@ -277,17 +277,6 @@ public CassandraBatchOperations delete(Iterable<?> entities, WriteOptions option
277277
return this;
278278
}
279279

280-
private void assertNotQueryOptions(Iterable<?> entities) {
281-
282-
for (Object entity : entities) {
283-
if (entity instanceof QueryOptions) {
284-
throw new IllegalArgumentException(
285-
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
286-
ClassUtils.getDescriptiveType(entity), ClassUtils.getShortName(entity.getClass())));
287-
}
288-
}
289-
}
290-
291280
private void assertNotExecuted() {
292281
Assert.state(!this.executed.get(), "This Cassandra Batch was already executed");
293282
}
@@ -296,7 +285,17 @@ private CassandraPersistentEntity<?> getRequiredPersistentEntity(Class<?> entity
296285
return getMappingContext().getRequiredPersistentEntity(ClassUtils.getUserClass(entityType));
297286
}
298287

288+
private static void assertNotQueryOptions(Object o) {
289+
290+
if (o instanceof QueryOptions) {
291+
throw new IllegalArgumentException(
292+
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
293+
ClassUtils.getDescriptiveType(o), ClassUtils.getShortName(o.getClass())));
294+
}
295+
}
296+
299297
private static void assertNotStatement(String operation, Object o) {
298+
300299
if (o instanceof Statement<?>) {
301300
throw new IllegalArgumentException(String.format("%s cannot use a Statement: %s. Use only entities for %s",
302301
StringUtils.capitalize(operation), ClassUtils.getDescriptiveType(o), operation));

Diff for: spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraBatchTemplate.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public ReactiveCassandraBatchOperations insert(Iterable<?> entities, WriteOption
206206
assertNotExecuted();
207207
Assert.notNull(entities, "Entities must not be null");
208208
Assert.notNull(options, "WriteOptions must not be null");
209-
assertNotQueryOptions(entities);
210209

211210
addStatements(doInsert(entities, options));
212211

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

235234
Assert.notNull(entity, "Entity must not be null");
236235
assertNotStatement("insert", entity);
236+
assertNotQueryOptions(entity);
237237

238238
BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
239239
.getRequiredPersistentEntity(entity.getClass());
@@ -271,7 +271,6 @@ public ReactiveCassandraBatchOperations update(Iterable<?> entities, WriteOption
271271
assertNotExecuted();
272272
Assert.notNull(entities, "Entities must not be null");
273273
Assert.notNull(options, "WriteOptions must not be null");
274-
assertNotQueryOptions(entities);
275274

276275
addStatements(Mono.just(doUpdate(entities, options)));
277276

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

299298
Assert.notNull(entity, "Entity must not be null");
300299
assertNotStatement("update", entity);
300+
assertNotQueryOptions(entity);
301301

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

@@ -334,7 +334,6 @@ public ReactiveCassandraBatchOperations delete(Iterable<?> entities, WriteOption
334334
assertNotExecuted();
335335
Assert.notNull(entities, "Entities must not be null");
336336
Assert.notNull(options, "WriteOptions must not be null");
337-
assertNotQueryOptions(entities);
338337

339338
addStatements(Mono.just(doDelete(entities, options)));
340339

@@ -353,17 +352,6 @@ public ReactiveCassandraBatchOperations delete(Mono<? extends Iterable<?>> entit
353352
return this;
354353
}
355354

356-
private void assertNotQueryOptions(Iterable<?> entities) {
357-
358-
for (Object entity : entities) {
359-
if (entity instanceof QueryOptions) {
360-
throw new IllegalArgumentException(
361-
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
362-
ClassUtils.getDescriptiveType(entity), ClassUtils.getShortName(entity.getClass())));
363-
}
364-
}
365-
}
366-
367355
private Collection<SimpleStatement> doDelete(Iterable<?> entities, WriteOptions options) {
368356

369357
List<SimpleStatement> deleteQueries = new ArrayList<>();
@@ -372,6 +360,7 @@ private Collection<SimpleStatement> doDelete(Iterable<?> entities, WriteOptions
372360

373361
Assert.notNull(entity, "Entity must not be null");
374362
assertNotStatement("delete", entity);
363+
assertNotQueryOptions(entity);
375364

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

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

384373
return deleteQueries;
385374
}
375+
376+
private static void assertNotQueryOptions(Object o) {
377+
378+
if (o instanceof QueryOptions) {
379+
throw new IllegalArgumentException(
380+
String.format("%s must not be used as entity; Please make sure to call the appropriate method accepting %s",
381+
ClassUtils.getDescriptiveType(o), ClassUtils.getShortName(o.getClass())));
382+
}
383+
}
384+
385+
private static void assertNotStatement(String operation, Object o) {
386+
387+
if (o instanceof Statement<?>) {
388+
throw new IllegalArgumentException(String.format("%s cannot use a Statement: %s. Use only entities for %s",
389+
StringUtils.capitalize(operation), ClassUtils.getDescriptiveType(o), operation));
390+
}
391+
}
386392
}

0 commit comments

Comments
 (0)