Skip to content

Commit 39e30cd

Browse files
committed
Polishing.
Extract duplicates into peek method. See #4312 Original pull request: #4323
1 parent e611f2a commit 39e30cd

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -679,17 +679,17 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
679679
* in between.
680680
*/
681681
if (value instanceof Document document) {
682-
if(property.isMap()) {
683-
if(document.isEmpty() || document.values().iterator().next() instanceof DBRef) {
682+
if (property.isMap()) {
683+
if (document.isEmpty() || peek(document.values()) instanceof DBRef) {
684684
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, null, callback, handler));
685685
} else {
686686
accessor.setProperty(property, readMap(context, document, property.getTypeInformation()));
687687
}
688688
} else {
689689
accessor.setProperty(property, read(property.getActualType(), document));
690690
}
691-
} else if (value instanceof Collection<?> collection && collection.size() > 0
692-
&& collection.iterator().next() instanceof Document) {
691+
} else if (value instanceof Collection<?> collection && !collection.isEmpty()
692+
&& peek(collection) instanceof Document) {
693693
accessor.setProperty(property, readCollectionOrArray(context, collection, property.getTypeInformation()));
694694
} else {
695695
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, null, callback, handler));
@@ -722,8 +722,8 @@ public DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referringP
722722
}
723723

724724
// DATAMONGO-913
725-
if (object instanceof LazyLoadingProxy) {
726-
return ((LazyLoadingProxy) object).toDBRef();
725+
if (object instanceof LazyLoadingProxy proxy) {
726+
return proxy.toDBRef();
727727
}
728728

729729
return createDBRef(object, referringProperty);
@@ -1022,11 +1022,13 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
10221022
.getPointer();
10231023
}).collect(Collectors.toList());
10241024

1025-
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>(targetCollection.size()));
1025+
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class),
1026+
new ArrayList<>(targetCollection.size()));
10261027
}
10271028

10281029
if (property.hasExplicitWriteTarget()) {
1029-
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
1030+
return writeCollectionInternal(collection, new FieldTypeInformation<>(property),
1031+
new ArrayList<>(collection.size()));
10301032
}
10311033

10321034
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
@@ -1674,7 +1676,7 @@ private Object readDBRef(ConversionContext context, @Nullable DBRef dbref, TypeI
16741676
}
16751677

16761678
List<Object> result = bulkReadAndConvertDBRefs(context, Collections.singletonList(dbref), type);
1677-
return CollectionUtils.isEmpty(result) ? null : result.iterator().next();
1679+
return CollectionUtils.isEmpty(result) ? null : peek(result);
16781680
}
16791681

16801682
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -1699,10 +1701,9 @@ private <T> List<T> bulkReadAndConvertDBRefs(ConversionContext context, List<DBR
16991701
return Collections.emptyList();
17001702
}
17011703

1702-
List<Document> referencedRawDocuments = dbrefs.size() == 1
1703-
? Collections.singletonList(readRef(dbrefs.iterator().next()))
1704+
List<Document> referencedRawDocuments = dbrefs.size() == 1 ? Collections.singletonList(readRef(peek(dbrefs)))
17041705
: bulkReadRefs(dbrefs);
1705-
String collectionName = dbrefs.iterator().next().getCollectionName();
1706+
String collectionName = peek(dbrefs).getCollectionName();
17061707

17071708
List<T> targetList = new ArrayList<>(dbrefs.size());
17081709

@@ -1847,6 +1848,10 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<?> s
18471848
return true;
18481849
}
18491850

1851+
private static <T> T peek(Iterable<T> result) {
1852+
return result.iterator().next();
1853+
}
1854+
18501855
static Predicate<MongoPersistentProperty> isIdentifier(PersistentEntity<?, ?> entity) {
18511856
return entity::isIdProperty;
18521857
}
@@ -1925,7 +1930,8 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19251930

19261931
public MongoDbPropertyValueProvider withContext(ConversionContext context) {
19271932

1928-
return context == this.context ? this : new MongoDbPropertyValueProvider(context, accessor, evaluator);
1933+
return context == this.context ? this
1934+
: new MongoDbPropertyValueProvider(context, accessor, evaluator);
19291935
}
19301936
}
19311937

0 commit comments

Comments
 (0)