Browse Source

collection to object

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1948 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Keith Donald 17 years ago
parent
commit
0420b17e44
  1. 15
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java
  2. 18
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java

15
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java

@ -15,6 +15,8 @@ @@ -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 { @@ -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);
}
}
}
}

18
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java

@ -15,6 +15,8 @@ @@ -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 { @@ -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);
}
}
}
}

Loading…
Cancel
Save