Skip to content

Instanceof casting simplification #4265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ private Document fixDocumentKeys(Document doc) {
for (Object key : doc.keySet()) {

Object value = doc.get(key);
if (value instanceof Document) {
value = fixDocumentKeys((Document) value);
} else if (value instanceof BasicDBObject) {
value = fixDocumentKeys(new Document((BasicDBObject) value));
if (value instanceof Document document) {
value = fixDocumentKeys(document);
} else if (value instanceof BasicDBObject basicDBObject) {
value = fixDocumentKeys(new Document(basicDBObject));
}

if (key instanceof String) {
if (key instanceof String newKey) {

String newKey = (String) key;
if (newKey.contains(".")) {
newKey = newKey.replace('.', ',');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ private static <T> Object doGetTimestamp(Object timestamp, Class<T> targetType)
return timestamp;
}

if (timestamp instanceof Instant) {
return new BsonTimestamp((int) ((Instant) timestamp).getEpochSecond(), 0);
if (timestamp instanceof Instant instant) {
return new BsonTimestamp((int) instant.getEpochSecond(), 0);
}

if (timestamp instanceof BsonTimestamp) {
return Instant.ofEpochSecond(((BsonTimestamp) timestamp).getTime());
if (timestamp instanceof BsonTimestamp bsonTimestamp) {
return Instant.ofEpochSecond(bsonTimestamp.getTime());
}

throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,15 @@ public Document toQueryDocument() {

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

if (entry.getValue() instanceof Document && requiresRewrite(entry.getValue())) {
if (entry.getValue() instanceof Document document && requiresRewrite(entry.getValue())) {

Document theValue = (Document) entry.getValue();
target.putAll(createGeoWithin(entry.getKey(), theValue, source.get("$and")));
target.putAll(createGeoWithin(entry.getKey(), document, source.get("$and")));
continue;
}

if (entry.getValue() instanceof Collection && requiresRewrite(entry.getValue())) {
if (entry.getValue() instanceof Collection<?> collection && requiresRewrite(entry.getValue())) {

Collection<?> source = (Collection<?>) entry.getValue();

target.put(entry.getKey(), rewriteCollection(source));
target.put(entry.getKey(), rewriteCollection(collection));
continue;
}

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

if (valueToInspect instanceof Document) {
return requiresRewrite((Document) valueToInspect);
if (valueToInspect instanceof Document document) {
return requiresRewrite(document);
}

if (valueToInspect instanceof Collection) {
return requiresRewrite((Collection<?>) valueToInspect);
if (valueToInspect instanceof Collection<?> collection) {
return requiresRewrite(collection);
}

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

for (Object o : collection) {
if (o instanceof Document && requiresRewrite((Document) o)) {
if (o instanceof Document document && requiresRewrite(document)) {
return true;
}
}
Expand Down Expand Up @@ -139,8 +136,8 @@ private Collection<Object> rewriteCollection(Collection<?> source) {
Collection<Object> rewrittenCollection = new ArrayList<>(source.size());

for (Object item : source) {
if (item instanceof Document && requiresRewrite(item)) {
rewrittenCollection.add(CountQuery.of((Document) item).toQueryDocument());
if (item instanceof Document document && requiresRewrite(item)) {
rewrittenCollection.add(CountQuery.of(document).toQueryDocument());
} else {
rewrittenCollection.add(item);
}
Expand Down Expand Up @@ -242,8 +239,8 @@ private static Object toCenterCoordinates(Object value) {
return value;
}

if (value instanceof Point) {
return Arrays.asList(((Point) value).getX(), ((Point) value).getY());
if (value instanceof Point point) {
return Arrays.asList(point.getX(), point.getY());
}

if (value instanceof Document document) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ private BulkWriteResult bulkWriteTo(MongoCollection<Document> collection) {
bulkOptions);
} catch (RuntimeException ex) {

if (ex instanceof MongoBulkWriteException) {
if (ex instanceof MongoBulkWriteException mongoBulkWriteException) {

MongoBulkWriteException mongoBulkWriteException = (MongoBulkWriteException) ex;
if (mongoBulkWriteException.getWriteConcernError() != null) {
throw new DataIntegrityViolationException(ex.getMessage(), ex);
}
Expand All @@ -284,13 +283,13 @@ private WriteModel<Document> extractAndMapWriteModel(SourceAwareWriteModelHolder

maybeEmitBeforeSaveEvent(it);

if (it.getModel() instanceof InsertOneModel) {
if (it.getModel() instanceof InsertOneModel<Document> model) {

Document target = ((InsertOneModel<Document>) it.getModel()).getDocument();
Document target = model.getDocument();
maybeInvokeBeforeSaveCallback(it.getSource(), target);
} else if (it.getModel() instanceof ReplaceOneModel) {
} else if (it.getModel() instanceof ReplaceOneModel<Document> model) {

Document target = ((ReplaceOneModel<Document>) it.getModel()).getReplacement();
Document target = model.getReplacement();
maybeInvokeBeforeSaveCallback(it.getSource(), target);
}

Expand Down Expand Up @@ -324,33 +323,19 @@ private BulkOperations update(Query query, Update update, boolean upsert, boolea

private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) {

if (writeModel instanceof UpdateOneModel) {

UpdateOneModel<Document> model = (UpdateOneModel<Document>) writeModel;

return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()),
model.getOptions());
if (writeModel instanceof UpdateOneModel<Document> model) {
return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions());
}

if (writeModel instanceof UpdateManyModel) {

UpdateManyModel<Document> model = (UpdateManyModel<Document>) writeModel;

return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()),
model.getOptions());
if (writeModel instanceof UpdateManyModel<Document> model) {
return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions());
}

if (writeModel instanceof DeleteOneModel) {

DeleteOneModel<Document> model = (DeleteOneModel<Document>) writeModel;

if (writeModel instanceof DeleteOneModel<Document> model) {
return new DeleteOneModel<>(getMappedQuery(model.getFilter()), model.getOptions());
}

if (writeModel instanceof DeleteManyModel) {

DeleteManyModel<Document> model = (DeleteManyModel<Document>) writeModel;

if (writeModel instanceof DeleteManyModel<Document> model) {
return new DeleteManyModel<>(getMappedQuery(model.getFilter()), model.getOptions());
}

Expand All @@ -367,8 +352,8 @@ private Bson getMappedQuery(Bson query) {

private Document getMappedObject(Object source) {

if (source instanceof Document) {
return (Document) source;
if (source instanceof Document document) {
return document;
}

Document sink = new Document();
Expand All @@ -383,39 +368,39 @@ private void addModel(Object source, WriteModel<Document> model) {

private void maybeEmitBeforeSaveEvent(SourceAwareWriteModelHolder holder) {

if (holder.getModel() instanceof InsertOneModel) {
if (holder.getModel() instanceof InsertOneModel<Document> model) {

Document target = ((InsertOneModel<Document>) holder.getModel()).getDocument();
Document target = model.getDocument();
maybeEmitEvent(new BeforeSaveEvent<>(holder.getSource(), target, collectionName));
} else if (holder.getModel() instanceof ReplaceOneModel) {
} else if (holder.getModel() instanceof ReplaceOneModel<Document> model) {

Document target = ((ReplaceOneModel<Document>) holder.getModel()).getReplacement();
Document target = model.getReplacement();
maybeEmitEvent(new BeforeSaveEvent<>(holder.getSource(), target, collectionName));
}
}

private void maybeEmitAfterSaveEvent(SourceAwareWriteModelHolder holder) {

if (holder.getModel() instanceof InsertOneModel) {
if (holder.getModel() instanceof InsertOneModel<Document> model) {

Document target = ((InsertOneModel<Document>) holder.getModel()).getDocument();
Document target = model.getDocument();
maybeEmitEvent(new AfterSaveEvent<>(holder.getSource(), target, collectionName));
} else if (holder.getModel() instanceof ReplaceOneModel) {
} else if (holder.getModel() instanceof ReplaceOneModel<Document> model) {

Document target = ((ReplaceOneModel<Document>) holder.getModel()).getReplacement();
Document target = model.getReplacement();
maybeEmitEvent(new AfterSaveEvent<>(holder.getSource(), target, collectionName));
}
}

private void maybeInvokeAfterSaveCallback(SourceAwareWriteModelHolder holder) {

if (holder.getModel() instanceof InsertOneModel) {
if (holder.getModel() instanceof InsertOneModel<Document> model) {

Document target = ((InsertOneModel<Document>) holder.getModel()).getDocument();
Document target = model.getDocument();
maybeInvokeAfterSaveCallback(holder.getSource(), target);
} else if (holder.getModel() instanceof ReplaceOneModel) {
} else if (holder.getModel() instanceof ReplaceOneModel<Document> model) {

Document target = ((ReplaceOneModel<Document>) holder.getModel()).getReplacement();
Document target = model.getReplacement();
maybeInvokeAfterSaveCallback(holder.getSource(), target);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private Object[] convertScriptArgs(boolean quote, Object... args) {
return args;
}

List<Object> convertedValues = new ArrayList<Object>(args.length);
List<Object> convertedValues = new ArrayList<>(args.length);

for (Object arg : args) {
convertedValues.add(arg instanceof String && quote ? String.format("'%s'", arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ public Query getQueryForVersion() {

@Override
public MappedDocument toMappedDocument(MongoWriter<? super T> writer) {
return MappedDocument.of(map instanceof Document //
? (Document) map //
return MappedDocument.of(map instanceof Document document //
? document //
: new Document(map));
}

Expand Down Expand Up @@ -576,8 +576,8 @@ protected SimpleMappedEntity(T map) {
public MappedDocument toMappedDocument(MongoWriter<? super T> writer) {

T bean = getBean();
bean = (T) (bean instanceof Document //
? (Document) bean //
bean = (T) (bean instanceof Document document//
? document //
: new Document(bean));
Document document = new Document();
writer.write(bean, document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ private String getCollectionName(Aggregation aggregation) {
return collection;
}

if (aggregation instanceof TypedAggregation) {

TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
if (aggregation instanceof TypedAggregation typedAggregation) {

if (typedAggregation.getInputType() != null) {
return template.getCollectionName(typedAggregation.getInputType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ JsonSchemaProperty createSchemaProperty(String fieldName, Object type, boolean r

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

TypedJsonSchemaObject schemaObject = type instanceof Type ? JsonSchemaObject.of(Type.class.cast(type))
TypedJsonSchemaObject schemaObject = type instanceof Type typeObject ? JsonSchemaObject.of(typeObject)
: JsonSchemaObject.of(Class.class.cast(type));

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

private String computePropertyFieldName(PersistentProperty property) {
private String computePropertyFieldName(PersistentProperty<?> property) {

return property instanceof MongoPersistentProperty ? ((MongoPersistentProperty) property).getFieldName()
: property.getName();
return property instanceof MongoPersistentProperty mongoPersistentProperty ?
mongoPersistentProperty.getFieldName() : property.getName();
}

private boolean isRequiredProperty(PersistentProperty property) {
private boolean isRequiredProperty(PersistentProperty<?> property) {
return property.getType().isPrimitive();
}

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

if (!(property instanceof MongoPersistentProperty)) {
if (!(property instanceof MongoPersistentProperty mongoProperty)) {
return property.getType();
}

MongoPersistentProperty mongoProperty = (MongoPersistentProperty) property;
if (!mongoProperty.isIdProperty()) {
return mongoProperty.getFieldType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {

if (DATA_INTEGRITY_EXCEPTIONS.contains(exception)) {

if (ex instanceof MongoServerException) {
if (((MongoServerException) ex).getCode() == 11000) {
if (ex instanceof MongoServerException mse) {
if (mse.getCode() == 11000) {
return new DuplicateKeyException(ex.getMessage(), ex);
}
if (ex instanceof MongoBulkWriteException) {
for (BulkWriteError x : ((MongoBulkWriteException) ex).getWriteErrors()) {
if (ex instanceof MongoBulkWriteException bulkException) {
for (BulkWriteError x : bulkException.getWriteErrors()) {
if (x.getCode() == 11000) {
return new DuplicateKeyException(ex.getMessage(), ex);
}
Expand All @@ -114,9 +114,9 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
}

// All other MongoExceptions
if (ex instanceof MongoException) {
if (ex instanceof MongoException mongoException) {

int code = ((MongoException) ex).getCode();
int code = mongoException.getCode();

if (MongoDbErrorCodes.isDuplicateKeyCode(code)) {
return new DuplicateKeyException(ex.getMessage(), ex);
Expand Down
Loading