@@ -348,7 +348,8 @@ String getFieldName(MongoPersistentProperty prop) {
348
348
populateProperties (context , mappedEntity , documentAccessor , evaluator , instance );
349
349
350
350
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 );
352
353
353
354
readProperties (context , mappedEntity , convertingAccessor , documentAccessor , valueProvider , evaluator ,
354
355
Predicates .isTrue ());
@@ -660,17 +661,17 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
660
661
* in between.
661
662
*/
662
663
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 ) {
665
666
accessor .setProperty (property , dbRefResolver .resolveDbRef (property , null , callback , handler ));
666
667
} else {
667
668
accessor .setProperty (property , readMap (context , document , property .getTypeInformation ()));
668
669
}
669
670
} else {
670
671
accessor .setProperty (property , read (property .getActualType (), document ));
671
672
}
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 ) {
674
675
accessor .setProperty (property , readCollectionOrArray (context , collection , property .getTypeInformation ()));
675
676
} else {
676
677
accessor .setProperty (property , dbRefResolver .resolveDbRef (property , null , callback , handler ));
@@ -703,8 +704,8 @@ public DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referringP
703
704
}
704
705
705
706
// DATAMONGO-913
706
- if (object instanceof LazyLoadingProxy ) {
707
- return (( LazyLoadingProxy ) object ) .toDBRef ();
707
+ if (object instanceof LazyLoadingProxy proxy ) {
708
+ return proxy .toDBRef ();
708
709
}
709
710
710
711
return createDBRef (object , referringProperty );
@@ -895,7 +896,8 @@ private void writeAssociation(Association<MongoPersistentProperty> association,
895
896
}
896
897
897
898
@ 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 ) {
899
901
900
902
if (obj == null ) {
901
903
return ;
@@ -1009,11 +1011,13 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
1009
1011
.getPointer ();
1010
1012
}).collect (Collectors .toList ());
1011
1013
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 ()));
1013
1016
}
1014
1017
1015
1018
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 ()));
1017
1021
}
1018
1022
1019
1023
return writeCollectionInternal (collection , property .getTypeInformation (), new ArrayList <>(collection .size ()));
@@ -1244,7 +1248,8 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
1244
1248
BsonUtils .addToMap (bson , key , getPotentiallyConvertedSimpleWrite (value , Object .class ));
1245
1249
}
1246
1250
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 ) {
1248
1253
DocumentAccessor accessor = new DocumentAccessor (bson );
1249
1254
1250
1255
if (conversions .hasValueConverter (property )) {
@@ -1667,7 +1672,7 @@ private Object readDBRef(ConversionContext context, @Nullable DBRef dbref, TypeI
1667
1672
}
1668
1673
1669
1674
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 );
1671
1676
}
1672
1677
1673
1678
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -1692,10 +1697,9 @@ private <T> List<T> bulkReadAndConvertDBRefs(ConversionContext context, List<DBR
1692
1697
return Collections .emptyList ();
1693
1698
}
1694
1699
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 )))
1697
1701
: bulkReadRefs (dbrefs );
1698
- String collectionName = dbrefs . iterator (). next ( ).getCollectionName ();
1702
+ String collectionName = peek ( dbrefs ).getCollectionName ();
1699
1703
1700
1704
List <T > targetList = new ArrayList <>(dbrefs .size ());
1701
1705
@@ -1840,6 +1844,10 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<?> s
1840
1844
return true ;
1841
1845
}
1842
1846
1847
+ private static <T > T peek (Iterable <T > result ) {
1848
+ return result .iterator ().next ();
1849
+ }
1850
+
1843
1851
static Predicate <MongoPersistentProperty > isIdentifier (PersistentEntity <?, ?> entity ) {
1844
1852
return entity ::isIdProperty ;
1845
1853
}
@@ -1920,7 +1928,8 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1920
1928
1921
1929
public MongoDbPropertyValueProvider withContext (ConversionContext context ) {
1922
1930
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 );
1924
1933
}
1925
1934
}
1926
1935
0 commit comments