| 1 | |
package liquibase.util; |
| 2 | |
|
| 3 | |
import java.math.BigDecimal; |
| 4 | |
import java.math.BigInteger; |
| 5 | |
import java.text.NumberFormat; |
| 6 | |
import java.text.ParseException; |
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | 0 | public abstract class NumberUtils { |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public static Number convertNumberToTargetClass(Number number, Class targetClass) throws IllegalArgumentException { |
| 38 | |
|
| 39 | 0 | if (targetClass.isInstance(number)) { |
| 40 | 0 | return number; |
| 41 | 0 | } else if (targetClass.equals(Byte.class)) { |
| 42 | 0 | long value = number.longValue(); |
| 43 | 0 | if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { |
| 44 | 0 | raiseOverflowException(number, targetClass); |
| 45 | |
} |
| 46 | 0 | return number.byteValue(); |
| 47 | 0 | } else if (targetClass.equals(Short.class)) { |
| 48 | 0 | long value = number.longValue(); |
| 49 | 0 | if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { |
| 50 | 0 | raiseOverflowException(number, targetClass); |
| 51 | |
} |
| 52 | 0 | return number.shortValue(); |
| 53 | 0 | } else if (targetClass.equals(Integer.class)) { |
| 54 | 0 | long value = number.longValue(); |
| 55 | 0 | if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { |
| 56 | 0 | raiseOverflowException(number, targetClass); |
| 57 | |
} |
| 58 | 0 | return number.intValue(); |
| 59 | 0 | } else if (targetClass.equals(Long.class)) { |
| 60 | 0 | return number.longValue(); |
| 61 | 0 | } else if (targetClass.equals(Float.class)) { |
| 62 | 0 | return number.floatValue(); |
| 63 | 0 | } else if (targetClass.equals(Double.class)) { |
| 64 | 0 | return number.doubleValue(); |
| 65 | 0 | } else if (targetClass.equals(BigInteger.class)) { |
| 66 | 0 | return BigInteger.valueOf(number.longValue()); |
| 67 | 0 | } else if (targetClass.equals(BigDecimal.class)) { |
| 68 | |
|
| 69 | |
|
| 70 | 0 | return new BigDecimal(number.toString()); |
| 71 | |
} else { |
| 72 | 0 | throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" |
| 73 | |
+ number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); |
| 74 | |
} |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
private static void raiseOverflowException(Number number, Class targetClass) { |
| 86 | 0 | throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" |
| 87 | |
+ number.getClass().getName() + "] to target class [" + targetClass.getName() + "]: overflow"); |
| 88 | |
} |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public static Number parseNumber(String text, Class targetClass) { |
| 112 | 0 | String trimmed = text.trim(); |
| 113 | |
|
| 114 | 0 | if (targetClass.equals(Byte.class)) { |
| 115 | 0 | return Byte.decode(trimmed); |
| 116 | 0 | } else if (targetClass.equals(Short.class)) { |
| 117 | 0 | return Short.decode(trimmed); |
| 118 | 0 | } else if (targetClass.equals(Integer.class)) { |
| 119 | 0 | return Integer.decode(trimmed); |
| 120 | 0 | } else if (targetClass.equals(Long.class)) { |
| 121 | 0 | return Long.decode(trimmed); |
| 122 | 0 | } else if (targetClass.equals(BigInteger.class)) { |
| 123 | 0 | return decodeBigInteger(trimmed); |
| 124 | 0 | } else if (targetClass.equals(Float.class)) { |
| 125 | 0 | return Float.valueOf(trimmed); |
| 126 | 0 | } else if (targetClass.equals(Double.class)) { |
| 127 | 0 | return Double.valueOf(trimmed); |
| 128 | 0 | } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) { |
| 129 | 0 | return new BigDecimal(trimmed); |
| 130 | |
} else { |
| 131 | 0 | throw new IllegalArgumentException("Cannot convert String [" + text + "] to target class [" |
| 132 | |
+ targetClass.getName() + "]"); |
| 133 | |
} |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public static Number parseNumber(String text, Class targetClass, NumberFormat numberFormat) { |
| 155 | 0 | if (numberFormat != null) { |
| 156 | |
try { |
| 157 | 0 | Number number = numberFormat.parse(text.trim()); |
| 158 | 0 | return convertNumberToTargetClass(number, targetClass); |
| 159 | 0 | } catch (ParseException ex) { |
| 160 | 0 | throw new IllegalArgumentException(ex.getMessage()); |
| 161 | |
} |
| 162 | |
} else { |
| 163 | 0 | return parseNumber(text, targetClass); |
| 164 | |
} |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
private static BigInteger decodeBigInteger(String value) { |
| 173 | 0 | int radix = 10; |
| 174 | 0 | int index = 0; |
| 175 | 0 | boolean negative = false; |
| 176 | |
|
| 177 | |
|
| 178 | 0 | if (value.startsWith("-")) { |
| 179 | 0 | negative = true; |
| 180 | 0 | index++; |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | 0 | if (value.startsWith("0x", index) || value.startsWith("0X", index)) { |
| 185 | 0 | index += 2; |
| 186 | 0 | radix = 16; |
| 187 | 0 | } else if (value.startsWith("#", index)) { |
| 188 | 0 | index++; |
| 189 | 0 | radix = 16; |
| 190 | 0 | } else if (value.startsWith("0", index) && value.length() > 1 + index) { |
| 191 | 0 | index++; |
| 192 | 0 | radix = 8; |
| 193 | |
} |
| 194 | |
|
| 195 | 0 | BigInteger result = new BigInteger(value.substring(index), radix); |
| 196 | 0 | return (negative ? result.negate() : result); |
| 197 | |
} |
| 198 | |
|
| 199 | |
} |