1 16 17 package org.springframework.util; 18 19 import java.math.BigDecimal ; 20 import java.math.BigInteger ; 21 import java.text.NumberFormat ; 22 import java.text.ParseException ; 23 24 33 public abstract class NumberUtils { 34 35 51 public static Number convertNumberToTargetClass(Number number, Class targetClass) 52 throws IllegalArgumentException { 53 54 Assert.notNull(number, "Number must not be null"); 55 Assert.notNull(targetClass, "Target class must not be null"); 56 57 if (targetClass.isInstance(number)) { 58 return number; 59 } 60 else if (targetClass.equals(Byte .class)) { 61 long value = number.longValue(); 62 if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { 63 raiseOverflowException(number, targetClass); 64 } 65 return new Byte (number.byteValue()); 66 } 67 else if (targetClass.equals(Short .class)) { 68 long value = number.longValue(); 69 if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { 70 raiseOverflowException(number, targetClass); 71 } 72 return new Short (number.shortValue()); 73 } 74 else if (targetClass.equals(Integer .class)) { 75 long value = number.longValue(); 76 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { 77 raiseOverflowException(number, targetClass); 78 } 79 return new Integer (number.intValue()); 80 } 81 else if (targetClass.equals(Long .class)) { 82 return new Long (number.longValue()); 83 } 84 else if (targetClass.equals(Float .class)) { 85 return new Float (number.floatValue()); 86 } 87 else if (targetClass.equals(Double .class)) { 88 return new Double (number.doubleValue()); 89 } 90 else if (targetClass.equals(BigInteger .class)) { 91 return BigInteger.valueOf(number.longValue()); 92 } 93 else if (targetClass.equals(BigDecimal .class)) { 94 return new BigDecimal (number.toString()); 97 } 98 else { 99 throw new IllegalArgumentException ("Could not convert number [" + number + "] of type [" + 100 number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); 101 } 102 } 103 104 109 private static void raiseOverflowException(Number number, Class targetClass) { 110 throw new IllegalArgumentException ("Could not convert number [" + number + "] of type [" + 111 number.getClass().getName() + "] to target class [" + targetClass.getName() + "]: overflow"); 112 } 113 114 133 public static Number parseNumber(String text, Class targetClass) { 134 Assert.notNull(text, "Text must not be null"); 135 Assert.notNull(targetClass, "Target class must not be null"); 136 137 String trimmed = text.trim(); 138 139 if (targetClass.equals(Byte .class)) { 140 return Byte.decode(trimmed); 141 } 142 else if (targetClass.equals(Short .class)) { 143 return Short.decode(trimmed); 144 } 145 else if (targetClass.equals(Integer .class)) { 146 return Integer.decode(trimmed); 147 } 148 else if (targetClass.equals(Long .class)) { 149 return Long.decode(trimmed); 150 } 151 else if (targetClass.equals(BigInteger .class)) { 152 return decodeBigInteger(trimmed); 153 } 154 else if (targetClass.equals(Float .class)) { 155 return Float.valueOf(trimmed); 156 } 157 else if (targetClass.equals(Double .class)) { 158 return Double.valueOf(trimmed); 159 } 160 else if (targetClass.equals(BigDecimal .class) || targetClass.equals(Number .class)) { 161 return new BigDecimal (trimmed); 162 } 163 else { 164 throw new IllegalArgumentException ( 165 "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]"); 166 } 167 } 168 169 184 public static Number parseNumber(String text, Class targetClass, NumberFormat numberFormat) { 185 if (numberFormat != null) { 186 Assert.notNull(text, "Text must not be null"); 187 Assert.notNull(targetClass, "Target class must not be null"); 188 try { 189 Number number = numberFormat.parse(text.trim()); 190 return convertNumberToTargetClass(number, targetClass); 191 } 192 catch (ParseException ex) { 193 throw new IllegalArgumentException (ex.getMessage()); 194 } 195 } 196 else { 197 return parseNumber(text, targetClass); 198 } 199 } 200 201 206 private static BigInteger decodeBigInteger(String value) { 207 int radix = 10; 208 int index = 0; 209 boolean negative = false; 210 211 if (value.startsWith("-")) { 213 negative = true; 214 index++; 215 } 216 217 if (value.startsWith("0x", index) || value.startsWith("0X", index)) { 219 index += 2; 220 radix = 16; 221 } 222 else if (value.startsWith("#", index)) { 223 index++; 224 radix = 16; 225 } 226 else if (value.startsWith("0", index) && value.length() > 1 + index) { 227 index++; 228 radix = 8; 229 } 230 231 BigInteger result = new BigInteger (value.substring(index), radix); 232 return (negative ? result.negate() : result); 233 } 234 235 } 236 | Popular Tags |