account for unavailability of NumberFormat.getIntegerInstance(...) pre JDK 1.4
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@600605 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b952b75526
commit
686f2f611c
|
@ -16,6 +16,9 @@
|
|||
*/
|
||||
package org.apache.commons.lang.text;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.FieldPosition;
|
||||
|
@ -39,6 +42,20 @@ public class NumberMetaFormat extends MetaFormatSupport {
|
|||
private static final String INTEGER = "integer";
|
||||
private static final String CURRENCY = "currency";
|
||||
private static final String PERCENT = "percent";
|
||||
private static final Method GET_INTEGER_INSTANCE;
|
||||
|
||||
static {
|
||||
Method m = null;
|
||||
try {
|
||||
Method mm = NumberFormat.class.getDeclaredMethod("getIntegerInstance", new Class[] { Locale.class });
|
||||
if (Modifier.isStatic(mm.getModifiers())) {
|
||||
m = mm;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// leave null
|
||||
}
|
||||
GET_INTEGER_INSTANCE = m;
|
||||
}
|
||||
|
||||
private Locale locale;
|
||||
|
||||
|
@ -116,8 +133,7 @@ public class NumberMetaFormat extends MetaFormatSupport {
|
|||
if (subformats == null) {
|
||||
subformats = new HashMap();
|
||||
subformats.put(DEFAULT, NumberFormat.getInstance(getLocale()));
|
||||
subformats.put(INTEGER, NumberFormat
|
||||
.getIntegerInstance(getLocale()));
|
||||
subformats.put(INTEGER, createIntegerInstance(getLocale()));
|
||||
subformats.put(CURRENCY, NumberFormat
|
||||
.getCurrencyInstance(getLocale()));
|
||||
subformats.put(PERCENT, NumberFormat
|
||||
|
@ -127,4 +143,26 @@ public class NumberMetaFormat extends MetaFormatSupport {
|
|||
decimalFormatSymbols = new DecimalFormatSymbols(getLocale());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the "integer" NumberFormat instance for the specified Locale.
|
||||
*
|
||||
* @param locale the Locale to use
|
||||
* @return integer NumberFormat
|
||||
*/
|
||||
private static NumberFormat createIntegerInstance(Locale locale) {
|
||||
if (GET_INTEGER_INSTANCE != null) {
|
||||
try {
|
||||
return (NumberFormat) GET_INTEGER_INSTANCE.invoke(null, new Object[] { locale });
|
||||
} catch (IllegalAccessException e) {
|
||||
//fall through
|
||||
} catch (InvocationTargetException e) {
|
||||
//fall through
|
||||
}
|
||||
}
|
||||
NumberFormat result = NumberFormat.getInstance(locale);
|
||||
result.setMaximumFractionDigits(0);
|
||||
result.setParseIntegerOnly(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue