array to collection conversion
This commit is contained in:
parent
6c4fb85753
commit
2ca694350e
|
|
@ -17,16 +17,12 @@
|
||||||
package org.springframework.core.convert.support;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.springframework.core.CollectionFactory;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
/**
|
|
||||||
* A generic converter that can convert from one array type to another.
|
|
||||||
*
|
|
||||||
* @author Keith Donald
|
|
||||||
* @author Juergen Hoeller
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
|
||||||
class ArrayGenericConverter implements GenericConverter {
|
class ArrayGenericConverter implements GenericConverter {
|
||||||
|
|
||||||
private GenericConversionService conversionService;
|
private GenericConversionService conversionService;
|
||||||
|
|
@ -36,15 +32,33 @@ class ArrayGenericConverter implements GenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
return sourceType.isArray() && targetType.isArray();
|
return sourceType.isArray() || targetType.isArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
if (isArrayToArray(sourceType, targetType)) {
|
||||||
|
return convertArrayToArray(source, sourceType, targetType);
|
||||||
|
} else if (isArrayToCollection(sourceType, targetType)) {
|
||||||
|
return convertArrayToCollection(source, sourceType, targetType);
|
||||||
|
} else if (isCollectionToArray(sourceType, targetType)) {
|
||||||
|
return convertCollectionToArray(source, sourceType, targetType);
|
||||||
|
} else if (isArrayToObject(sourceType, targetType)) {
|
||||||
|
return convertArrayToObject(source, sourceType, targetType);
|
||||||
|
} else {
|
||||||
|
return convertObjectToArray(source, sourceType, targetType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isArrayToArray(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
return sourceType.isArray() && targetType.isArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object convertArrayToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
if (sourceType.isAssignableTo(targetType)) {
|
if (sourceType.isAssignableTo(targetType)) {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
|
||||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
||||||
Object target = Array.newInstance(targetElementType.getType(), Array.getLength(source));
|
Object target = Array.newInstance(targetElementType.getType(), Array.getLength(source));
|
||||||
GenericConverter converter = conversionService.getConverter(sourceElementType, targetElementType);
|
GenericConverter converter = conversionService.getConverter(sourceElementType, targetElementType);
|
||||||
for (int i = 0; i < Array.getLength(target); i++) {
|
for (int i = 0; i < Array.getLength(target); i++) {
|
||||||
|
|
@ -53,4 +67,46 @@ class ArrayGenericConverter implements GenericConverter {
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isArrayToCollection(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
return sourceType.isArray() && targetType.isCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object convertArrayToCollection(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
int length = Array.getLength(source);
|
||||||
|
Collection collection = CollectionFactory.createCollection(targetType.getType(), length);
|
||||||
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
||||||
|
if (targetElementType == TypeDescriptor.NULL) {
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
collection.add(Array.get(source, i));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
|
GenericConverter converter = conversionService.getConverter(sourceElementType, targetElementType);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
collection.add(converter.convert(Array.get(source, i), sourceElementType, targetElementType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCollectionToArray(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
return sourceType.isCollection() && targetType.isArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object convertCollectionToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isArrayToObject(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
return sourceType.isArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object convertArrayToObject(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object convertObjectToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -153,7 +153,6 @@ public class GenericConversionServiceTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void convertArrayToListInterface() {
|
public void convertArrayToListInterface() {
|
||||||
List<?> result = converter.convert(new String[] { "1", "2", "3" }, List.class);
|
List<?> result = converter.convert(new String[] { "1", "2", "3" }, List.class);
|
||||||
assertEquals("1", result.get(0));
|
assertEquals("1", result.get(0));
|
||||||
|
|
@ -164,7 +163,6 @@ public class GenericConversionServiceTests {
|
||||||
public List<Integer> genericList = new ArrayList<Integer>();
|
public List<Integer> genericList = new ArrayList<Integer>();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void convertArrayToListGenericTypeConversion() throws Exception {
|
public void convertArrayToListGenericTypeConversion() throws Exception {
|
||||||
converter.addConverterFactory(new StringToNumberConverterFactory());
|
converter.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
List<Integer> result = (List<Integer>) converter.convert(new String[] { "1", "2", "3" }, TypeDescriptor
|
List<Integer> result = (List<Integer>) converter.convert(new String[] { "1", "2", "3" }, TypeDescriptor
|
||||||
|
|
@ -175,7 +173,6 @@ public class GenericConversionServiceTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void convertArrayToListImpl() {
|
public void convertArrayToListImpl() {
|
||||||
LinkedList<?> result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class);
|
LinkedList<?> result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class);
|
||||||
assertEquals("1", result.get(0));
|
assertEquals("1", result.get(0));
|
||||||
|
|
@ -183,14 +180,9 @@ public class GenericConversionServiceTests {
|
||||||
assertEquals("3", result.get(2));
|
assertEquals("3", result.get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected=ConversionFailedException.class)
|
||||||
@Ignore
|
|
||||||
public void convertArrayToAbstractList() {
|
public void convertArrayToAbstractList() {
|
||||||
try {
|
converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
||||||
converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue