Skip to content

Commit e5d5515

Browse files
author
Keith Donald
committed
fixed failing test; initial conditional converter impls for collections, arrays, and maps
1 parent a60cb43 commit e5d5515

12 files changed

+70
-26
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ public boolean isAssignableTo(TypeDescriptor typeDescriptor) {
293293
if (!typesAssignable) {
294294
return false;
295295
}
296-
if (isArray()) {
296+
if (isArray() && typeDescriptor.isArray()) {
297297
return getElementTypeDescriptor().isAssignableTo(typeDescriptor.getElementTypeDescriptor());
298-
} else if (isCollection()) {
298+
} else if (isCollection() && typeDescriptor.isCollection()) {
299299
return isNestedAssignable(getElementTypeDescriptor(), typeDescriptor.getElementTypeDescriptor());
300-
} else if (isMap()) {
300+
} else if (isMap() && typeDescriptor.isMap()) {
301301
return isNestedAssignable(getMapKeyTypeDescriptor(), typeDescriptor.getMapKeyTypeDescriptor()) &&
302302
isNestedAssignable(getMapValueTypeDescriptor(), typeDescriptor.getMapValueTypeDescriptor());
303303
} else {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.springframework.core.convert.ConversionService;
2424
import org.springframework.core.convert.TypeDescriptor;
25-
import org.springframework.core.convert.converter.GenericConverter;
25+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2626
import org.springframework.util.ObjectUtils;
2727

2828
/**
@@ -32,7 +32,7 @@
3232
* @author Keith Donald
3333
* @since 3.0
3434
*/
35-
final class ArrayToArrayConverter implements GenericConverter {
35+
final class ArrayToArrayConverter implements ConditionalGenericConverter {
3636

3737
private final CollectionToArrayConverter helperConverter;
3838

@@ -44,6 +44,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4444
return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class));
4545
}
4646

47+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
48+
return true;
49+
}
50+
4751
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
4852
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
4953
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.core.CollectionFactory;
2525
import org.springframework.core.convert.ConversionService;
2626
import org.springframework.core.convert.TypeDescriptor;
27-
import org.springframework.core.convert.converter.GenericConverter;
27+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2828

2929
/**
3030
* Converts an Array to a Collection.
@@ -36,7 +36,7 @@
3636
* @author Keith Donald
3737
* @since 3.0
3838
*/
39-
final class ArrayToCollectionConverter implements GenericConverter {
39+
final class ArrayToCollectionConverter implements ConditionalGenericConverter {
4040

4141
private final ConversionService conversionService;
4242

@@ -48,6 +48,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4848
return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class));
4949
}
5050

51+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
52+
return true;
53+
}
54+
5155
@SuppressWarnings("unchecked")
5256
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
5357
if (source == null) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.springframework.core.convert.ConversionService;
2424
import org.springframework.core.convert.TypeDescriptor;
25-
import org.springframework.core.convert.converter.GenericConverter;
25+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2626
import org.springframework.util.ObjectUtils;
2727

2828
/**
@@ -32,7 +32,7 @@
3232
* @author Keith Donald
3333
* @since 3.0
3434
*/
35-
final class ArrayToObjectConverter implements GenericConverter {
35+
final class ArrayToObjectConverter implements ConditionalGenericConverter {
3636

3737
private final CollectionToObjectConverter helperConverter;
3838

@@ -43,6 +43,10 @@ public ArrayToObjectConverter(ConversionService conversionService) {
4343
public Set<ConvertiblePair> getConvertibleTypes() {
4444
return Collections.singleton(new ConvertiblePair(Object[].class, Object.class));
4545
}
46+
47+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
48+
return true;
49+
}
4650

4751
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
4852
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.springframework.core.convert.ConversionService;
2424
import org.springframework.core.convert.TypeDescriptor;
25-
import org.springframework.core.convert.converter.GenericConverter;
25+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2626
import org.springframework.util.ObjectUtils;
2727

2828
/**
@@ -32,7 +32,7 @@
3232
* @author Keith Donald
3333
* @since 3.0
3434
*/
35-
final class ArrayToStringConverter implements GenericConverter {
35+
final class ArrayToStringConverter implements ConditionalGenericConverter {
3636

3737
private final CollectionToStringConverter helperConverter;
3838

@@ -43,7 +43,11 @@ public ArrayToStringConverter(ConversionService conversionService) {
4343
public Set<ConvertiblePair> getConvertibleTypes() {
4444
return Collections.singleton(new ConvertiblePair(Object[].class, String.class));
4545
}
46-
46+
47+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
48+
return true;
49+
}
50+
4751
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
4852
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
4953
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import org.springframework.core.convert.ConversionService;
2525
import org.springframework.core.convert.TypeDescriptor;
26-
import org.springframework.core.convert.converter.GenericConverter;
26+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2727

2828
/**
2929
* Converts a Collection to an array.
@@ -36,7 +36,7 @@
3636
* @author Keith Donald
3737
* @since 3.0
3838
*/
39-
final class CollectionToArrayConverter implements GenericConverter {
39+
final class CollectionToArrayConverter implements ConditionalGenericConverter {
4040

4141
private final ConversionService conversionService;
4242

@@ -48,6 +48,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4848
return Collections.singleton(new ConvertiblePair(Collection.class, Object[].class));
4949
}
5050

51+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
52+
return true;
53+
}
54+
5155
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
5256
if (source == null) {
5357
return null;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.core.CollectionFactory;
2424
import org.springframework.core.convert.ConversionService;
2525
import org.springframework.core.convert.TypeDescriptor;
26-
import org.springframework.core.convert.converter.GenericConverter;
26+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2727

2828
/**
2929
* Converts from a Collection to another Collection.
@@ -36,7 +36,7 @@
3636
* @author Keith Donald
3737
* @since 3.0
3838
*/
39-
final class CollectionToCollectionConverter implements GenericConverter {
39+
final class CollectionToCollectionConverter implements ConditionalGenericConverter {
4040

4141
private final ConversionService conversionService;
4242

@@ -48,6 +48,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4848
return Collections.singleton(new ConvertiblePair(Collection.class, Collection.class));
4949
}
5050

51+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
52+
return true;
53+
}
54+
5155
@SuppressWarnings("unchecked")
5256
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
5357
if (source == null) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222

2323
import org.springframework.core.convert.ConversionService;
2424
import org.springframework.core.convert.TypeDescriptor;
25-
import org.springframework.core.convert.converter.GenericConverter;
25+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2626

2727
/**
2828
* Converts a Collection to an Object by returning the first collection element after converting it to the desired targetType.
2929
*
3030
* @author Keith Donald
3131
* @since 3.0
3232
*/
33-
final class CollectionToObjectConverter implements GenericConverter {
33+
final class CollectionToObjectConverter implements ConditionalGenericConverter {
3434

3535
private final ConversionService conversionService;
3636

@@ -42,6 +42,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4242
return Collections.singleton(new ConvertiblePair(Collection.class, Object.class));
4343
}
4444

45+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
46+
return true;
47+
}
48+
4549
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
4650
if (source == null) {
4751
return null;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222

2323
import org.springframework.core.convert.ConversionService;
2424
import org.springframework.core.convert.TypeDescriptor;
25-
import org.springframework.core.convert.converter.GenericConverter;
25+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2626

2727
/**
2828
* Converts a Collection to a comma-delimited String.
2929
*
3030
* @author Keith Donald
3131
* @since 3.0
3232
*/
33-
final class CollectionToStringConverter implements GenericConverter {
33+
final class CollectionToStringConverter implements ConditionalGenericConverter {
3434

3535
private static final String DELIMITER = ",";
3636

@@ -44,6 +44,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4444
return Collections.singleton(new ConvertiblePair(Collection.class, String.class));
4545
}
4646

47+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
48+
return true;
49+
}
50+
4751
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
4852
if (source == null) {
4953
return null;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.core.CollectionFactory;
2424
import org.springframework.core.convert.ConversionService;
2525
import org.springframework.core.convert.TypeDescriptor;
26-
import org.springframework.core.convert.converter.GenericConverter;
26+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2727

2828
/**
2929
* Converts a Map to another Map.
@@ -36,7 +36,7 @@
3636
* @author Keith Donald
3737
* @since 3.0
3838
*/
39-
final class MapToMapConverter implements GenericConverter {
39+
final class MapToMapConverter implements ConditionalGenericConverter {
4040

4141
private final ConversionService conversionService;
4242

@@ -48,6 +48,10 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4848
return Collections.singleton(new ConvertiblePair(Map.class, Map.class));
4949
}
5050

51+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
52+
return true;
53+
}
54+
5155
@SuppressWarnings("unchecked")
5256
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
5357
if (source == null) {

0 commit comments

Comments
 (0)