From 1c6965132f05ab9d127157d637728bddce5d0b39 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Wed, 16 Sep 2009 19:24:56 +0000 Subject: [PATCH] convert polishing: removed checked exception from Converter interface to support direct use, added Converter to each implementation for consistency --- .../support/AbstractCollectionConverter.java | 2 - .../core/convert/support/ArrayToArray.java | 1 - .../convert/support/ArrayToCollection.java | 1 - .../support/CharacterToNumberFactory.java | 3 +- .../support/DefaultConversionService.java | 25 +++----- .../support/GenericConversionService.java | 22 +++---- .../convert/support/MapEntryConverter.java | 3 - .../core/convert/support/MapToMap.java | 2 - .../convert/support/MapToStringArray.java | 2 - .../support/MapToStringCollection.java | 2 - .../support/NoOpConversionExecutor.java | 1 - ...r.java => NumberToCharacterConverter.java} | 4 +- ...va => NumberToNumberConverterFactory.java} | 5 +- .../core/convert/support/ObjectToArray.java | 3 - .../convert/support/ObjectToCollection.java | 3 - ...ring.java => ObjectToStringConverter.java} | 2 +- .../support/StaticConversionExecutor.java | 3 - .../convert/support/StringArrayToMap.java | 3 - .../convert/support/StringArrayToObject.java | 2 - .../core/convert/support/StringToArray.java | 3 - .../convert/support/StringToBigDecimal.java | 36 ----------- .../convert/support/StringToBigInteger.java | 36 ----------- ...ean.java => StringToBooleanConverter.java} | 2 +- .../core/convert/support/StringToByte.java | 34 ----------- ...r.java => StringToCharacterConverter.java} | 6 +- .../convert/support/StringToCollection.java | 2 - .../core/convert/support/StringToDouble.java | 34 ----------- ...java => StringToEnumConverterFactory.java} | 5 +- .../core/convert/support/StringToFloat.java | 34 ----------- .../core/convert/support/StringToInteger.java | 34 ----------- ...cale.java => StringToLocaleConverter.java} | 2 +- .../core/convert/support/StringToLong.java | 34 ----------- .../core/convert/support/StringToMap.java | 1 - .../StringToNumberConverterFactory.java | 60 +++++++++++++++++++ .../core/convert/support/StringToShort.java | 34 ----------- 35 files changed, 90 insertions(+), 356 deletions(-) rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{NumberToCharacter.java => NumberToCharacterConverter.java} (89%) rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{NumberToNumberFactory.java => NumberToNumberConverterFactory.java} (93%) rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{ObjectToString.java => ObjectToStringConverter.java} (93%) delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigDecimal.java delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigInteger.java rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{StringToBoolean.java => StringToBooleanConverter.java} (94%) delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToByte.java rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{StringToCharacter.java => StringToCharacterConverter.java} (79%) delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToDouble.java rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{StringToEnumFactory.java => StringToEnumConverterFactory.java} (93%) delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToFloat.java delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToInteger.java rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{StringToLocale.java => StringToLocaleConverter.java} (93%) delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLong.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java delete mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToShort.java diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java index edcae3a4cf8..f17beebde08 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java @@ -35,7 +35,6 @@ abstract class AbstractCollectionConverter implements ConversionExecutor { private final ConversionExecutor elementConverter; - public AbstractCollectionConverter(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, GenericConversionService conversionService) { this.conversionService = conversionService; this.sourceCollectionType = sourceCollectionType; @@ -56,7 +55,6 @@ abstract class AbstractCollectionConverter implements ConversionExecutor { } } - /** * The collection type to convert to. */ diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java index e27ce2e3a07..08f210dea29 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java @@ -29,7 +29,6 @@ import org.springframework.core.convert.TypeDescriptor; * @author Juergen Hoeller * @since 3.0 */ -@SuppressWarnings("unchecked") class ArrayToArray extends AbstractCollectionConverter { public ArrayToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetArrayType, GenericConversionService conversionService) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java index a78fd4c44fb..970856f85c5 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java @@ -39,7 +39,6 @@ class ArrayToCollection extends AbstractCollectionConverter { } @Override - @SuppressWarnings("unchecked") protected Object doExecute(Object sourceArray) throws Exception { int length = Array.getLength(sourceArray); Collection collection = CollectionFactory.createCollection(getTargetCollectionType(), length); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CharacterToNumberFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CharacterToNumberFactory.java index 33e622d0573..1fa3eb6cbd9 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CharacterToNumberFactory.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CharacterToNumberFactory.java @@ -44,7 +44,6 @@ class CharacterToNumberFactory implements ConverterFactory { return new CharacterToNumber(targetType); } - private static class CharacterToNumber implements Converter { private final Class targetType; @@ -53,7 +52,7 @@ class CharacterToNumberFactory implements ConverterFactory { this.targetType = targetType; } - public T convert(Character source) throws Exception { + public T convert(Character source) { return NumberUtils.convertNumberToTargetClass((short) source.charValue(), targetType); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java index 5eb60fc9eb6..b4c999390a5 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java @@ -30,22 +30,15 @@ public class DefaultConversionService extends GenericConversionService { * Create a new default conversion service, installing the default converters. */ public DefaultConversionService() { - addConverter(new StringToByte()); - addConverter(new StringToBoolean()); - addConverter(new StringToCharacter()); - addConverter(new StringToShort()); - addConverter(new StringToInteger()); - addConverter(new StringToLong()); - addConverter(new StringToFloat()); - addConverter(new StringToDouble()); - addConverter(new StringToBigInteger()); - addConverter(new StringToBigDecimal()); - addConverter(new StringToLocale()); - addConverter(new NumberToCharacter()); - addConverter(new ObjectToString()); - addConverter(new StringToEnumFactory()); - addConverter(new NumberToNumberFactory()); - addConverter(new CharacterToNumberFactory()); + addConverter(new StringToBooleanConverter()); + addConverter(new StringToCharacterConverter()); + addConverter(new StringToLocaleConverter()); + addConverter(new NumberToCharacterConverter()); + addConverter(new ObjectToStringConverter()); + addConverterFactory(new StringToNumberConverterFactory()); + addConverterFactory(new StringToEnumConverterFactory()); + addConverterFactory(new NumberToNumberConverterFactory()); + addConverterFactory(new CharacterToNumberFactory()); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index d7e264a43df..3faef9b40ee 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -46,7 +46,7 @@ import org.springframework.util.ClassUtils; * @author Juergen Hoeller * @since 3.0 * @see #addConverter(Converter) - * @see #addConverter(ConverterFactory) + * @see #addConverterFactory(ConverterFactory) */ public class GenericConversionService implements ConversionService, ConverterRegistry { @@ -58,7 +58,6 @@ public class GenericConversionService implements ConversionService, ConverterReg */ private final Map> sourceTypeConverters = new HashMap>(); - /** * Registers the converters in the set provided. * JavaBean-friendly alternative to calling {@link #addConverter(Converter)}. @@ -71,13 +70,13 @@ public class GenericConversionService implements ConversionService, ConverterReg } /** - * Registers the converters in the set provided. - * JavaBean-friendly alternative to calling {@link #addConverter(ConverterFactory)}. - * @see #addConverter(ConverterFactory) + * Registers the converter factories in the set provided. + * JavaBean-friendly alternative to calling {@link #addConverterFactory(ConverterFactory)}. + * @see #addConverterFactory(ConverterFactory) */ public void setConverterFactories(Set converters) { for (ConverterFactory converterFactory : converters) { - addConverter(converterFactory); + addConverterFactory(converterFactory); } } @@ -95,35 +94,33 @@ public class GenericConversionService implements ConversionService, ConverterReg return this.parent; } - // implementing ConverterRegistry public void addConverter(Converter converter) { List typeInfo = getRequiredTypeInfo(converter); if (typeInfo == null) { - throw new IllegalArgumentException("Unable to the determine sourceType and targetType your Converter converts between"); + throw new IllegalArgumentException("Unable to the determine sourceType and targetType your Converter converts between; declare these types or implement ConverterInfo"); } Class sourceType = typeInfo.get(0); Class targetType = typeInfo.get(1); getSourceMap(sourceType).put(targetType, converter); } - public void addConverter(ConverterFactory converterFactory) { + public void addConverterFactory(ConverterFactory converterFactory) { List typeInfo = getRequiredTypeInfo(converterFactory); if (typeInfo == null) { - throw new IllegalArgumentException("Unable to the determine sourceType and targetType your ConverterFactory creates Converters to convert between"); + throw new IllegalArgumentException("Unable to the determine sourceType and targetType your ConverterFactory creates Converters to convert between; declare these types or implement ConverterInfo"); } Class sourceType = typeInfo.get(0); Class targetType = typeInfo.get(1); getSourceMap(sourceType).put(targetType, converterFactory); } - public void removeConverter(Class sourceType, Class targetType) { + public void removeConvertible(Class sourceType, Class targetType) { Map sourceMap = getSourceMap(sourceType); sourceMap.remove(targetType); } - // implementing ConversionService public boolean canConvert(Class sourceType, Class targetType) { @@ -459,7 +456,6 @@ public class GenericConversionService implements ConversionService, ConverterReg } } - @SuppressWarnings("unchecked") private Converter getConverter(Map converters, Class currentClass, Class targetType) { Object converter = converters.get(currentClass); if (converter == null) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapEntryConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapEntryConverter.java index 7430e6938a7..02eb8ff06d6 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapEntryConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapEntryConverter.java @@ -26,18 +26,15 @@ class MapEntryConverter { public static final MapEntryConverter NO_OP_INSTANCE = new MapEntryConverter(null, null); - private final ConversionExecutor keyConverter; private final ConversionExecutor valueConverter; - public MapEntryConverter(ConversionExecutor keyConverter, ConversionExecutor valueConverter) { this.keyConverter = keyConverter; this.valueConverter = valueConverter; } - public Object convertKey(Object key) { if (this.keyConverter != null) { return this.keyConverter.execute(key); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java index 8381b09bfab..0c57b9a2ede 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java @@ -39,7 +39,6 @@ class MapToMap implements ConversionExecutor { private final MapEntryConverter entryConverter; - /** * Creates a new map-to-map converter * @param sourceType the source map type @@ -53,7 +52,6 @@ class MapToMap implements ConversionExecutor { this.entryConverter = createEntryConverter(); } - private MapEntryConverter createEntryConverter() { if (this.sourceType.isMapEntryTypeKnown() && this.targetType.isMapEntryTypeKnown()) { ConversionExecutor keyConverter = this.conversionService.getConversionExecutor( diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringArray.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringArray.java index 427b11a4331..4da98aab404 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringArray.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringArray.java @@ -37,14 +37,12 @@ class MapToStringArray implements ConversionExecutor { private final MapEntryConverter entryConverter; - public MapToStringArray(TypeDescriptor targetType, GenericConversionService conversionService) { this.targetType = targetType; this.conversionService = conversionService; this.entryConverter = createEntryConverter(); } - private MapEntryConverter createEntryConverter() { if (this.targetType.isMapEntryTypeKnown()) { ConversionExecutor keyConverter = this.conversionService.getConversionExecutor(this.targetType.getMapKeyType(), diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringCollection.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringCollection.java index 3409c797bcb..9cc5fd381a2 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringCollection.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToStringCollection.java @@ -34,13 +34,11 @@ class MapToStringCollection implements ConversionExecutor { private final ArrayToCollection collectionConverter; - public MapToStringCollection(TypeDescriptor targetType, GenericConversionService conversionService) { this.converter = new MapToStringArray(targetType, conversionService); this.collectionConverter = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), targetType, conversionService); } - public Object execute(Object source) throws ConversionFailedException { Map map = (Map) source; Object array = this.converter.execute(map); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java index 8a64f2ba023..f4a2b3a6093 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java @@ -28,7 +28,6 @@ class NoOpConversionExecutor implements ConversionExecutor { public static final ConversionExecutor INSTANCE = new NoOpConversionExecutor(); - private NoOpConversionExecutor() { } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToCharacter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToCharacterConverter.java similarity index 89% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToCharacter.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToCharacterConverter.java index 1db34868359..11aae0001be 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToCharacter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToCharacterConverter.java @@ -17,7 +17,6 @@ package org.springframework.core.convert.support; import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; /** * Converts from any JDK-standard Number implementation to a Character. @@ -32,9 +31,8 @@ import org.springframework.util.NumberUtils; * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal - * @see NumberUtils */ -class NumberToCharacter implements Converter { +class NumberToCharacterConverter implements Converter { public Character convert(Number source) { return (char) source.shortValue(); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToNumberFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToNumberConverterFactory.java similarity index 93% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToNumberFactory.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToNumberConverterFactory.java index 0f3c1969b1e..bff2d0b6993 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToNumberFactory.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NumberToNumberConverterFactory.java @@ -38,13 +38,12 @@ import org.springframework.util.NumberUtils; * @see java.math.BigDecimal * @see NumberUtils */ -class NumberToNumberFactory implements ConverterFactory { +class NumberToNumberConverterFactory implements ConverterFactory { public Converter getConverter(Class targetType) { return new NumberToNumber(targetType); } - private static class NumberToNumber implements Converter { private final Class targetType; @@ -53,7 +52,7 @@ class NumberToNumberFactory implements ConverterFactory { this.targetType = targetType; } - public T convert(Number source) throws Exception { + public T convert(Number source) { return NumberUtils.convertNumberToTargetClass(source, targetType); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArray.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArray.java index 92ace3eae5c..bbd1c80c31a 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArray.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArray.java @@ -34,16 +34,13 @@ class ObjectToArray implements ConversionExecutor { private final ConversionExecutor elementConverter; - public ObjectToArray(TypeDescriptor sourceObjectType, TypeDescriptor targetArrayType, GenericConversionService conversionService) { - this.elementType = targetArrayType.getElementType(); this.elementConverter = conversionService.getConversionExecutor( sourceObjectType.getType(), TypeDescriptor.valueOf(this.elementType)); } - public Object execute(Object source) throws ConversionFailedException { Object array = Array.newInstance(this.elementType, 1); Object element = this.elementConverter.execute(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollection.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollection.java index 983147d1392..a630d388c28 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollection.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollection.java @@ -33,11 +33,9 @@ class ObjectToCollection implements ConversionExecutor { private final TypeDescriptor targetCollectionType; private final ConversionExecutor elementConverter; - public ObjectToCollection(TypeDescriptor sourceObjectType, TypeDescriptor targetCollectionType, GenericConversionService typeConverter) { - this.targetCollectionType = targetCollectionType; Class elementType = targetCollectionType.getElementType(); if (elementType != null) { @@ -48,7 +46,6 @@ class ObjectToCollection implements ConversionExecutor { } } - @SuppressWarnings("unchecked") public Object execute(Object source) throws ConversionFailedException { Collection collection = CollectionFactory.createCollection(this.targetCollectionType.getType(), 1); collection.add(this.elementConverter.execute(source)); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToStringConverter.java similarity index 93% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToStringConverter.java index 385d704d54c..b5c6904e55d 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToStringConverter.java @@ -26,7 +26,7 @@ import org.springframework.core.convert.converter.Converter; * @author Keith Donald * @since 3.0 */ -class ObjectToString implements Converter { +class ObjectToStringConverter implements Converter { public String convert(Object source) { return source.toString(); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java index 0f1725774f1..981008d1fc5 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java @@ -35,15 +35,12 @@ class StaticConversionExecutor implements ConversionExecutor { private final Converter converter; - public StaticConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType, Converter converter) { this.sourceType = sourceType; this.targetType = targetType; this.converter = converter; } - - @SuppressWarnings("unchecked") public Object execute(Object source) throws ConversionFailedException { if (source == null) { return null; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToMap.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToMap.java index e8449af7b67..4945e45ec86 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToMap.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToMap.java @@ -40,7 +40,6 @@ class StringArrayToMap implements ConversionExecutor { private final MapEntryConverter entryConverter; - public StringArrayToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { this.sourceType = sourceType; this.targetType = targetType; @@ -48,7 +47,6 @@ class StringArrayToMap implements ConversionExecutor { this.entryConverter = createEntryConverter(); } - private MapEntryConverter createEntryConverter() { if (this.targetType.isMapEntryTypeKnown()) { ConversionExecutor keyConverter = this.conversionService.getConversionExecutor(String.class, @@ -62,7 +60,6 @@ class StringArrayToMap implements ConversionExecutor { } } - @SuppressWarnings("unchecked") public Object execute(Object source) throws ConversionFailedException { try { int length = Array.getLength(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToObject.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToObject.java index ac477fb379c..dd721aab9df 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToObject.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringArrayToObject.java @@ -31,12 +31,10 @@ class StringArrayToObject implements ConversionExecutor { private final ConversionExecutor elementConverter; - public StringArrayToObject(TypeDescriptor targetType, GenericConversionService conversionService) { this.elementConverter = conversionService.getConversionExecutor(String.class, targetType); } - public Object execute(Object source) throws ConversionFailedException { String str = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(source)); return this.elementConverter.execute(str); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArray.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArray.java index a52c2fba57b..0a4a9bb7dfc 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArray.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArray.java @@ -25,17 +25,14 @@ import org.springframework.util.StringUtils; * @author Keith Donald * @since 3.0 */ -@SuppressWarnings("unchecked") class StringToArray implements ConversionExecutor { private final ArrayToArray converter; - public StringToArray(TypeDescriptor targetType, GenericConversionService conversionService) { this.converter = new ArrayToArray(TypeDescriptor.valueOf(String[].class), targetType, conversionService); } - public Object execute(Object source) { String str = (String) source; String[] fields = StringUtils.commaDelimitedListToStringArray(str); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigDecimal.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigDecimal.java deleted file mode 100644 index c4688402274..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigDecimal.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import java.math.BigDecimal; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to a BigDecimal. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToBigDecimal implements Converter { - - public BigDecimal convert(String source) { - return NumberUtils.parseNumber(source, BigDecimal.class); - } - -} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigInteger.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigInteger.java deleted file mode 100644 index edec532913d..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBigInteger.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import java.math.BigInteger; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to a BigInteger. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToBigInteger implements Converter { - - public BigInteger convert(String source) { - return NumberUtils.parseNumber(source, BigInteger.class); - } - -} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBoolean.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java similarity index 94% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBoolean.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java index 6e0d0960812..68bf584d4a7 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBoolean.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java @@ -24,7 +24,7 @@ import org.springframework.core.convert.converter.Converter; * @author Keith Donald * @since 3.0 */ -class StringToBoolean implements Converter { +class StringToBooleanConverter implements Converter { public Boolean convert(String source) { if (source.equals("true")) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToByte.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToByte.java deleted file mode 100644 index dd8b156b6d4..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToByte.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to a Byte. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToByte implements Converter { - - public Byte convert(String source) { - return NumberUtils.parseNumber(source, Byte.class); - } - -} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java similarity index 79% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacter.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java index 0167f6498bd..b6e84f0beb1 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java @@ -24,11 +24,11 @@ import org.springframework.core.convert.converter.Converter; * @author Keith Donald * @since 3.0 */ -class StringToCharacter implements Converter { +class StringToCharacterConverter implements Converter { public Character convert(String source) { - if (source.length() != 1) { - throw new IllegalArgumentException("To be a Character the String '" + source + "' must have a length of 1"); + if (source.length() == 0) { + throw new IllegalArgumentException("Invalid value; to convert to a Character a String must have a length of 1 or greater"); } return source.charAt(0); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollection.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollection.java index 1179c6cbe67..9589e3cc5af 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollection.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCollection.java @@ -30,12 +30,10 @@ class StringToCollection implements ConversionExecutor { private final ArrayToCollection converter; - public StringToCollection(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { this.converter = new ArrayToCollection(sourceType, targetType, conversionService); } - public Object execute(Object source) throws ConversionFailedException { String string = (String) source; String[] fields = string.split(","); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToDouble.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToDouble.java deleted file mode 100644 index 7b431c76dcb..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToDouble.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to a Double. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToDouble implements Converter { - - public Double convert(String source) { - return NumberUtils.parseNumber(source, Double.class); - } - -} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java similarity index 93% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumFactory.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java index 4f48bf8673f..f0c04ecec85 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumFactory.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java @@ -27,13 +27,12 @@ import org.springframework.core.convert.converter.ConverterInfo; * @since 3.0 */ @SuppressWarnings("unchecked") -class StringToEnumFactory implements ConverterFactory { +class StringToEnumConverterFactory implements ConverterFactory { public Converter getConverter(Class targetType) { return new StringToEnum(targetType); } - private class StringToEnum implements Converter, ConverterInfo { private final Class enumType; @@ -50,7 +49,7 @@ class StringToEnumFactory implements ConverterFactory { return this.enumType; } - public T convert(String source) throws Exception { + public T convert(String source) { if ("".equals(source)) { // It's an empty enum identifier: reset the enum value to null. return null; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToFloat.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToFloat.java deleted file mode 100644 index 31cc441937f..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToFloat.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to Float. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToFloat implements Converter { - - public Float convert(String source) { - return NumberUtils.parseNumber(source, Float.class); - } - -} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToInteger.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToInteger.java deleted file mode 100644 index ef8fe1d3e33..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToInteger.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to an Integer. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToInteger implements Converter { - - public Integer convert(String source) { - return NumberUtils.parseNumber(source, Integer.class); - } - -} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLocale.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLocaleConverter.java similarity index 93% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLocale.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLocaleConverter.java index 1f18f4d639c..3bcfa717059 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLocale.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLocaleConverter.java @@ -27,7 +27,7 @@ import org.springframework.util.StringUtils; * @author Keith Donald * @since 3.0 */ -class StringToLocale implements Converter { +class StringToLocaleConverter implements Converter { public Locale convert(String source) { return StringUtils.parseLocaleString(source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLong.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLong.java deleted file mode 100644 index 7e5a9090e9c..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToLong.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to a Long. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToLong implements Converter { - - public Long convert(String source) { - return NumberUtils.parseNumber(source, Long.class); - } - -} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToMap.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToMap.java index 66c838ad5c6..fb2385394dd 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToMap.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToMap.java @@ -32,7 +32,6 @@ import org.springframework.core.convert.TypeDescriptor; * @author Keith Donald * @since 3.0 */ -@SuppressWarnings("unchecked") class StringToMap implements ConversionExecutor { private StringArrayToMap converter; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java new file mode 100644 index 00000000000..46305ac87dc --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.convert.support; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.ConverterFactory; +import org.springframework.util.NumberUtils; + +/** + * Converts from a String any JDK-standard Number implementation. + * + *

Support Number classes including Byte, Short, Integer, Float, Double, Long, BigInteger, BigDecimal. This class + * delegates to {@link NumberUtils#parseNumber(String, Class)} to perform the conversion. + * + * @author Keith Donald + * @since 3.0 + * @see java.lang.Byte + * @see java.lang.Short + * @see java.lang.Integer + * @see java.lang.Long + * @see java.math.BigInteger + * @see java.lang.Float + * @see java.lang.Double + * @see java.math.BigDecimal + * @see NumberUtils + */ +class StringToNumberConverterFactory implements ConverterFactory { + + public Converter getConverter(Class targetType) { + return new StringToNumber(targetType); + } + + private static class StringToNumber implements Converter { + + private final Class targetType; + + public StringToNumber(Class targetType) { + this.targetType = targetType; + } + + public T convert(String source) { + return NumberUtils.parseNumber(source, targetType); + } + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToShort.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToShort.java deleted file mode 100644 index b214251ac84..00000000000 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToShort.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.core.convert.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.NumberUtils; - -/** - * Converts a String to a Short. - * - * @author Keith Donald - * @since 3.0 - */ -class StringToShort implements Converter { - - public Short convert(String source) { - return NumberUtils.parseNumber(source, Short.class); - } - -}