提交 fb057bb5 编写于 作者: J Juergen Hoeller

generified NumberUtils signature

上级 8669b329
/*
* Copyright 2002-2008 the original author or authors.
* 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.
......@@ -101,6 +101,7 @@ public class CustomNumberEditor extends PropertyEditorSupport {
* Parse the Number from the given text, using the specified NumberFormat.
*/
@Override
@SuppressWarnings("unchecked")
public void setAsText(String text) throws IllegalArgumentException {
if (this.allowEmpty && !StringUtils.hasText(text)) {
// Treat empty String as null value.
......@@ -120,6 +121,7 @@ public class CustomNumberEditor extends PropertyEditorSupport {
* Coerce a Number value into the required target class, if necessary.
*/
@Override
@SuppressWarnings("unchecked")
public void setValue(Object value) {
if (value instanceof Number) {
super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
......
/*
* Copyright 2002-2008 the original author or authors.
* 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.
......@@ -49,59 +49,60 @@ public abstract class NumberUtils {
* @see java.lang.Double
* @see java.math.BigDecimal
*/
public static Number convertNumberToTargetClass(Number number, Class targetClass)
@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
throws IllegalArgumentException {
Assert.notNull(number, "Number must not be null");
Assert.notNull(targetClass, "Target class must not be null");
if (targetClass.isInstance(number)) {
return number;
return (T) number;
}
else if (targetClass.equals(Byte.class)) {
long value = number.longValue();
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
raiseOverflowException(number, targetClass);
}
return number.byteValue();
return (T) new Byte(number.byteValue());
}
else if (targetClass.equals(Short.class)) {
long value = number.longValue();
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
raiseOverflowException(number, targetClass);
}
return number.shortValue();
return (T) new Short(number.shortValue());
}
else if (targetClass.equals(Integer.class)) {
long value = number.longValue();
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
raiseOverflowException(number, targetClass);
}
return number.intValue();
return (T) new Integer(number.intValue());
}
else if (targetClass.equals(Long.class)) {
return number.longValue();
return (T) new Long(number.longValue());
}
else if (targetClass.equals(BigInteger.class)) {
if (number instanceof BigDecimal) {
// do not lose precision - use BigDecimal's own conversion
return ((BigDecimal) number).toBigInteger();
return (T) ((BigDecimal) number).toBigInteger();
}
else {
// original value is not a Big* number - use standard long conversion
return BigInteger.valueOf(number.longValue());
return (T) BigInteger.valueOf(number.longValue());
}
}
else if (targetClass.equals(Float.class)) {
return number.floatValue();
return (T) new Float(number.floatValue());
}
else if (targetClass.equals(Double.class)) {
return number.doubleValue();
return (T) new Double(number.doubleValue());
}
else if (targetClass.equals(BigDecimal.class)) {
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
// (see BigDecimal javadoc for details)
return new BigDecimal(number.toString());
return (T) new BigDecimal(number.toString());
}
else {
throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" +
......@@ -138,34 +139,35 @@ public abstract class NumberUtils {
* @see java.lang.Double#valueOf
* @see java.math.BigDecimal#BigDecimal(String)
*/
public static Number parseNumber(String text, Class targetClass) {
@SuppressWarnings("unchecked")
public static <T extends Number> T parseNumber(String text, Class<T> targetClass) {
Assert.notNull(text, "Text must not be null");
Assert.notNull(targetClass, "Target class must not be null");
String trimmed = StringUtils.trimAllWhitespace(text);
if (targetClass.equals(Byte.class)) {
return (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed));
return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed));
}
else if (targetClass.equals(Short.class)) {
return (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed));
return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed));
}
else if (targetClass.equals(Integer.class)) {
return (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed));
return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed));
}
else if (targetClass.equals(Long.class)) {
return (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed));
return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed));
}
else if (targetClass.equals(BigInteger.class)) {
return (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed));
return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed));
}
else if (targetClass.equals(Float.class)) {
return Float.valueOf(trimmed);
return (T) Float.valueOf(trimmed);
}
else if (targetClass.equals(Double.class)) {
return Double.valueOf(trimmed);
return (T) Double.valueOf(trimmed);
}
else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
return new BigDecimal(trimmed);
return (T) new BigDecimal(trimmed);
}
else {
throw new IllegalArgumentException(
......@@ -188,7 +190,7 @@ public abstract class NumberUtils {
* @see #convertNumberToTargetClass
* @see #parseNumber(String, Class)
*/
public static Number parseNumber(String text, Class targetClass, NumberFormat numberFormat) {
public static <T extends Number> T parseNumber(String text, Class<T> targetClass, NumberFormat numberFormat) {
if (numberFormat != null) {
Assert.notNull(text, "Text must not be null");
Assert.notNull(targetClass, "Target class must not be null");
......
/*
* Copyright 2002-2008 the original author or authors.
* 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.
......@@ -162,6 +162,7 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
* @return the converted value
* @see #getColumnValue(java.sql.ResultSet, int, Class)
*/
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class requiredType) {
if (String.class.equals(requiredType)) {
return value.toString();
......
/*
* Copyright 2002-2008 the original author or authors.
* 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.
......@@ -144,7 +144,7 @@ public abstract class DataAccessUtils {
}
else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
try {
result = NumberUtils.convertNumberToTargetClass(((Number) result), requiredType);
result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
}
catch (IllegalArgumentException ex) {
throw new TypeMismatchDataAccessException(ex.getMessage());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册