You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2007/12/03 18:20:25 UTC

svn commit: r600605 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/NumberMetaFormat.java

Author: mbenson
Date: Mon Dec  3 09:20:24 2007
New Revision: 600605

URL: http://svn.apache.org/viewvc?rev=600605&view=rev
Log:
account for unavailability of NumberFormat.getIntegerInstance(...) pre JDK 1.4

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/NumberMetaFormat.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/NumberMetaFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/NumberMetaFormat.java?rev=600605&r1=600604&r2=600605&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/NumberMetaFormat.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/NumberMetaFormat.java Mon Dec  3 09:20:24 2007
@@ -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 @@
     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 @@
         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
@@ -126,5 +142,27 @@
             reverseSubformats = invert(subformats);
             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;
     }
 }