提交 cb548697 编写于 作者: K Keith Donald

collection to object

上级 67c02f6c
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import java.lang.reflect.Array;
import org.springframework.core.convert.TypeDescriptor;
class ArrayToObjectGenericConverter implements GenericConverter {
......@@ -26,7 +28,18 @@ class ArrayToObjectGenericConverter implements GenericConverter {
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
throw new UnsupportedOperationException("Not yet implemented");
int length = Array.getLength(source);
if (length == 0) {
return null;
} else {
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
if (sourceElementType.isAssignableTo(targetType)) {
return Array.get(source, 0);
} else {
GenericConverter converter = conversionService.getConverter(sourceElementType, targetType);
return converter.convert(Array.get(source, 0), sourceElementType, targetType);
}
}
}
}
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import java.util.Collection;
import org.springframework.core.convert.TypeDescriptor;
class CollectionToObjectGenericConverter implements GenericConverter {
......@@ -24,9 +26,19 @@ class CollectionToObjectGenericConverter implements GenericConverter {
public CollectionToObjectGenericConverter(GenericConversionService conversionService) {
this.conversionService = conversionService;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
throw new UnsupportedOperationException("Not yet implemented");
Collection sourceCollection = (Collection) source;
if (sourceCollection.size() == 0) {
return null;
} else {
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetType)) {
return sourceCollection.iterator().next();
} else {
GenericConverter converter = conversionService.getConverter(sourceElementType, targetType);
return converter.convert(sourceCollection.iterator().next(), sourceElementType, targetType);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册