SPR-8714 Do not create copy in map-to-map and collection-to-collection conversion if not necessary
This commit is contained in:
@@ -57,6 +57,7 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
boolean isCopyRequired = !targetType.getType().isInstance(source);
|
||||
Collection<?> sourceCollection = (Collection<?>) source;
|
||||
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
|
||||
if (targetType.getElementTypeDescriptor() == null) {
|
||||
@@ -68,9 +69,12 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
|
||||
for (Object sourceElement : sourceCollection) {
|
||||
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor());
|
||||
target.add(targetElement);
|
||||
if (sourceElement != targetElement) {
|
||||
isCopyRequired = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
return isCopyRequired ? target : source;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ final class MapToMapConverter implements ConditionalGenericConverter {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
boolean isCopyRequired = !targetType.getType().isInstance(source);
|
||||
Map<Object, Object> sourceMap = (Map<Object, Object>) source;
|
||||
Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
|
||||
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
|
||||
@@ -65,8 +66,11 @@ final class MapToMapConverter implements ConditionalGenericConverter {
|
||||
Object targetKey = convertKey(sourceKey, sourceType, targetType.getMapKeyTypeDescriptor());
|
||||
Object targetValue = convertValue(sourceValue, sourceType, targetType.getMapValueTypeDescriptor());
|
||||
targetMap.put(targetKey, targetValue);
|
||||
if (sourceKey != targetKey || sourceValue != targetValue) {
|
||||
isCopyRequired = true;
|
||||
}
|
||||
}
|
||||
return targetMap;
|
||||
return isCopyRequired ? targetMap : sourceMap;
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
Reference in New Issue
Block a user