Skip to content

Commit 7416116

Browse files
DATAMONGO-1666 - Polishing.
Some minor language level cleanups and removal of deprecated API usage. Original Pull Request: #457
1 parent bcba123 commit 7416116

File tree

2 files changed

+83
-94
lines changed

2 files changed

+83
-94
lines changed

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

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private ParameterValueProvider<MongoPersistentProperty> getParameterProvider(Mon
250250
Bson source, DefaultSpELExpressionEvaluator evaluator, ObjectPath path) {
251251

252252
MongoDbPropertyValueProvider provider = new MongoDbPropertyValueProvider(source, evaluator, path);
253-
PersistentEntityParameterValueProvider<MongoPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<MongoPersistentProperty>(
253+
PersistentEntityParameterValueProvider<MongoPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<>(
254254
entity, provider, path.getCurrentObject());
255255

256256
return new ConverterAwareSpELExpressionParameterValueProvider(evaluator, conversionService, parameterProvider,
@@ -273,7 +273,7 @@ private <S extends Object> S read(final MongoPersistentEntity<S> entity, final D
273273
DocumentAccessor documentAccessor = new DocumentAccessor(bson);
274274

275275
// make sure id property is set before all other properties
276-
Optional<Object> idValue = idProperty.filter(it -> documentAccessor.hasValue(it)).map(it -> {
276+
Optional<Object> idValue = idProperty.filter(documentAccessor::hasValue).map(it -> {
277277

278278
Optional<Object> value = getValueInternal(it, bson, evaluator, path);
279279
accessor.setProperty(it, value);
@@ -285,42 +285,38 @@ private <S extends Object> S read(final MongoPersistentEntity<S> entity, final D
285285
idValue.isPresent() ? idProperty.map(it -> bson.get(it.getFieldName())).orElse(null) : null);
286286

287287
// Set properties not already set in the constructor
288-
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
289-
public void doWithPersistentProperty(MongoPersistentProperty prop) {
288+
entity.doWithProperties((PropertyHandler<MongoPersistentProperty>) prop -> {
290289

291-
// we skip the id property since it was already set
292-
if (idProperty != null && idProperty.equals(prop)) {
293-
return;
294-
}
295-
296-
if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) {
297-
return;
298-
}
290+
// we skip the id property since it was already set
291+
if (idProperty != null && idProperty.equals(prop)) {
292+
return;
293+
}
299294

300-
accessor.setProperty(prop, getValueInternal(prop, bson, evaluator, currentPath));
295+
if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) {
296+
return;
301297
}
298+
299+
accessor.setProperty(prop, getValueInternal(prop, bson, evaluator, currentPath));
302300
});
303301

304302
// Handle associations
305-
entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
306-
public void doWithAssociation(Association<MongoPersistentProperty> association) {
303+
entity.doWithAssociations((AssociationHandler<MongoPersistentProperty>) association -> {
307304

308-
final MongoPersistentProperty property = association.getInverse();
309-
Object value = documentAccessor.get(property);
305+
final MongoPersistentProperty property = association.getInverse();
306+
Object value = documentAccessor.get(property);
310307

311-
if (value == null || entity.isConstructorArgument(property)) {
312-
return;
313-
}
308+
if (value == null || entity.isConstructorArgument(property)) {
309+
return;
310+
}
314311

315-
DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
312+
DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
316313

317-
DbRefProxyHandler handler = new DefaultDbRefProxyHandler(spELContext, mappingContext,
318-
MappingMongoConverter.this);
319-
DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator,
320-
MappingMongoConverter.this);
314+
DbRefProxyHandler handler = new DefaultDbRefProxyHandler(spELContext, mappingContext,
315+
MappingMongoConverter.this);
316+
DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator,
317+
MappingMongoConverter.this);
321318

322-
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler));
323-
}
319+
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler));
324320
});
325321

326322
return result;
@@ -430,31 +426,26 @@ protected void writeInternal(Object obj, final Bson bson, MongoPersistentEntity<
430426
prop -> dbObjectAccessor.computeIfAbsent(prop, () -> idMapper.convertId(accessor.getProperty(prop))));
431427

432428
// Write the properties
433-
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
434-
public void doWithPersistentProperty(MongoPersistentProperty prop) {
429+
entity.doWithProperties((PropertyHandler<MongoPersistentProperty>) prop -> {
435430

436-
if (idProperty.map(it -> it.equals(prop)).orElse(false) || !prop.isWritable()) {
437-
return;
438-
}
431+
if (idProperty.map(it -> it.equals(prop)).orElse(false) || !prop.isWritable()) {
432+
return;
433+
}
439434

440-
accessor.getProperty(prop).ifPresent(it -> {
441-
if (!conversions.isSimpleType(it.getClass())) {
435+
accessor.getProperty(prop).ifPresent(it -> {
436+
if (!conversions.isSimpleType(it.getClass())) {
442437

443-
writePropertyInternal(it, bson, prop);
444-
} else {
445-
writeSimpleInternal(it, bson, prop);
446-
}
447-
});
448-
}
438+
writePropertyInternal(it, bson, prop);
439+
} else {
440+
writeSimpleInternal(it, bson, prop);
441+
}
442+
});
449443
});
450444

451-
entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
445+
entity.doWithAssociations((AssociationHandler<MongoPersistentProperty>) association -> {
452446

453-
public void doWithAssociation(Association<MongoPersistentProperty> association) {
454-
455-
MongoPersistentProperty inverseProp = association.getInverse();
456-
accessor.getProperty(inverseProp).ifPresent(it -> writePropertyInternal(it, bson, inverseProp));
457-
}
447+
MongoPersistentProperty inverseProp = association.getInverse();
448+
accessor.getProperty(inverseProp).ifPresent(it -> writePropertyInternal(it, bson, inverseProp));
458449
});
459450
}
460451

@@ -562,7 +553,7 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
562553
return writeCollectionInternal(collection, Optional.of(property.getTypeInformation()), new BasicDBList());
563554
}
564555

565-
List<Object> dbList = new ArrayList<Object>();
556+
List<Object> dbList = new ArrayList<>(collection.size());
566557

567558
for (Object element : collection) {
568559

@@ -624,7 +615,7 @@ protected Bson createMap(Map<Object, Object> map, MongoPersistentProperty proper
624615
private BasicDBList writeCollectionInternal(Collection<?> source, Optional<TypeInformation<?>> type,
625616
BasicDBList sink) {
626617

627-
Optional<TypeInformation<?>> componentType = type.flatMap(it -> it.getComponentType());
618+
Optional<TypeInformation<?>> componentType = type.flatMap(TypeInformation::getComponentType);
628619

629620
for (Object element : source) {
630621

@@ -758,7 +749,7 @@ protected String potentiallyUnescapeMapKey(String source) {
758749
*/
759750
protected void addCustomTypeKeyIfNecessary(Optional<TypeInformation<?>> type, Object value, Bson bson) {
760751

761-
Optional<Class<?>> actualType = type.map(it -> it.getActualType()).map(it -> it.getType());
752+
Optional<Class<?>> actualType = type.map(TypeInformation::getActualType).map(TypeInformation::getType);
762753
Class<?> reference = actualType.orElse(Object.class);
763754
Class<?> valueType = ClassUtils.getUserClass(value.getClass());
764755

@@ -904,7 +895,7 @@ private Object readCollectionOrArray(TypeInformation<?> targetType, List sourceV
904895
Class<?> rawComponentType = componentType.getType();
905896

906897
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
907-
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>()
898+
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<>(sourceValue.size())
908899
: CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size());
909900

910901
if (sourceValue.isEmpty()) {
@@ -964,8 +955,8 @@ protected Map<Object, Object> readMap(TypeInformation<?> type, Bson bson, Object
964955
Class<?> mapType = typeMapper.readType(bson, type).getType();
965956

966957
Optional<TypeInformation<?>> valueType = type.getMapValueType();
967-
Class<?> rawKeyType = type.getComponentType().map(it -> it.getType()).orElse(null);
968-
Class<?> rawValueType = type.getMapValueType().map(it -> it.getType()).orElse(null);
958+
Class<?> rawKeyType = type.getComponentType().map(TypeInformation::getType).orElse(null);
959+
Class<?> rawValueType = type.getMapValueType().map(TypeInformation::getType).orElse(null);
969960

970961
Map<String, Object> sourceMap = asMap(bson);
971962
Map<Object, Object> map = CollectionFactory.createMap(mapType, rawKeyType, sourceMap.keySet().size());
@@ -1008,7 +999,7 @@ protected Map<Object, Object> readMap(TypeInformation<?> type, Bson bson, Object
1008999
}
10091000

10101001
@SuppressWarnings("unchecked")
1011-
private Map<String, Object> asMap(Bson bson) {
1002+
private static Map<String, Object> asMap(Bson bson) {
10121003

10131004
if (bson instanceof Document) {
10141005
return (Document) bson;
@@ -1022,7 +1013,7 @@ private Map<String, Object> asMap(Bson bson) {
10221013
String.format("Cannot read %s. as map. Given Bson must be a Document or DBObject!", bson.getClass()));
10231014
}
10241015

1025-
private void addToMap(Bson bson, String key, Object value) {
1016+
private static void addToMap(Bson bson, String key, Object value) {
10261017

10271018
if (bson instanceof Document) {
10281019
((Document) bson).put(key, value);
@@ -1037,7 +1028,7 @@ private void addToMap(Bson bson, String key, Object value) {
10371028
}
10381029

10391030
@SuppressWarnings("unchecked")
1040-
private void addAllToMap(Bson bson, Map value) {
1031+
private static void addAllToMap(Bson bson, Map value) {
10411032

10421033
if (bson instanceof Document) {
10431034
((Document) bson).putAll(value);
@@ -1053,7 +1044,7 @@ private void addAllToMap(Bson bson, Map value) {
10531044
String.format("Cannot add all to %s. Given Bson must be a Document or DBObject.", bson.getClass()));
10541045
}
10551046

1056-
private void removeFromMap(Bson bson, String key) {
1047+
private static void removeFromMap(Bson bson, String key) {
10571048

10581049
if (bson instanceof Document) {
10591050
((Document) bson).remove(key);
@@ -1120,7 +1111,7 @@ public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation)
11201111

11211112
if (obj instanceof Map) {
11221113

1123-
Map<Object, Object> converted = new LinkedHashMap<Object, Object>();
1114+
Map<Object, Object> converted = new LinkedHashMap<>(((Map)obj).size(), 1);
11241115
Document result = new Document();
11251116

11261117
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) obj).entrySet()) {
@@ -1254,7 +1245,7 @@ public <T> Optional<T> getPropertyValue(MongoPersistentProperty property) {
12541245
return Optional
12551246

12561247
.ofNullable(property.getSpelExpression()//
1257-
.map(it -> evaluator.evaluate(it))//
1248+
.map(evaluator::evaluate)//
12581249
.orElseGet(() -> source.get(property)))//
12591250
.map(it -> readValue(it, property.getTypeInformation(), path));
12601251
}
@@ -1337,7 +1328,7 @@ private <T> T readAndConvertDBRef(DBRef dbref, TypeInformation<?> type, ObjectPa
13371328
private void bulkReadAndConvertDBRefMapIntoTarget(TypeInformation<?> valueType, Class<?> rawValueType,
13381329
Map<String, Object> sourceMap, Map<Object, Object> targetMap) {
13391330

1340-
LinkedHashMap<String, Object> referenceMap = new LinkedHashMap<String, Object>(sourceMap);
1331+
LinkedHashMap<String, Object> referenceMap = new LinkedHashMap<>(sourceMap);
13411332
List<Object> convertedObjects = bulkReadAndConvertDBRefs((List<DBRef>) new ArrayList(referenceMap.values()),
13421333
valueType, ObjectPath.ROOT, rawValueType);
13431334
int index = 0;
@@ -1360,19 +1351,19 @@ private <T> List<T> bulkReadAndConvertDBRefs(List<DBRef> dbrefs, TypeInformation
13601351
? Collections.singletonList(readRef(dbrefs.iterator().next())) : bulkReadRefs(dbrefs);
13611352
String collectionName = dbrefs.iterator().next().getCollectionName();
13621353

1363-
List<T> targeList = new ArrayList<T>(dbrefs.size());
1354+
List<T> targeList = new ArrayList<>(dbrefs.size());
13641355

13651356
for (Document document : referencedRawDocuments) {
13661357

13671358
if (document != null) {
1368-
maybeEmitEvent(new AfterLoadEvent<T>(document, (Class<T>) rawType, collectionName));
1359+
maybeEmitEvent(new AfterLoadEvent<>(document, (Class<T>) rawType, collectionName));
13691360
}
13701361

13711362
final T target = (T) read(type, document, path);
13721363
targeList.add(target);
13731364

13741365
if (target != null) {
1375-
maybeEmitEvent(new AfterConvertEvent<T>(document, target, collectionName));
1366+
maybeEmitEvent(new AfterConvertEvent<>(document, target, collectionName));
13761367
}
13771368
}
13781369

@@ -1421,7 +1412,7 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<Obje
14211412

14221413
Assert.notNull(source, "Iterable of DBRefs must not be null!");
14231414

1424-
Set<String> collectionsFound = new HashSet<String>();
1415+
Set<String> collectionsFound = new HashSet<>();
14251416

14261417
for (Object dbObjItem : source) {
14271418

0 commit comments

Comments
 (0)