Skip to content

Commit 7f50fe1

Browse files
committed
Polishing.
Extract duplicates into peek method. See #4312 Original pull request: #4323
1 parent a2127a4 commit 7f50fe1

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ String getFieldName(MongoPersistentProperty prop) {
348348
populateProperties(context, mappedEntity, documentAccessor, evaluator, instance);
349349

350350
PersistentPropertyAccessor<?> convertingAccessor = new ConvertingPropertyAccessor<>(accessor, conversionService);
351-
MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(context, documentAccessor, evaluator, spELContext);
351+
MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(context, documentAccessor, evaluator,
352+
spELContext);
352353

353354
readProperties(context, mappedEntity, convertingAccessor, documentAccessor, valueProvider, evaluator,
354355
Predicates.isTrue());
@@ -660,17 +661,17 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
660661
* in between.
661662
*/
662663
if (value instanceof Document document) {
663-
if(property.isMap()) {
664-
if(document.isEmpty() || document.values().iterator().next() instanceof DBRef) {
664+
if (property.isMap()) {
665+
if (document.isEmpty() || peek(document.values()) instanceof DBRef) {
665666
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, null, callback, handler));
666667
} else {
667668
accessor.setProperty(property, readMap(context, document, property.getTypeInformation()));
668669
}
669670
} else {
670671
accessor.setProperty(property, read(property.getActualType(), document));
671672
}
672-
} else if (value instanceof Collection<?> collection && collection.size() > 0
673-
&& collection.iterator().next() instanceof Document) {
673+
} else if (value instanceof Collection<?> collection && !collection.isEmpty()
674+
&& peek(collection) instanceof Document) {
674675
accessor.setProperty(property, readCollectionOrArray(context, collection, property.getTypeInformation()));
675676
} else {
676677
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, null, callback, handler));
@@ -703,8 +704,8 @@ public DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referringP
703704
}
704705

705706
// DATAMONGO-913
706-
if (object instanceof LazyLoadingProxy) {
707-
return ((LazyLoadingProxy) object).toDBRef();
707+
if (object instanceof LazyLoadingProxy proxy) {
708+
return proxy.toDBRef();
708709
}
709710

710711
return createDBRef(object, referringProperty);
@@ -895,7 +896,8 @@ private void writeAssociation(Association<MongoPersistentProperty> association,
895896
}
896897

897898
@SuppressWarnings({ "unchecked" })
898-
protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor accessor, MongoPersistentProperty prop, PersistentPropertyAccessor<?> persistentPropertyAccessor) {
899+
protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor accessor, MongoPersistentProperty prop,
900+
PersistentPropertyAccessor<?> persistentPropertyAccessor) {
899901

900902
if (obj == null) {
901903
return;
@@ -1009,11 +1011,13 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
10091011
.getPointer();
10101012
}).collect(Collectors.toList());
10111013

1012-
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>(targetCollection.size()));
1014+
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class),
1015+
new ArrayList<>(targetCollection.size()));
10131016
}
10141017

10151018
if (property.hasExplicitWriteTarget()) {
1016-
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
1019+
return writeCollectionInternal(collection, new FieldTypeInformation<>(property),
1020+
new ArrayList<>(collection.size()));
10171021
}
10181022

10191023
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
@@ -1244,7 +1248,8 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
12441248
BsonUtils.addToMap(bson, key, getPotentiallyConvertedSimpleWrite(value, Object.class));
12451249
}
12461250

1247-
private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property, PersistentPropertyAccessor<?> persistentPropertyAccessor) {
1251+
private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property,
1252+
PersistentPropertyAccessor<?> persistentPropertyAccessor) {
12481253
DocumentAccessor accessor = new DocumentAccessor(bson);
12491254

12501255
if (conversions.hasValueConverter(property)) {
@@ -1667,7 +1672,7 @@ private Object readDBRef(ConversionContext context, @Nullable DBRef dbref, TypeI
16671672
}
16681673

16691674
List<Object> result = bulkReadAndConvertDBRefs(context, Collections.singletonList(dbref), type);
1670-
return CollectionUtils.isEmpty(result) ? null : result.iterator().next();
1675+
return CollectionUtils.isEmpty(result) ? null : peek(result);
16711676
}
16721677

16731678
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -1692,10 +1697,9 @@ private <T> List<T> bulkReadAndConvertDBRefs(ConversionContext context, List<DBR
16921697
return Collections.emptyList();
16931698
}
16941699

1695-
List<Document> referencedRawDocuments = dbrefs.size() == 1
1696-
? Collections.singletonList(readRef(dbrefs.iterator().next()))
1700+
List<Document> referencedRawDocuments = dbrefs.size() == 1 ? Collections.singletonList(readRef(peek(dbrefs)))
16971701
: bulkReadRefs(dbrefs);
1698-
String collectionName = dbrefs.iterator().next().getCollectionName();
1702+
String collectionName = peek(dbrefs).getCollectionName();
16991703

17001704
List<T> targetList = new ArrayList<>(dbrefs.size());
17011705

@@ -1840,6 +1844,10 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<?> s
18401844
return true;
18411845
}
18421846

1847+
private static <T> T peek(Iterable<T> result) {
1848+
return result.iterator().next();
1849+
}
1850+
18431851
static Predicate<MongoPersistentProperty> isIdentifier(PersistentEntity<?, ?> entity) {
18441852
return entity::isIdProperty;
18451853
}
@@ -1920,7 +1928,8 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19201928

19211929
public MongoDbPropertyValueProvider withContext(ConversionContext context) {
19221930

1923-
return context == this.context ? this : new MongoDbPropertyValueProvider(context, accessor, evaluator, spELContext);
1931+
return context == this.context ? this
1932+
: new MongoDbPropertyValueProvider(context, accessor, evaluator, spELContext);
19241933
}
19251934
}
19261935

0 commit comments

Comments
 (0)