convert polishing: removed checked exception from Converter interface to support direct use, added Converter to each implementation for consistency
This commit is contained in:
parent
881c3ed6fa
commit
1c6965132f
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ class CharacterToNumberFactory implements ConverterFactory<Character, Number> {
|
|||
return new CharacterToNumber<T>(targetType);
|
||||
}
|
||||
|
||||
|
||||
private static class CharacterToNumber<T extends Number> implements Converter<Character, T> {
|
||||
|
||||
private final Class<T> targetType;
|
||||
|
|
@ -53,7 +52,7 @@ class CharacterToNumberFactory implements ConverterFactory<Character, Number> {
|
|||
this.targetType = targetType;
|
||||
}
|
||||
|
||||
public T convert(Character source) throws Exception {
|
||||
public T convert(Character source) {
|
||||
return NumberUtils.convertNumberToTargetClass((short) source.charValue(), targetType);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Class, Map<Class, Object>> sourceTypeConverters = new HashMap<Class, Map<Class, Object>>();
|
||||
|
||||
|
||||
/**
|
||||
* 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<ConverterFactory> 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<Class> typeInfo = getRequiredTypeInfo(converter);
|
||||
if (typeInfo == null) {
|
||||
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> your Converter<S, T> converts between");
|
||||
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> your Converter<S, T> 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<Class> typeInfo = getRequiredTypeInfo(converterFactory);
|
||||
if (typeInfo == null) {
|
||||
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> your ConverterFactory<S, T> creates Converters to convert between");
|
||||
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> your ConverterFactory<S, T> 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<Class, Object> converters, Class<?> currentClass, Class<?> targetType) {
|
||||
Object converter = converters.get(currentClass);
|
||||
if (converter == null) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ class NoOpConversionExecutor implements ConversionExecutor {
|
|||
|
||||
public static final ConversionExecutor INSTANCE = new NoOpConversionExecutor();
|
||||
|
||||
|
||||
private NoOpConversionExecutor() {
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Number, Character> {
|
||||
class NumberToCharacterConverter implements Converter<Number, Character> {
|
||||
|
||||
public Character convert(Number source) {
|
||||
return (char) source.shortValue();
|
||||
|
|
@ -38,13 +38,12 @@ import org.springframework.util.NumberUtils;
|
|||
* @see java.math.BigDecimal
|
||||
* @see NumberUtils
|
||||
*/
|
||||
class NumberToNumberFactory implements ConverterFactory<Number, Number> {
|
||||
class NumberToNumberConverterFactory implements ConverterFactory<Number, Number> {
|
||||
|
||||
public <T extends Number> Converter<Number, T> getConverter(Class<T> targetType) {
|
||||
return new NumberToNumber<T>(targetType);
|
||||
}
|
||||
|
||||
|
||||
private static class NumberToNumber<T extends Number> implements Converter<Number, T> {
|
||||
|
||||
private final Class<T> targetType;
|
||||
|
|
@ -53,7 +52,7 @@ class NumberToNumberFactory implements ConverterFactory<Number, Number> {
|
|||
this.targetType = targetType;
|
||||
}
|
||||
|
||||
public T convert(Number source) throws Exception {
|
||||
public T convert(Number source) {
|
||||
return NumberUtils.convertNumberToTargetClass(source, targetType);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -34,10 +34,8 @@ class ObjectToCollection implements ConversionExecutor {
|
|||
|
||||
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));
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import org.springframework.core.convert.converter.Converter;
|
|||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class ObjectToString implements Converter<Object, String> {
|
||||
class ObjectToStringConverter implements Converter<Object, String> {
|
||||
|
||||
public String convert(Object source) {
|
||||
return source.toString();
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<String, BigDecimal> {
|
||||
|
||||
public BigDecimal convert(String source) {
|
||||
return NumberUtils.parseNumber(source, BigDecimal.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, BigInteger> {
|
||||
|
||||
public BigInteger convert(String source) {
|
||||
return NumberUtils.parseNumber(source, BigInteger.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ import org.springframework.core.convert.converter.Converter;
|
|||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class StringToBoolean implements Converter<String, Boolean> {
|
||||
class StringToBooleanConverter implements Converter<String, Boolean> {
|
||||
|
||||
public Boolean convert(String source) {
|
||||
if (source.equals("true")) {
|
||||
|
|
@ -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<String, Byte> {
|
||||
|
||||
public Byte convert(String source) {
|
||||
return NumberUtils.parseNumber(source, Byte.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -24,11 +24,11 @@ import org.springframework.core.convert.converter.Converter;
|
|||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class StringToCharacter implements Converter<String, Character> {
|
||||
class StringToCharacterConverter implements Converter<String, Character> {
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -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(",");
|
||||
|
|
|
|||
|
|
@ -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<String, Double> {
|
||||
|
||||
public Double convert(String source) {
|
||||
return NumberUtils.parseNumber(source, Double.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -27,13 +27,12 @@ import org.springframework.core.convert.converter.ConverterInfo;
|
|||
* @since 3.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
class StringToEnumFactory implements ConverterFactory<String, Enum> {
|
||||
class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
|
||||
|
||||
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
|
||||
return new StringToEnum(targetType);
|
||||
}
|
||||
|
||||
|
||||
private class StringToEnum<T extends Enum> implements Converter<String, T>, ConverterInfo {
|
||||
|
||||
private final Class<T> enumType;
|
||||
|
|
@ -50,7 +49,7 @@ class StringToEnumFactory implements ConverterFactory<String, Enum> {
|
|||
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;
|
||||
|
|
@ -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<String, Float> {
|
||||
|
||||
public Float convert(String source) {
|
||||
return NumberUtils.parseNumber(source, Float.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Integer> {
|
||||
|
||||
public Integer convert(String source) {
|
||||
return NumberUtils.parseNumber(source, Integer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ import org.springframework.util.StringUtils;
|
|||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class StringToLocale implements Converter<String, Locale> {
|
||||
class StringToLocaleConverter implements Converter<String, Locale> {
|
||||
|
||||
public Locale convert(String source) {
|
||||
return StringUtils.parseLocaleString(source);
|
||||
|
|
@ -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<String, Long> {
|
||||
|
||||
public Long convert(String source) {
|
||||
return NumberUtils.parseNumber(source, Long.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
*
|
||||
* <p>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<String, Number> {
|
||||
|
||||
public <T extends Number> Converter<String, T> getConverter(Class<T> targetType) {
|
||||
return new StringToNumber<T>(targetType);
|
||||
}
|
||||
|
||||
private static class StringToNumber<T extends Number> implements Converter<String, T> {
|
||||
|
||||
private final Class<T> targetType;
|
||||
|
||||
public StringToNumber(Class<T> targetType) {
|
||||
this.targetType = targetType;
|
||||
}
|
||||
|
||||
public T convert(String source) {
|
||||
return NumberUtils.parseNumber(source, targetType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Short> {
|
||||
|
||||
public Short convert(String source) {
|
||||
return NumberUtils.parseNumber(source, Short.class);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue