Browse Source

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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4481 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/merge
Keith Donald 15 years ago
parent
commit
a87adacbfe
  1. 6
      org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  2. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java
  3. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java
  4. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java
  5. 10
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java
  6. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java
  7. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java
  8. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java
  9. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java
  10. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
  11. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java
  12. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java

6
org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

@ -293,11 +293,11 @@ public class TypeDescriptor { @@ -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 {

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java

@ -22,7 +22,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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);
}

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java

@ -24,7 +24,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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) {

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java

@ -22,7 +22,7 @@ import java.util.Set; @@ -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; @@ -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;
@ -43,6 +43,10 @@ final class ArrayToObjectConverter implements GenericConverter { @@ -43,6 +43,10 @@ final class ArrayToObjectConverter implements GenericConverter {
public Set<ConvertiblePair> getConvertibleTypes() {
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);

10
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java

@ -22,7 +22,7 @@ import java.util.Set; @@ -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; @@ -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;
@ -43,7 +43,11 @@ final class ArrayToStringConverter implements GenericConverter { @@ -43,7 +43,11 @@ final class ArrayToStringConverter implements GenericConverter {
public Set<ConvertiblePair> getConvertibleTypes() {
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);
}

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java

@ -23,7 +23,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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;

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java

@ -23,7 +23,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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) {

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java

@ -22,7 +22,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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;

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java

@ -22,7 +22,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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;

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java

@ -23,7 +23,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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) {

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java

@ -22,7 +22,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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;

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java

@ -23,7 +23,7 @@ import java.util.Set; @@ -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; @@ -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 { @@ -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) {

Loading…
Cancel
Save