generified NumberUtils signature
This commit is contained in:
parent
8669b3293c
commit
fb057bb5f6
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.
|
* Parse the Number from the given text, using the specified NumberFormat.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void setAsText(String text) throws IllegalArgumentException {
|
public void setAsText(String text) throws IllegalArgumentException {
|
||||||
if (this.allowEmpty && !StringUtils.hasText(text)) {
|
if (this.allowEmpty && !StringUtils.hasText(text)) {
|
||||||
// Treat empty String as null value.
|
// 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.
|
* Coerce a Number value into the required target class, if necessary.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void setValue(Object value) {
|
public void setValue(Object value) {
|
||||||
if (value instanceof Number) {
|
if (value instanceof Number) {
|
||||||
super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
|
super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.lang.Double
|
||||||
* @see java.math.BigDecimal
|
* @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 {
|
throws IllegalArgumentException {
|
||||||
|
|
||||||
Assert.notNull(number, "Number must not be null");
|
Assert.notNull(number, "Number must not be null");
|
||||||
Assert.notNull(targetClass, "Target class must not be null");
|
Assert.notNull(targetClass, "Target class must not be null");
|
||||||
|
|
||||||
if (targetClass.isInstance(number)) {
|
if (targetClass.isInstance(number)) {
|
||||||
return number;
|
return (T) number;
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(Byte.class)) {
|
else if (targetClass.equals(Byte.class)) {
|
||||||
long value = number.longValue();
|
long value = number.longValue();
|
||||||
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
|
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
|
||||||
raiseOverflowException(number, targetClass);
|
raiseOverflowException(number, targetClass);
|
||||||
}
|
}
|
||||||
return number.byteValue();
|
return (T) new Byte(number.byteValue());
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(Short.class)) {
|
else if (targetClass.equals(Short.class)) {
|
||||||
long value = number.longValue();
|
long value = number.longValue();
|
||||||
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
|
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
|
||||||
raiseOverflowException(number, targetClass);
|
raiseOverflowException(number, targetClass);
|
||||||
}
|
}
|
||||||
return number.shortValue();
|
return (T) new Short(number.shortValue());
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(Integer.class)) {
|
else if (targetClass.equals(Integer.class)) {
|
||||||
long value = number.longValue();
|
long value = number.longValue();
|
||||||
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
|
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
|
||||||
raiseOverflowException(number, targetClass);
|
raiseOverflowException(number, targetClass);
|
||||||
}
|
}
|
||||||
return number.intValue();
|
return (T) new Integer(number.intValue());
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(Long.class)) {
|
else if (targetClass.equals(Long.class)) {
|
||||||
return number.longValue();
|
return (T) new Long(number.longValue());
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(BigInteger.class)) {
|
else if (targetClass.equals(BigInteger.class)) {
|
||||||
if (number instanceof BigDecimal) {
|
if (number instanceof BigDecimal) {
|
||||||
// do not lose precision - use BigDecimal's own conversion
|
// do not lose precision - use BigDecimal's own conversion
|
||||||
return ((BigDecimal) number).toBigInteger();
|
return (T) ((BigDecimal) number).toBigInteger();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// original value is not a Big* number - use standard long conversion
|
// 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)) {
|
else if (targetClass.equals(Float.class)) {
|
||||||
return number.floatValue();
|
return (T) new Float(number.floatValue());
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(Double.class)) {
|
else if (targetClass.equals(Double.class)) {
|
||||||
return number.doubleValue();
|
return (T) new Double(number.doubleValue());
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(BigDecimal.class)) {
|
else if (targetClass.equals(BigDecimal.class)) {
|
||||||
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
|
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
|
||||||
// (see BigDecimal javadoc for details)
|
// (see BigDecimal javadoc for details)
|
||||||
return new BigDecimal(number.toString());
|
return (T) new BigDecimal(number.toString());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" +
|
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.lang.Double#valueOf
|
||||||
* @see java.math.BigDecimal#BigDecimal(String)
|
* @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(text, "Text must not be null");
|
||||||
Assert.notNull(targetClass, "Target class must not be null");
|
Assert.notNull(targetClass, "Target class must not be null");
|
||||||
String trimmed = StringUtils.trimAllWhitespace(text);
|
String trimmed = StringUtils.trimAllWhitespace(text);
|
||||||
|
|
||||||
if (targetClass.equals(Byte.class)) {
|
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)) {
|
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)) {
|
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)) {
|
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)) {
|
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)) {
|
else if (targetClass.equals(Float.class)) {
|
||||||
return Float.valueOf(trimmed);
|
return (T) Float.valueOf(trimmed);
|
||||||
}
|
}
|
||||||
else if (targetClass.equals(Double.class)) {
|
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)) {
|
else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
|
||||||
return new BigDecimal(trimmed);
|
return (T) new BigDecimal(trimmed);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
|
@ -188,7 +190,7 @@ public abstract class NumberUtils {
|
||||||
* @see #convertNumberToTargetClass
|
* @see #convertNumberToTargetClass
|
||||||
* @see #parseNumber(String, Class)
|
* @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) {
|
if (numberFormat != null) {
|
||||||
Assert.notNull(text, "Text must not be null");
|
Assert.notNull(text, "Text must not be null");
|
||||||
Assert.notNull(targetClass, "Target class must not be null");
|
Assert.notNull(targetClass, "Target class must not be null");
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* @return the converted value
|
||||||
* @see #getColumnValue(java.sql.ResultSet, int, Class)
|
* @see #getColumnValue(java.sql.ResultSet, int, Class)
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
protected Object convertValueToRequiredType(Object value, Class requiredType) {
|
protected Object convertValueToRequiredType(Object value, Class requiredType) {
|
||||||
if (String.class.equals(requiredType)) {
|
if (String.class.equals(requiredType)) {
|
||||||
return value.toString();
|
return value.toString();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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)) {
|
else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
|
||||||
try {
|
try {
|
||||||
result = NumberUtils.convertNumberToTargetClass(((Number) result), requiredType);
|
result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (IllegalArgumentException ex) {
|
||||||
throw new TypeMismatchDataAccessException(ex.getMessage());
|
throw new TypeMismatchDataAccessException(ex.getMessage());
|
||||||
|
|
Loading…
Reference in New Issue