Skip to content

Commit 561c3d4

Browse files
Tomasz Foryschristophstrobl
Tomasz Forys
authored andcommitted
Instanceof casting simplification.
Closes: #4265
1 parent 7f74794 commit 561c3d4

File tree

114 files changed

+728
-804
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+728
-804
lines changed

spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/MongoResultsWriter.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,14 @@ private Document fixDocumentKeys(Document doc) {
9696
for (Object key : doc.keySet()) {
9797

9898
Object value = doc.get(key);
99-
if (value instanceof Document) {
100-
value = fixDocumentKeys((Document) value);
101-
} else if (value instanceof BasicDBObject) {
102-
value = fixDocumentKeys(new Document((BasicDBObject) value));
99+
if (value instanceof Document document) {
100+
value = fixDocumentKeys(document);
101+
} else if (value instanceof BasicDBObject basicDBObject) {
102+
value = fixDocumentKeys(new Document(basicDBObject));
103103
}
104104

105-
if (key instanceof String) {
105+
if (key instanceof String newKey) {
106106

107-
String newKey = (String) key;
108107
if (newKey.contains(".")) {
109108
newKey = newKey.replace('.', ',');
110109
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ChangeStreamOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ private static <T> Object doGetTimestamp(Object timestamp, Class<T> targetType)
150150
return timestamp;
151151
}
152152

153-
if (timestamp instanceof Instant) {
154-
return new BsonTimestamp((int) ((Instant) timestamp).getEpochSecond(), 0);
153+
if (timestamp instanceof Instant instant) {
154+
return new BsonTimestamp((int) instant.getEpochSecond(), 0);
155155
}
156156

157-
if (timestamp instanceof BsonTimestamp) {
158-
return Instant.ofEpochSecond(((BsonTimestamp) timestamp).getTime());
157+
if (timestamp instanceof BsonTimestamp bsonTimestamp) {
158+
return Instant.ofEpochSecond(bsonTimestamp.getTime());
159159
}
160160

161161
throw new IllegalArgumentException(

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CountQuery.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,15 @@ public Document toQueryDocument() {
6464

6565
for (Map.Entry<String, Object> entry : source.entrySet()) {
6666

67-
if (entry.getValue() instanceof Document && requiresRewrite(entry.getValue())) {
67+
if (entry.getValue() instanceof Document document && requiresRewrite(entry.getValue())) {
6868

69-
Document theValue = (Document) entry.getValue();
70-
target.putAll(createGeoWithin(entry.getKey(), theValue, source.get("$and")));
69+
target.putAll(createGeoWithin(entry.getKey(), document, source.get("$and")));
7170
continue;
7271
}
7372

74-
if (entry.getValue() instanceof Collection && requiresRewrite(entry.getValue())) {
73+
if (entry.getValue() instanceof Collection<?> collection && requiresRewrite(entry.getValue())) {
7574

76-
Collection<?> source = (Collection<?>) entry.getValue();
77-
78-
target.put(entry.getKey(), rewriteCollection(source));
75+
target.put(entry.getKey(), rewriteCollection(collection));
7976
continue;
8077
}
8178

@@ -96,12 +93,12 @@ public Document toQueryDocument() {
9693
*/
9794
private boolean requiresRewrite(Object valueToInspect) {
9895

99-
if (valueToInspect instanceof Document) {
100-
return requiresRewrite((Document) valueToInspect);
96+
if (valueToInspect instanceof Document document) {
97+
return requiresRewrite(document);
10198
}
10299

103-
if (valueToInspect instanceof Collection) {
104-
return requiresRewrite((Collection<?>) valueToInspect);
100+
if (valueToInspect instanceof Collection<?> collection) {
101+
return requiresRewrite(collection);
105102
}
106103

107104
return false;
@@ -110,7 +107,7 @@ private boolean requiresRewrite(Object valueToInspect) {
110107
private boolean requiresRewrite(Collection<?> collection) {
111108

112109
for (Object o : collection) {
113-
if (o instanceof Document && requiresRewrite((Document) o)) {
110+
if (o instanceof Document document && requiresRewrite(document)) {
114111
return true;
115112
}
116113
}
@@ -139,8 +136,8 @@ private Collection<Object> rewriteCollection(Collection<?> source) {
139136
Collection<Object> rewrittenCollection = new ArrayList<>(source.size());
140137

141138
for (Object item : source) {
142-
if (item instanceof Document && requiresRewrite(item)) {
143-
rewrittenCollection.add(CountQuery.of((Document) item).toQueryDocument());
139+
if (item instanceof Document document && requiresRewrite(item)) {
140+
rewrittenCollection.add(CountQuery.of(document).toQueryDocument());
144141
} else {
145142
rewrittenCollection.add(item);
146143
}
@@ -242,8 +239,8 @@ private static Object toCenterCoordinates(Object value) {
242239
return value;
243240
}
244241

245-
if (value instanceof Point) {
246-
return Arrays.asList(((Point) value).getX(), ((Point) value).getY());
242+
if (value instanceof Point point) {
243+
return Arrays.asList(point.getX(), point.getY());
247244
}
248245

249246
if (value instanceof Document document) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,13 @@ private WriteModel<Document> extractAndMapWriteModel(SourceAwareWriteModelHolder
288288

289289
maybeEmitBeforeSaveEvent(it);
290290

291-
if (it.model() instanceof InsertOneModel) {
291+
if (it.model() instanceof InsertOneModel<Document> model) {
292292

293-
Document target = ((InsertOneModel<Document>) it.model()).getDocument();
293+
Document target = model.getDocument();
294294
maybeInvokeBeforeSaveCallback(it.source(), target);
295-
} else if (it.model() instanceof ReplaceOneModel) {
295+
} else if (it.model() instanceof ReplaceOneModel<Document> model) {
296296

297-
Document target = ((ReplaceOneModel<Document>) it.model()).getReplacement();
297+
Document target = model.getReplacement();
298298
maybeInvokeBeforeSaveCallback(it.source(), target);
299299
}
300300

@@ -348,8 +348,8 @@ protected Optional<? extends MongoPersistentEntity<?>> entity() {
348348

349349
private Document getMappedObject(Object source) {
350350

351-
if (source instanceof Document) {
352-
return (Document) source;
351+
if (source instanceof Document document) {
352+
return document;
353353
}
354354

355355
Document sink = new Document();
@@ -364,13 +364,13 @@ private void addModel(Object source, WriteModel<Document> model) {
364364

365365
private void maybeInvokeAfterSaveCallback(SourceAwareWriteModelHolder holder) {
366366

367-
if (holder.model() instanceof InsertOneModel) {
367+
if (holder.model() instanceof InsertOneModel<Document> model) {
368368

369-
Document target = ((InsertOneModel<Document>) holder.model()).getDocument();
369+
Document target = model.getDocument();
370370
maybeInvokeAfterSaveCallback(holder.source(), target);
371-
} else if (holder.model() instanceof ReplaceOneModel) {
371+
} else if (holder.model() instanceof ReplaceOneModel<Document> model) {
372372

373-
Document target = ((ReplaceOneModel<Document>) holder.model()).getReplacement();
373+
Document target = model.getReplacement();
374374
maybeInvokeAfterSaveCallback(holder.source(), target);
375375
}
376376
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private Object[] convertScriptArgs(boolean quote, Object... args) {
150150
return args;
151151
}
152152

153-
List<Object> convertedValues = new ArrayList<Object>(args.length);
153+
List<Object> convertedValues = new ArrayList<>(args.length);
154154

155155
for (Object arg : args) {
156156
convertedValues.add(arg instanceof String && quote ? String.format("'%s'", arg)

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,8 @@ public Query getQueryForVersion() {
575575

576576
@Override
577577
public MappedDocument toMappedDocument(MongoWriter<? super T> writer) {
578-
return MappedDocument.of(map instanceof Document //
579-
? (Document) map //
578+
return MappedDocument.of(map instanceof Document document //
579+
? document //
580580
: new Document(map));
581581
}
582582

@@ -656,8 +656,8 @@ protected SimpleMappedEntity(T map, EntityOperations entityOperations) {
656656
public MappedDocument toMappedDocument(MongoWriter<? super T> writer) {
657657

658658
T bean = getBean();
659-
bean = (T) (bean instanceof Document //
660-
? (Document) bean //
659+
bean = (T) (bean instanceof Document document//
660+
? document //
661661
: new Document(bean));
662662
Document document = new Document();
663663
writer.write(bean, document);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableAggregationOperationSupport.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ private String getCollectionName(Aggregation aggregation) {
9898
return collection;
9999
}
100100

101-
if (aggregation instanceof TypedAggregation) {
102-
103-
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
101+
if (aggregation instanceof TypedAggregation typedAggregation) {
104102

105103
if (typedAggregation.getInputType() != null) {
106104
return template.getCollectionName(typedAggregation.getInputType());

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ JsonSchemaProperty createSchemaProperty(String fieldName, Object type, boolean r
322322

323323
private TypedJsonSchemaObject createSchemaObject(Object type, Collection<?> possibleValues) {
324324

325-
TypedJsonSchemaObject schemaObject = type instanceof Type ? JsonSchemaObject.of(Type.class.cast(type))
325+
TypedJsonSchemaObject schemaObject = type instanceof Type typeObject ? JsonSchemaObject.of(typeObject)
326326
: JsonSchemaObject.of(Class.class.cast(type));
327327

328328
if (!CollectionUtils.isEmpty(possibleValues)) {
@@ -331,23 +331,22 @@ private TypedJsonSchemaObject createSchemaObject(Object type, Collection<?> poss
331331
return schemaObject;
332332
}
333333

334-
private String computePropertyFieldName(PersistentProperty property) {
334+
private String computePropertyFieldName(PersistentProperty<?> property) {
335335

336-
return property instanceof MongoPersistentProperty ? ((MongoPersistentProperty) property).getFieldName()
337-
: property.getName();
336+
return property instanceof MongoPersistentProperty mongoPersistentProperty ?
337+
mongoPersistentProperty.getFieldName() : property.getName();
338338
}
339339

340-
private boolean isRequiredProperty(PersistentProperty property) {
340+
private boolean isRequiredProperty(PersistentProperty<?> property) {
341341
return property.getType().isPrimitive();
342342
}
343343

344344
private Class<?> computeTargetType(PersistentProperty<?> property) {
345345

346-
if (!(property instanceof MongoPersistentProperty)) {
346+
if (!(property instanceof MongoPersistentProperty mongoProperty)) {
347347
return property.getType();
348348
}
349349

350-
MongoPersistentProperty mongoProperty = (MongoPersistentProperty) property;
351350
if (!mongoProperty.isIdProperty()) {
352351
return mongoProperty.getFieldType();
353352
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
9999

100100
if (DATA_INTEGRITY_EXCEPTIONS.contains(exception)) {
101101

102-
if (ex instanceof MongoServerException) {
103-
if (((MongoServerException) ex).getCode() == 11000) {
102+
if (ex instanceof MongoServerException mse) {
103+
if (mse.getCode() == 11000) {
104104
return new DuplicateKeyException(ex.getMessage(), ex);
105105
}
106-
if (ex instanceof MongoBulkWriteException) {
107-
for (BulkWriteError x : ((MongoBulkWriteException) ex).getWriteErrors()) {
106+
if (ex instanceof MongoBulkWriteException bulkException) {
107+
for (BulkWriteError x : bulkException.getWriteErrors()) {
108108
if (x.getCode() == 11000) {
109109
return new DuplicateKeyException(ex.getMessage(), ex);
110110
}
@@ -116,9 +116,9 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
116116
}
117117

118118
// All other MongoExceptions
119-
if (ex instanceof MongoException) {
119+
if (ex instanceof MongoException mongoException) {
120120

121-
int code = ((MongoException) ex).getCode();
121+
int code = mongoException.getCode();
122122

123123
if (MongoDbErrorCodes.isDuplicateKeyCode(code)) {
124124
return new DuplicateKeyException(ex.getMessage(), ex);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ public MongoTemplate(MongoDatabaseFactory mongoDbFactory, @Nullable MongoConvert
255255
// We always have a mapping context in the converter, whether it's a simple one or not
256256
mappingContext = this.mongoConverter.getMappingContext();
257257
// We create indexes based on mapping events
258-
if (mappingContext instanceof MongoMappingContext) {
259-
260-
MongoMappingContext mappingContext = (MongoMappingContext) this.mappingContext;
258+
if (mappingContext instanceof MongoMappingContext mappingContext) {
261259

262260
if (mappingContext.isAutoIndexCreation()) {
263261

@@ -276,8 +274,8 @@ private MongoTemplate(MongoDatabaseFactory dbFactory, MongoTemplate that) {
276274

277275
// we need to (re)create the MappingMongoConverter as we need to have it use a DbRefResolver that operates within
278276
// the sames session. Otherwise loading referenced objects would happen outside of it.
279-
if (that.mongoConverter instanceof MappingMongoConverter) {
280-
this.mongoConverter = ((MappingMongoConverter) that.mongoConverter).with(dbFactory);
277+
if (that.mongoConverter instanceof MappingMongoConverter mappingMongoConverter) {
278+
this.mongoConverter = mappingMongoConverter.with(dbFactory);
281279
} else {
282280
this.mongoConverter = that.mongoConverter;
283281
}
@@ -366,8 +364,8 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
366364
setEntityCallbacks(EntityCallbacks.create(applicationContext));
367365
}
368366

369-
if (mappingContext instanceof ApplicationEventPublisherAware) {
370-
((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(eventPublisher);
367+
if (mappingContext instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
368+
applicationEventPublisherAware.setApplicationEventPublisher(eventPublisher);
371369
}
372370

373371
resourceLoader = applicationContext;
@@ -449,8 +447,8 @@ private void prepareIndexCreator(ApplicationContext context) {
449447
}
450448
}
451449

452-
if (context instanceof ConfigurableApplicationContext && indexCreator != null) {
453-
((ConfigurableApplicationContext) context).addApplicationListener(indexCreator);
450+
if (context instanceof ConfigurableApplicationContext configurableApplicationContext && indexCreator != null) {
451+
configurableApplicationContext.addApplicationListener(indexCreator);
454452
}
455453
}
456454

@@ -1312,7 +1310,7 @@ private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc
13121310

13131311
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
13141312
if (wc == null || wc.getWObject() == null
1315-
|| (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) {
1313+
|| (wc.getWObject() instanceof Number concern && concern.intValue() < 1)) {
13161314
return WriteConcern.ACKNOWLEDGED;
13171315
}
13181316
}
@@ -2230,7 +2228,7 @@ protected <O> Stream<O> aggregateStream(Aggregation aggregation, String collecti
22302228
cursor = hintFunction.apply(mongoDbFactory, cursor::hintString, cursor::hint);
22312229
}
22322230

2233-
Class<?> domainType = aggregation instanceof TypedAggregation ? ((TypedAggregation) aggregation).getInputType()
2231+
Class<?> domainType = aggregation instanceof TypedAggregation typedAggregation ? typedAggregation.getInputType()
22342232
: null;
22352233

22362234
Optionals.firstNonEmpty(options::getCollation, //
@@ -3120,8 +3118,8 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
31203118
opts.arrayFilters(arrayFilters);
31213119
}
31223120

3123-
if (update instanceof Document) {
3124-
return collectionPreparer.prepare(collection).findOneAndUpdate(query, (Document) update, opts);
3121+
if (update instanceof Document document) {
3122+
return collectionPreparer.prepare(collection).findOneAndUpdate(query, document, opts);
31253123
} else if (update instanceof List) {
31263124
return collectionPreparer.prepare(collection).findOneAndUpdate(query, (List<Document>) update, opts);
31273125
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,12 @@ private Document evaluateFields(@Nullable MongoPersistentEntity<?> entity) {
388388

389389
for (Entry<String, Object> entry : fields.entrySet()) {
390390

391-
if (entry.getValue() instanceof MongoExpression) {
391+
if (entry.getValue() instanceof MongoExpression mongoExpression) {
392392

393393
AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
394394
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
395395

396-
evaluated.put(entry.getKey(), AggregationExpression.from((MongoExpression) entry.getValue()).toDocument(ctx));
396+
evaluated.put(entry.getKey(), AggregationExpression.from(mongoExpression).toDocument(ctx));
397397
} else {
398398
evaluated.put(entry.getKey(), entry.getValue());
399399
}
@@ -456,7 +456,7 @@ class DistinctQueryContext extends QueryContext {
456456
*/
457457
private DistinctQueryContext(@Nullable Object query, String fieldName) {
458458

459-
super(query instanceof Document ? new BasicQuery((Document) query) : (Query) query);
459+
super(query instanceof Document document ? new BasicQuery(document) : (Query) query);
460460
this.fieldName = fieldName;
461461
}
462462

@@ -907,10 +907,10 @@ class AggregationDefinition {
907907

908908
this.aggregation = aggregation;
909909

910-
if (aggregation instanceof TypedAggregation) {
911-
this.inputType = ((TypedAggregation<?>) aggregation).getInputType();
912-
} else if (aggregationOperationContext instanceof TypeBasedAggregationOperationContext) {
913-
this.inputType = ((TypeBasedAggregationOperationContext) aggregationOperationContext).getType();
910+
if (aggregation instanceof TypedAggregation typedAggregation) {
911+
this.inputType = typedAggregation.getInputType();
912+
} else if (aggregationOperationContext instanceof TypeBasedAggregationOperationContext typeBasedAggregationOperationContext) {
913+
this.inputType = typeBasedAggregationOperationContext.getType();
914914
} else {
915915
this.inputType = null;
916916
}
@@ -935,8 +935,8 @@ class AggregationDefinition {
935935

936936
this.aggregation = aggregation;
937937

938-
if (aggregation instanceof TypedAggregation) {
939-
this.inputType = ((TypedAggregation<?>) aggregation).getInputType();
938+
if (aggregation instanceof TypedAggregation typedAggregation) {
939+
this.inputType = typedAggregation.getInputType();
940940
} else {
941941
this.inputType = inputType;
942942
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveAggregationOperationSupport.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ private String getCollectionName(Aggregation aggregation) {
9898
return collection;
9999
}
100100

101-
if (aggregation instanceof TypedAggregation) {
102-
103-
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
101+
if (aggregation instanceof TypedAggregation typedAggregation) {
104102

105103
if (typedAggregation.getInputType() != null) {
106104
return template.getCollectionName(typedAggregation.getInputType());

0 commit comments

Comments
 (0)