default converter tests
This commit is contained in:
@@ -37,7 +37,7 @@ import org.springframework.util.NumberUtils;
|
||||
*/
|
||||
public class NumberToNumber implements SuperConverter<Number, Number> {
|
||||
|
||||
public <RT extends Number> RT convert(Number source, Class<RT> targetClass) throws Exception {
|
||||
public <RT extends Number> RT convert(Number source, Class<RT> targetClass) {
|
||||
return NumberUtils.convertNumberToTargetClass(source, targetClass);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,11 @@ import java.math.BigDecimal;
|
||||
*/
|
||||
public class StringToBigDecimal implements Converter<String, BigDecimal> {
|
||||
|
||||
public BigDecimal convert(String source) throws Exception {
|
||||
public BigDecimal convert(String source) {
|
||||
return new BigDecimal(source);
|
||||
}
|
||||
|
||||
public String convertBack(BigDecimal target) throws Exception {
|
||||
public String convertBack(BigDecimal target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,11 @@ import java.math.BigInteger;
|
||||
*/
|
||||
public class StringToBigInteger implements Converter<String, BigInteger> {
|
||||
|
||||
public BigInteger convert(String source) throws Exception {
|
||||
public BigInteger convert(String source) {
|
||||
return new BigInteger(source);
|
||||
}
|
||||
|
||||
public String convertBack(BigInteger target) throws Exception {
|
||||
public String convertBack(BigInteger target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.core.convert.converter;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Converts String to a Boolean. The trueString and falseStrings are configurable.
|
||||
*
|
||||
@@ -23,10 +25,6 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToBoolean implements Converter<String, Boolean> {
|
||||
|
||||
private static final String VALUE_TRUE = "true";
|
||||
|
||||
private static final String VALUE_FALSE = "false";
|
||||
|
||||
private String trueString;
|
||||
|
||||
private String falseString;
|
||||
@@ -35,47 +33,38 @@ public class StringToBoolean implements Converter<String, Boolean> {
|
||||
* Create a StringToBoolean converter with the default 'true' and 'false' strings.
|
||||
*/
|
||||
public StringToBoolean() {
|
||||
this("true", "false");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a StringToBoolean converter configured with specific values for true and false strings.
|
||||
* @param trueString special true string to use
|
||||
* @param falseString special false string to use
|
||||
* @param trueString special true string to use (required)
|
||||
* @param falseString special false string to use (required)
|
||||
*/
|
||||
public StringToBoolean(String trueString, String falseString) {
|
||||
Assert.hasText(trueString, "The true string is required");
|
||||
Assert.hasText(falseString, "The false string is required");
|
||||
this.trueString = trueString;
|
||||
this.falseString = falseString;
|
||||
}
|
||||
|
||||
public Boolean convert(String source) throws Exception {
|
||||
if (trueString != null && source.equals(trueString)) {
|
||||
public Boolean convert(String source) {
|
||||
if (source.equals(trueString)) {
|
||||
return Boolean.TRUE;
|
||||
} else if (falseString != null && source.equals(falseString)) {
|
||||
return Boolean.FALSE;
|
||||
} else if (trueString == null && source.equals(VALUE_TRUE)) {
|
||||
return Boolean.TRUE;
|
||||
} else if (falseString == null && source.equals(VALUE_FALSE)) {
|
||||
} else if (source.equals(falseString)) {
|
||||
return Boolean.FALSE;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid boolean value [" + source + "]");
|
||||
throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected '" + trueString + "' or '" + falseString + "'");
|
||||
}
|
||||
}
|
||||
|
||||
public String convertBack(Boolean target) throws Exception {
|
||||
public String convertBack(Boolean target) {
|
||||
if (Boolean.TRUE.equals(target)) {
|
||||
if (trueString != null) {
|
||||
return trueString;
|
||||
} else {
|
||||
return VALUE_TRUE;
|
||||
}
|
||||
return trueString;
|
||||
} else if (Boolean.FALSE.equals(target)) {
|
||||
if (falseString != null) {
|
||||
return falseString;
|
||||
} else {
|
||||
return VALUE_FALSE;
|
||||
}
|
||||
return falseString;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid boolean value [" + target + "]");
|
||||
throw new IllegalArgumentException("Invalid boolean value " + target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToByte implements Converter<String, Byte> {
|
||||
|
||||
public Byte convert(String source) throws Exception {
|
||||
return new Byte(source);
|
||||
public Byte convert(String source) {
|
||||
return Byte.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Byte target) throws Exception {
|
||||
public String convertBack(Byte target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,14 +22,14 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToCharacter implements Converter<String, Character> {
|
||||
|
||||
public Character convert(String source) throws Exception {
|
||||
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");
|
||||
}
|
||||
return new Character(source.charAt(0));
|
||||
return Character.valueOf(source.charAt(0));
|
||||
}
|
||||
|
||||
public String convertBack(Character target) throws Exception {
|
||||
public String convertBack(Character target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToDouble implements Converter<String, Double> {
|
||||
|
||||
public Double convert(String source) throws Exception {
|
||||
public Double convert(String source) {
|
||||
return Double.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Double target) throws Exception {
|
||||
public String convertBack(Double target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,11 @@ package org.springframework.core.convert.converter;
|
||||
@SuppressWarnings("unchecked")
|
||||
public class StringToEnum implements SuperTwoWayConverter<String, Enum> {
|
||||
|
||||
public <RT extends Enum> RT convert(String source, Class<RT> targetClass) throws Exception {
|
||||
public <RT extends Enum> RT convert(String source, Class<RT> targetClass) {
|
||||
return (RT) Enum.valueOf(targetClass, source);
|
||||
}
|
||||
|
||||
public <RS extends String> RS convertBack(Enum target, Class<RS> sourceClass) throws Exception {
|
||||
public <RS extends String> RS convertBack(Enum target, Class<RS> sourceClass) {
|
||||
return (RS) target.name();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToFloat implements Converter<String, Float> {
|
||||
|
||||
public Float convert(String source) throws Exception {
|
||||
public Float convert(String source) {
|
||||
return Float.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Float target) throws Exception {
|
||||
public String convertBack(Float target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToInteger implements Converter<String, Integer> {
|
||||
|
||||
public Integer convert(String source) throws Exception {
|
||||
public Integer convert(String source) {
|
||||
return Integer.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Integer target) throws Exception {
|
||||
public String convertBack(Integer target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,11 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class StringToLocale implements Converter<String, Locale> {
|
||||
|
||||
public Locale convert(String source) throws Exception {
|
||||
public Locale convert(String source) {
|
||||
return StringUtils.parseLocaleString(source);
|
||||
}
|
||||
|
||||
public String convertBack(Locale target) throws Exception {
|
||||
public String convertBack(Locale target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToLong implements Converter<String, Long> {
|
||||
|
||||
public Long convert(String source) throws Exception {
|
||||
public Long convert(String source) {
|
||||
return Long.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Long target) throws Exception {
|
||||
public String convertBack(Long target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public class StringToShort implements Converter<String, Short> {
|
||||
|
||||
public Short convert(String source) throws Exception {
|
||||
public Short convert(String source) {
|
||||
return Short.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Short target) throws Exception {
|
||||
public String convertBack(Short target) {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user