This commit is contained in:
Keith Donald
2011-05-23 07:38:27 +00:00
parent 5c67dbf424
commit 7430fcd904
8 changed files with 168 additions and 56 deletions

View File

@@ -59,11 +59,20 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
}
int length = Array.getLength(source);
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), length);
for (int i = 0; i < length; i++) {
Object sourceElement = Array.get(source, i);
Object targetElement = this.conversionService.convert(sourceElement, sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
target.add(targetElement);
}
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
if (Object.class.equals(targetElementType.getType())) {
for (int i = 0; i < length; i++) {
Object sourceElement = Array.get(source, i);
target.add(sourceElement);
}
} else {
for (int i = 0; i < length; i++) {
Object sourceElement = Array.get(source, i);
Object targetElement = this.conversionService.convert(sourceElement, sourceElementType, targetElementType);
target.add(targetElement);
}
}
return target;
}

View File

@@ -61,9 +61,17 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
}
Collection<?> sourceCollection = (Collection<?>) source;
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
for (Object sourceElement : sourceCollection) {
Object targetElement = this.conversionService.convert(sourceElement, sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
target.add(targetElement);
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
if (Object.class.equals(targetElementType.getType())) {
for (Object sourceElement : sourceCollection) {
target.add(sourceElement);
}
} else {
for (Object sourceElement : sourceCollection) {
Object targetElement = this.conversionService.convert(sourceElement, sourceElementType, targetElementType);
target.add(targetElement);
}
}
return target;
}

View File

@@ -60,7 +60,9 @@ final class MapToMapConverter implements ConditionalGenericConverter {
}
Map<Object, Object> sourceMap = (Map<Object, Object>) source;
Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor();
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
TypeDescriptor targetValueType = targetType.getMapValueTypeDescriptor();
if (Object.class.equals(targetKeyType.getType()) && Object.class.equals(targetValueType.getType())) {
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
@@ -70,8 +72,8 @@ final class MapToMapConverter implements ConditionalGenericConverter {
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
Object sourceKey = entry.getKey();
Object sourceValue = entry.getValue();
Object targetKey = this.conversionService.convert(sourceKey, sourceType.getMapKeyTypeDescriptor(), targetType.getMapKeyTypeDescriptor());
Object targetValue = this.conversionService.convert(sourceValue, sourceType.getMapValueTypeDescriptor(), targetType.getMapValueTypeDescriptor());
Object targetKey = this.conversionService.convert(sourceKey, sourceKeyType, targetKeyType);
Object targetValue = this.conversionService.convert(sourceValue, sourceValueType, targetValueType);
targetMap.put(targetKey, targetValue);
}
}

View File

@@ -55,13 +55,12 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter {
return null;
}
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), 1);
TypeDescriptor elementType = targetType.getElementTypeDescriptor();
// Avoid potential recursion...
if (!Collection.class.isAssignableFrom(elementType.getType())) {
target.add(this.conversionService.convert(source, sourceType, elementType));
}
else {
target.add(source);
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
// Avoid potential recursion....
if (targetElementType.isCollection()) {
target.add(source);
} else {
target.add(this.conversionService.convert(source, sourceType, targetElementType));
}
return target;
}