Polishing.
Extract duplicates into peek method. See #4312 Original pull request: #4323
This commit is contained in:
@@ -679,8 +679,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
* in between.
|
||||
*/
|
||||
if (value instanceof Document document) {
|
||||
if(property.isMap()) {
|
||||
if(document.isEmpty() || document.values().iterator().next() instanceof DBRef) {
|
||||
if (property.isMap()) {
|
||||
if (document.isEmpty() || peek(document.values()) instanceof DBRef) {
|
||||
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, null, callback, handler));
|
||||
} else {
|
||||
accessor.setProperty(property, readMap(context, document, property.getTypeInformation()));
|
||||
@@ -688,8 +688,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
} else {
|
||||
accessor.setProperty(property, read(property.getActualType(), document));
|
||||
}
|
||||
} else if (value instanceof Collection<?> collection && collection.size() > 0
|
||||
&& collection.iterator().next() instanceof Document) {
|
||||
} else if (value instanceof Collection<?> collection && !collection.isEmpty()
|
||||
&& peek(collection) instanceof Document) {
|
||||
accessor.setProperty(property, readCollectionOrArray(context, collection, property.getTypeInformation()));
|
||||
} else {
|
||||
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, null, callback, handler));
|
||||
@@ -722,8 +722,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
|
||||
// DATAMONGO-913
|
||||
if (object instanceof LazyLoadingProxy) {
|
||||
return ((LazyLoadingProxy) object).toDBRef();
|
||||
if (object instanceof LazyLoadingProxy proxy) {
|
||||
return proxy.toDBRef();
|
||||
}
|
||||
|
||||
return createDBRef(object, referringProperty);
|
||||
@@ -1022,11 +1022,13 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
.getPointer();
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>(targetCollection.size()));
|
||||
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class),
|
||||
new ArrayList<>(targetCollection.size()));
|
||||
}
|
||||
|
||||
if (property.hasExplicitWriteTarget()) {
|
||||
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
|
||||
return writeCollectionInternal(collection, new FieldTypeInformation<>(property),
|
||||
new ArrayList<>(collection.size()));
|
||||
}
|
||||
|
||||
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
|
||||
@@ -1674,7 +1676,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
|
||||
List<Object> result = bulkReadAndConvertDBRefs(context, Collections.singletonList(dbref), type);
|
||||
return CollectionUtils.isEmpty(result) ? null : result.iterator().next();
|
||||
return CollectionUtils.isEmpty(result) ? null : peek(result);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@@ -1699,10 +1701,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Document> referencedRawDocuments = dbrefs.size() == 1
|
||||
? Collections.singletonList(readRef(dbrefs.iterator().next()))
|
||||
List<Document> referencedRawDocuments = dbrefs.size() == 1 ? Collections.singletonList(readRef(peek(dbrefs)))
|
||||
: bulkReadRefs(dbrefs);
|
||||
String collectionName = dbrefs.iterator().next().getCollectionName();
|
||||
String collectionName = peek(dbrefs).getCollectionName();
|
||||
|
||||
List<T> targetList = new ArrayList<>(dbrefs.size());
|
||||
|
||||
@@ -1847,6 +1848,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
return true;
|
||||
}
|
||||
|
||||
private static <T> T peek(Iterable<T> result) {
|
||||
return result.iterator().next();
|
||||
}
|
||||
|
||||
static Predicate<MongoPersistentProperty> isIdentifier(PersistentEntity<?, ?> entity) {
|
||||
return entity::isIdProperty;
|
||||
}
|
||||
@@ -1925,7 +1930,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
public MongoDbPropertyValueProvider withContext(ConversionContext context) {
|
||||
|
||||
return context == this.context ? this : new MongoDbPropertyValueProvider(context, accessor, evaluator);
|
||||
return context == this.context ? this
|
||||
: new MongoDbPropertyValueProvider(context, accessor, evaluator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user