]> granicus.if.org Git - icu/commitdiff
ICU-10796 Runtime binding of LocaleDisplayNamesImpl. Refactored some methods in other...
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 4 Jun 2014 19:23:03 +0000 (19:23 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 4 Jun 2014 19:23:03 +0000 (19:23 +0000)
X-SVN-Rev: 35805

icu4j/build.xml
icu4j/main/classes/core/src/com/ibm/icu/ICUConfig.properties
icu4j/main/classes/core/src/com/ibm/icu/impl/Grego.java
icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyMetaInfo.java
icu4j/main/classes/core/src/com/ibm/icu/text/LocaleDisplayNames.java
icu4j/main/classes/core/src/com/ibm/icu/util/TimeZone.java
icu4j/main/classes/core/src/com/ibm/icu/util/VersionInfo.java

index 37b4ff572ae4d253472097d94d336c99c5ce9430..9b4790d44ed9aec9af403d718be2bc262c2db7a6 100644 (file)
                 target="${javac.target}"
                 encoding="${java.src.encoding}"
                 debug="on" deprecation="off">
+            <include name="com/ibm/icu/impl/LocaleDisplayNamesImpl.java"/>
             <include name="com/ibm/icu/impl/TimeZoneNames*.java"/>
             <include name="com/ibm/icu/lang/UCharacter.java"/>
             <include name="com/ibm/icu/text/BreakIteratorFactory.java"/>
index 5ce6950f3c2b4255e162225d2a833a2017866a34..171ebed73ea09355496521fb719909325eb7fda8 100644 (file)
@@ -1,6 +1,6 @@
 #*
 #*******************************************************************************
-#* Copyright (C) 2008-2012, International Business Machines Corporation and    *
+#* Copyright (C) 2008-2014, International Business Machines Corporation and    *
 #* others. All Rights Reserved.                                                *
 #*******************************************************************************
 #* This is the properties contains ICU runtime configuration 
@@ -42,3 +42,9 @@ com.ibm.icu.impl.ICUResourceBundle.skipRuntimeLocaleResourceScan = false
 # Time zone names service factory
 # @internal
 # com.ibm.icu.text.TimeZoneNames.Factory.impl = com.ibm.icu.impl.TimeZoneNamesFactoryImpl
+
+#
+# [Internal Use Only]
+# LocaleDisplayNames implementation class
+# @internal
+# com.ibm.icu.text.LocaleDisplayNames.impl = com.ibm.icu.impl.LocaleDisplayNamesImpl
index e5fbaccee88f5710ed117a91b2d37f222a5865da..cd38d9d4f002edafb30d04e303c38b1a85ec407a 100644 (file)
@@ -1,6 +1,6 @@
 /**
  *******************************************************************************
- * Copyright (C) 2003-2008, International Business Machines Corporation and
+ * Copyright (C) 2003-2014, International Business Machines Corporation and
  * others. All Rights Reserved.
  *******************************************************************************
  * Partial port from ICU4C's Grego class in i18n/gregoimp.h.
@@ -16,7 +16,8 @@
 
 package com.ibm.icu.impl;
 
-import com.ibm.icu.util.Calendar;
+import java.util.Locale;
+
 
 /**
  * A utility class providing proleptic Gregorian calendar functions
@@ -105,7 +106,7 @@ public class Grego {
      */
     public static int dayOfWeek(long day) {
         long[] remainder = new long[1];
-        floorDivide(day + Calendar.THURSDAY, 7, remainder);
+        floorDivide(day + 5 /* Calendar.THURSDAY */, 7, remainder);
         int dayOfWeek = (int)remainder[0];
         dayOfWeek = (dayOfWeek == 0) ? 7 : dayOfWeek;
         return dayOfWeek;
@@ -210,4 +211,24 @@ public class Grego {
         }
         return weekInMonth;
     }
+
+    /**
+     * Convenient method for formatting time to ISO 8601 style
+     * date string.
+     * @param time long time
+     * @return ISO-8601 date string
+     */
+    public static String timeToString(long time) {
+        int[] fields = timeToFields(time, null);
+        int millis = fields[5];
+        int hour = millis / MILLIS_PER_HOUR;
+        millis = millis % MILLIS_PER_HOUR;
+        int min = millis / MILLIS_PER_MINUTE;
+        millis = millis % MILLIS_PER_MINUTE;
+        int sec = millis / MILLIS_PER_SECOND;
+        millis = millis % MILLIS_PER_SECOND;
+
+        return String.format((Locale)null, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
+                fields[0], fields[1] + 1, fields[2], hour, min, sec, millis);
+    }
 }
index efe8a040216b3b32819289a3e232f3ad44e21e91..bbb3b48384b808a2bcdd04dcb2998ad937c4619d 100644 (file)
@@ -11,9 +11,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
-import com.ibm.icu.util.Calendar;
-import com.ibm.icu.util.GregorianCalendar;
-import com.ibm.icu.util.TimeZone;
+import com.ibm.icu.impl.Grego;
 
 /**
  * Provides information about currencies that is not specific to a locale.
@@ -567,11 +565,7 @@ public class CurrencyMetaInfo {
         if (date == Long.MAX_VALUE || date == Long.MIN_VALUE) {
             return null;
         }
-        GregorianCalendar gc = new GregorianCalendar();
-        gc.setTimeZone(TimeZone.getTimeZone("GMT"));
-        gc.setTimeInMillis(date);
-        return "" + gc.get(Calendar.YEAR) + '-' + (gc.get(Calendar.MONTH) + 1) + '-' +
-                gc.get(Calendar.DAY_OF_MONTH);
+        return Grego.timeToString(date);
     }
 
     private static String debugString(Object o) {
index 7c53b2a68efa39fd84470df7a70b706c8b0c1c84..369346f4bb0a9558e9d7f047bc8b3bd57082e7a9 100644 (file)
@@ -6,9 +6,13 @@
  */
 package com.ibm.icu.text;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Locale;
 
-import com.ibm.icu.impl.LocaleDisplayNamesImpl;
+import com.ibm.icu.impl.ICUConfig;
+import com.ibm.icu.lang.UScript;
+import com.ibm.icu.text.DisplayContext.Type;
 import com.ibm.icu.util.ULocale;
 
 /**
@@ -58,7 +62,21 @@ public abstract class LocaleDisplayNames {
      * @stable ICU 4.4
      */
     public static LocaleDisplayNames getInstance(ULocale locale, DialectHandling dialectHandling) {
-        return LocaleDisplayNamesImpl.getInstance(locale, dialectHandling);
+        LocaleDisplayNames result = null;
+        if (FACTORY_DIALECTHANDLING != null) {
+            try {
+                result = (LocaleDisplayNames) FACTORY_DIALECTHANDLING.invoke(null,
+                        locale, dialectHandling);
+            } catch (InvocationTargetException e) {
+                // fall through
+            } catch (IllegalAccessException e) {
+                // fall through
+            }
+        }
+        if (result == null) {
+            result = new LastResortLocaleDisplayNames(locale, dialectHandling);
+        }
+        return result;
     }
 
     /**
@@ -71,7 +89,21 @@ public abstract class LocaleDisplayNames {
      * @stable ICU 51
      */
     public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) {
-        return LocaleDisplayNamesImpl.getInstance(locale, contexts);
+        LocaleDisplayNames result = null;
+        if (FACTORY_DISPLAYCONTEXT != null) {
+            try {
+                result = (LocaleDisplayNames) FACTORY_DISPLAYCONTEXT.invoke(null,
+                        locale, (Object[])contexts);
+            } catch (InvocationTargetException e) {
+                // fall through
+            } catch (IllegalAccessException e) {
+                // fall through
+            }
+        }
+        if (result == null) {
+            result = new LastResortLocaleDisplayNames(locale, contexts);
+        }
+        return result;
     }
 
     // getters for state
@@ -204,4 +236,138 @@ public abstract class LocaleDisplayNames {
     @Deprecated
     protected LocaleDisplayNames() {
     }
+
+    private static final Method FACTORY_DIALECTHANDLING;
+    private static final Method FACTORY_DISPLAYCONTEXT;
+
+    static {
+        String implClassName = ICUConfig.get("com.ibm.icu.text.LocaleDisplayNames.impl", "com.ibm.icu.impl.LocaleDisplayNamesImpl");
+
+        Method factoryDialectHandling = null;
+        Method factoryDisplayContext = null;
+
+        try {
+            Class<?> implClass = Class.forName(implClassName);
+            try {
+                factoryDialectHandling = implClass.getMethod("getInstance",
+                        ULocale.class, DialectHandling.class);
+            } catch (NoSuchMethodException e) {
+            }
+            try {
+                factoryDisplayContext = implClass.getMethod("getInstance",
+                        ULocale.class, DisplayContext[].class);
+            } catch (NoSuchMethodException e) {
+            }
+
+        } catch (ClassNotFoundException e) {
+            // fallback to last resort impl
+        }
+
+        FACTORY_DIALECTHANDLING = factoryDialectHandling;
+        FACTORY_DISPLAYCONTEXT = factoryDisplayContext;
+    }
+
+    /**
+     * Minimum implementation of LocaleDisplayNames
+     */
+    private static class LastResortLocaleDisplayNames extends LocaleDisplayNames {
+
+        private ULocale locale;
+        private DisplayContext[] contexts;
+
+        private LastResortLocaleDisplayNames(ULocale locale, DialectHandling dialectHandling) {
+            this.locale = locale;
+            DisplayContext context = (dialectHandling == DialectHandling.DIALECT_NAMES) ?
+                    DisplayContext.DIALECT_NAMES : DisplayContext.STANDARD_NAMES;
+            this.contexts = new DisplayContext[] {context};
+        }
+
+        private LastResortLocaleDisplayNames(ULocale locale, DisplayContext... contexts) {
+            this.locale = locale;
+            this.contexts = new DisplayContext[contexts.length];
+            System.arraycopy(contexts, 0, this.contexts, 0, contexts.length);
+        }
+
+        @Override
+        public ULocale getLocale() {
+            return locale;
+        }
+
+        @Override
+        public DialectHandling getDialectHandling() {
+            DialectHandling result = DialectHandling.STANDARD_NAMES;
+            for (DisplayContext context : contexts) {
+                if (context.type() == DisplayContext.Type.DIALECT_HANDLING) {
+                    if (context.value() == DisplayContext.DIALECT_NAMES.ordinal()) {
+                        result = DialectHandling.DIALECT_NAMES;
+                        break;
+                    }
+                }
+            }
+            return result;
+        }
+
+        @Override
+        public DisplayContext getContext(Type type) {
+            DisplayContext result = DisplayContext.STANDARD_NAMES;  // final fallback
+            for (DisplayContext context : contexts) {
+                if (context.type() == type) {
+                    result = context;
+                    break;
+                }
+            }
+            return result;
+        }
+
+        @Override
+        public String localeDisplayName(ULocale locale) {
+            return locale.getName();
+        }
+
+        @Override
+        public String localeDisplayName(Locale locale) {
+            return ULocale.forLocale(locale).getName();
+        }
+
+        @Override
+        public String localeDisplayName(String localeId) {
+            return new ULocale(localeId).getName();
+        }
+
+        @Override
+        public String languageDisplayName(String lang) {
+            return lang;
+        }
+
+        @Override
+        public String scriptDisplayName(String script) {
+            return script;
+        }
+
+        @Override
+        public String scriptDisplayName(int scriptCode) {
+            return UScript.getShortName(scriptCode);
+        }
+
+        @Override
+        public String regionDisplayName(String region) {
+            return region;
+        }
+
+        @Override
+        public String variantDisplayName(String variant) {
+            return variant;
+        }
+
+        @Override
+        public String keyDisplayName(String key) {
+            return key;
+        }
+
+        @Override
+        public String keyValueDisplayName(String key, String value) {
+            return value;
+        }
+        
+    }
 }
index f3186a4076e50862a39cd72fbcbe2a9eacae9e04..527980f7a54246536a59dc5b9974458e57ffaa08 100644 (file)
@@ -1031,13 +1031,9 @@ abstract public class TimeZone implements Serializable, Cloneable, Freezable<Tim
      *
      * @stable ICU 3.8
      */
-    public static synchronized String getTZDataVersion() {
-        if (TZDATA_VERSION == null) {
-            UResourceBundle tzbundle = UResourceBundle.getBundleInstance(
-                    "com/ibm/icu/impl/data/icudt" + VersionInfo.ICU_DATA_VERSION_PATH, "zoneinfo64");
-            TZDATA_VERSION = tzbundle.getString("TZVersion");
-        }
-        return TZDATA_VERSION;
+    public static String getTZDataVersion() {
+        // The implementation had been moved to VersionInfo.
+        return VersionInfo.getTZDataVersion();
     }
 
     /**
@@ -1282,11 +1278,6 @@ abstract public class TimeZone implements Serializable, Cloneable, Freezable<Tim
      */
     private static volatile TimeZone  defaultZone = null;
 
-    /**
-     * The tzdata version
-     */
-    private static String TZDATA_VERSION = null;
-
     /**
      * TimeZone implementation type
      */
index 3a5da3ac4f851225f89d20499768fd1704387005..a69bf8191a4d9dad2ab94133e4f9de3d7cd8f256 100644 (file)
@@ -592,7 +592,7 @@ public final class VersionInfo implements Comparable<VersionInfo>
         System.out.println("Implementation Version: " + ICU_VERSION.getVersionString(2, 4));
         System.out.println("Unicode Data Version:   " + UNICODE_VERSION.getVersionString(2, 4));
         System.out.println("CLDR Data Version:      " + LocaleData.getCLDRVersion().getVersionString(2, 4));
-        System.out.println("Time Zone Data Version: " + TimeZone.getTZDataVersion());
+        System.out.println("Time Zone Data Version: " + getTZDataVersion());
     }
 
     /**
@@ -633,4 +633,21 @@ public final class VersionInfo implements Comparable<VersionInfo>
         return verStr.toString();
     }
     ///CLOVER:ON
+
+
+    // Moved from TimeZone class
+    private static volatile String TZDATA_VERSION = null;
+
+    static String getTZDataVersion() {
+        if (TZDATA_VERSION == null) {
+            synchronized (VersionInfo.class) {
+                if (TZDATA_VERSION == null) {
+                    UResourceBundle tzbundle = UResourceBundle.getBundleInstance("com/ibm/icu/impl/data/icudt"
+                            + VersionInfo.ICU_DATA_VERSION_PATH, "zoneinfo64");
+                    TZDATA_VERSION = tzbundle.getString("TZVersion");
+                }
+            }
+        }
+        return TZDATA_VERSION;
+    }
 }