fixed failing test; initial conditional converter impls for collections, arrays, and maps

This commit is contained in:
Keith Donald 2011-06-07 15:56:51 +00:00
parent a60cb43c6a
commit e5d551587a
12 changed files with 70 additions and 26 deletions

View File

@ -293,11 +293,11 @@ public class TypeDescriptor {
if (!typesAssignable) {
return false;
}
if (isArray()) {
if (isArray() && typeDescriptor.isArray()) {
return getElementTypeDescriptor().isAssignableTo(typeDescriptor.getElementTypeDescriptor());
} else if (isCollection()) {
} else if (isCollection() && typeDescriptor.isCollection()) {
return isNestedAssignable(getElementTypeDescriptor(), typeDescriptor.getElementTypeDescriptor());
} else if (isMap()) {
} else if (isMap() && typeDescriptor.isMap()) {
return isNestedAssignable(getMapKeyTypeDescriptor(), typeDescriptor.getMapKeyTypeDescriptor()) &&
isNestedAssignable(getMapValueTypeDescriptor(), typeDescriptor.getMapValueTypeDescriptor());
} else {

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.util.ObjectUtils;
/**
@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToArrayConverter implements GenericConverter {
final class ArrayToArrayConverter implements ConditionalGenericConverter {
private final CollectionToArrayConverter helperConverter;
@ -44,6 +44,10 @@ final class ArrayToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
}

View File

@ -24,7 +24,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts an Array to a Collection.
@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToCollectionConverter implements GenericConverter {
final class ArrayToCollectionConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -48,6 +48,10 @@ final class ArrayToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.util.ObjectUtils;
/**
@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToObjectConverter implements GenericConverter {
final class ArrayToObjectConverter implements ConditionalGenericConverter {
private final CollectionToObjectConverter helperConverter;
@ -44,6 +44,10 @@ final class ArrayToObjectConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, Object.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
}

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.util.ObjectUtils;
/**
@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToStringConverter implements GenericConverter {
final class ArrayToStringConverter implements ConditionalGenericConverter {
private final CollectionToStringConverter helperConverter;
@ -44,6 +44,10 @@ final class ArrayToStringConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, String.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
}

View File

@ -23,7 +23,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts a Collection to an array.
@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToArrayConverter implements GenericConverter {
final class CollectionToArrayConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -48,6 +48,10 @@ final class CollectionToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;

View File

@ -23,7 +23,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a Collection to another Collection.
@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToCollectionConverter implements GenericConverter {
final class CollectionToCollectionConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -48,6 +48,10 @@ final class CollectionToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts a Collection to an Object by returning the first collection element after converting it to the desired targetType.
@ -30,7 +30,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToObjectConverter implements GenericConverter {
final class CollectionToObjectConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -42,6 +42,10 @@ final class CollectionToObjectConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Object.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts a Collection to a comma-delimited String.
@ -30,7 +30,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToStringConverter implements GenericConverter {
final class CollectionToStringConverter implements ConditionalGenericConverter {
private static final String DELIMITER = ",";
@ -44,6 +44,10 @@ final class CollectionToStringConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, String.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;

View File

@ -23,7 +23,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts a Map to another Map.
@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class MapToMapConverter implements GenericConverter {
final class MapToMapConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -48,6 +48,10 @@ final class MapToMapConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Map.class, Map.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts an Object to a single-element Array containing the Object.
@ -31,7 +31,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class ObjectToArrayConverter implements GenericConverter {
final class ObjectToArrayConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -43,6 +43,10 @@ final class ObjectToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object.class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;

View File

@ -23,7 +23,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts an Object to a single-element Collection containing the Object.
@ -33,7 +33,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Juergen Hoeller
* @since 3.0
*/
final class ObjectToCollectionConverter implements GenericConverter {
final class ObjectToCollectionConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
@ -45,6 +45,10 @@ final class ObjectToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object.class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {