Skip to content

Commit 48b965a

Browse files
committed
Improve performance of NumberUtils
This commit aims to improve the space and time performance of NumberUtils by invoking valueOf() factory methods instead of the corresponding constructors when converting a number to a target class.
1 parent e0392d9 commit 48b965a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

spring-core/src/main/java/org/springframework/util/NumberUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ else if (Byte.class == targetClass) {
9191
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
9292
raiseOverflowException(number, targetClass);
9393
}
94-
return (T) new Byte(number.byteValue());
94+
return (T) Byte.valueOf(number.byteValue());
9595
}
9696
else if (Short.class == targetClass) {
9797
long value = number.longValue();
9898
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
9999
raiseOverflowException(number, targetClass);
100100
}
101-
return (T) new Short(number.shortValue());
101+
return (T) Short.valueOf(number.shortValue());
102102
}
103103
else if (Integer.class == targetClass) {
104104
long value = number.longValue();
105105
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
106106
raiseOverflowException(number, targetClass);
107107
}
108-
return (T) new Integer(number.intValue());
108+
return (T) Integer.valueOf(number.intValue());
109109
}
110110
else if (Long.class == targetClass) {
111111
BigInteger bigInt = null;
@@ -119,7 +119,7 @@ else if (number instanceof BigDecimal) {
119119
if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) {
120120
raiseOverflowException(number, targetClass);
121121
}
122-
return (T) new Long(number.longValue());
122+
return (T) Long.valueOf(number.longValue());
123123
}
124124
else if (BigInteger.class == targetClass) {
125125
if (number instanceof BigDecimal) {
@@ -132,10 +132,10 @@ else if (BigInteger.class == targetClass) {
132132
}
133133
}
134134
else if (Float.class == targetClass) {
135-
return (T) new Float(number.floatValue());
135+
return (T) Float.valueOf(number.floatValue());
136136
}
137137
else if (Double.class == targetClass) {
138-
return (T) new Double(number.doubleValue());
138+
return (T) Double.valueOf(number.doubleValue());
139139
}
140140
else if (BigDecimal.class == targetClass) {
141141
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)

0 commit comments

Comments
 (0)