Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of string utilities.
Definition at line 17 of file NumberUtils.java.
Static Public Member Functions | |
| static Number | convertNumberToTargetClass (Number number, Class targetClass) throws IllegalArgumentException |
| Convert the given number into an instance of the given target class. | |
| static Number | parseNumber (String text, Class targetClass) |
Parse the given text into a number instance of the given target class, using the corresponding default decode methods. | |
| static Number | parseNumber (String text, Class targetClass, NumberFormat numberFormat) |
| Parse the given text into a number instance of the given target class, using the given NumberFormat. | |
| static Number liquibase.util.NumberUtils.convertNumberToTargetClass | ( | Number | number, | |
| Class | targetClass | |||
| ) | throws IllegalArgumentException [static] |
Convert the given number into an instance of the given target class.
| number | the number to convert | |
| targetClass | the target class to convert to |
| IllegalArgumentException | if the target class is not supported (i.e. not a standard Number subclass as included in the JDK) |
java.lang.Short
java.lang.Integer
java.lang.Long
java.math.BigInteger
java.lang.Float
java.lang.Double
java.math.BigDecimal
Definition at line 36 of file NumberUtils.java.
Referenced by liquibase.util.NumberUtils.parseNumber().
00037 { 00038 00039 if (targetClass.isInstance(number)) { 00040 return number; 00041 } else if (targetClass.equals(Byte.class)) { 00042 long value = number.longValue(); 00043 if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { 00044 raiseOverflowException(number, targetClass); 00045 } 00046 return number.byteValue(); 00047 } else if (targetClass.equals(Short.class)) { 00048 long value = number.longValue(); 00049 if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { 00050 raiseOverflowException(number, targetClass); 00051 } 00052 return number.shortValue(); 00053 } else if (targetClass.equals(Integer.class)) { 00054 long value = number.longValue(); 00055 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { 00056 raiseOverflowException(number, targetClass); 00057 } 00058 return number.intValue(); 00059 } else if (targetClass.equals(Long.class)) { 00060 return number.longValue(); 00061 } else if (targetClass.equals(Float.class)) { 00062 return number.floatValue(); 00063 } else if (targetClass.equals(Double.class)) { 00064 return number.doubleValue(); 00065 } else if (targetClass.equals(BigInteger.class)) { 00066 return BigInteger.valueOf(number.longValue()); 00067 } else if (targetClass.equals(BigDecimal.class)) { 00068 // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) 00069 // (see BigDecimal javadoc for details) 00070 return new BigDecimal(number.toString()); 00071 } else { 00072 throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + 00073 number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); 00074 } 00075 }
| static Number liquibase.util.NumberUtils.parseNumber | ( | String | text, | |
| Class | targetClass | |||
| ) | [static] |
Parse the given text into a number instance of the given target class, using the corresponding default decode methods.
Trims the input String before attempting to parse the number. Supports numbers in hex format (with leading 0x) and in octal format (with leading 0).
| text | the text to convert | |
| targetClass | the target class to parse into |
| IllegalArgumentException | if the target class is not supported (i.e. not a standard Number subclass as included in the JDK) |
java.lang.Short.decode
java.lang.Integer.decode
java.lang.Long.decode
decodeBigInteger(String)
java.lang.Float.valueOf
java.lang.Double.valueOf
java.math.BigDecimal.BigDecimal(String)
Definition at line 108 of file NumberUtils.java.
Referenced by liquibase.util.NumberUtils.parseNumber().
00108 { 00109 String trimmed = text.trim(); 00110 00111 if (targetClass.equals(Byte.class)) { 00112 return Byte.decode(trimmed); 00113 } else if (targetClass.equals(Short.class)) { 00114 return Short.decode(trimmed); 00115 } else if (targetClass.equals(Integer.class)) { 00116 return Integer.decode(trimmed); 00117 } else if (targetClass.equals(Long.class)) { 00118 return Long.decode(trimmed); 00119 } else if (targetClass.equals(BigInteger.class)) { 00120 return decodeBigInteger(trimmed); 00121 } else if (targetClass.equals(Float.class)) { 00122 return Float.valueOf(trimmed); 00123 } else if (targetClass.equals(Double.class)) { 00124 return Double.valueOf(trimmed); 00125 } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) { 00126 return new BigDecimal(trimmed); 00127 } else { 00128 throw new IllegalArgumentException( 00129 "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]"); 00130 } 00131 }
| static Number liquibase.util.NumberUtils.parseNumber | ( | String | text, | |
| Class | targetClass, | |||
| NumberFormat | numberFormat | |||
| ) | [static] |
Parse the given text into a number instance of the given target class, using the given NumberFormat.
Trims the input String before attempting to parse the number.
| text | the text to convert | |
| targetClass | the target class to parse into | |
| numberFormat | the NumberFormat to use for parsing (if null, this method falls back to parseNumber(String, Class)) |
| IllegalArgumentException | if the target class is not supported (i.e. not a standard Number subclass as included in the JDK) |
Definition at line 149 of file NumberUtils.java.
References liquibase.util.NumberUtils.convertNumberToTargetClass(), and liquibase.util.NumberUtils.parseNumber().
00149 { 00150 if (numberFormat != null) { 00151 try { 00152 Number number = numberFormat.parse(text.trim()); 00153 return convertNumberToTargetClass(number, targetClass); 00154 } 00155 catch (ParseException ex) { 00156 throw new IllegalArgumentException(ex.getMessage()); 00157 } 00158 } else { 00159 return parseNumber(text, targetClass); 00160 } 00161 }