From fb057bb5f6b694c07534696029de36ee7d23e5d5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 12 Feb 2009 17:40:51 +0000 Subject: [PATCH] generified NumberUtils signature --- .../propertyeditors/CustomNumberEditor.java | 4 +- .../org/springframework/util/NumberUtils.java | 46 ++++++++++--------- .../jdbc/core/SingleColumnRowMapper.java | 3 +- .../dao/support/DataAccessUtils.java | 4 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CustomNumberEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CustomNumberEditor.java index e967672547..6c1f55255f 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CustomNumberEditor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CustomNumberEditor.java @@ -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"); * 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)); diff --git a/org.springframework.core/src/main/java/org/springframework/util/NumberUtils.java b/org.springframework.core/src/main/java/org/springframework/util/NumberUtils.java index 1e82ee65e0..4dc2d3e6ca 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/NumberUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/NumberUtils.java @@ -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"); * 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 convertNumberToTargetClass(Number number, Class 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 parseNumber(String text, Class 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 parseNumber(String text, Class targetClass, NumberFormat numberFormat) { if (numberFormat != null) { Assert.notNull(text, "Text must not be null"); Assert.notNull(targetClass, "Target class must not be null"); diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java index a7b440fc48..32311d42c9 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java @@ -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"); * you may not use this file except in compliance with the License. @@ -162,6 +162,7 @@ public class SingleColumnRowMapper implements RowMapper { * @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(); diff --git a/org.springframework.transaction/src/main/java/org/springframework/dao/support/DataAccessUtils.java b/org.springframework.transaction/src/main/java/org/springframework/dao/support/DataAccessUtils.java index 7f5331819a..6e7e07608a 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/dao/support/DataAccessUtils.java +++ b/org.springframework.transaction/src/main/java/org/springframework/dao/support/DataAccessUtils.java @@ -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"); * 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) requiredType); } catch (IllegalArgumentException ex) { throw new TypeMismatchDataAccessException(ex.getMessage()); -- GitLab