test/java/util/PluggableLocale/providersrc/LocaleNameProviderImpl.java

Print this page
rev 5957 : imported patch 8000245.8000273.8000615

@@ -35,59 +35,66 @@
 public class LocaleNameProviderImpl extends LocaleNameProvider {
     static Locale[] avail = {Locale.JAPANESE,
                              Locale.JAPAN,
                              new Locale("ja", "JP", "osaka"),
                              new Locale("ja", "JP", "kyoto"),
-                             new Locale("xx")};
+                             new Locale("xx"),
+                             new Locale("yy", "YY", "YYYY")};
     static List<Locale> availList = Arrays.asList(avail);
     public Locale[] getAvailableLocales() {
         return avail;
     }
 
+    @Override
     public String getDisplayLanguage(String lang, Locale target) {
-        if (!Utils.supportsLocale(availList, target)) {
-            throw new IllegalArgumentException("locale is not supported: "+target);
+        return getDisplayString(lang, target);
         }
 
-        String ret = null;
-
-        try {
-            ResourceBundle rb = ResourceBundle.getBundle("com.bar.LocaleNames", target);
-            ret = rb.getString(lang);
-        } catch (MissingResourceException mre) {
+    @Override
+    public String getDisplayCountry(String ctry, Locale target) {
+        return getDisplayString(ctry, target);
         }
 
-        return ret;
+    @Override
+    public String getDisplayVariant(String vrnt, Locale target) {
+        return getDisplayString(vrnt, target);
     }
 
-    public String getDisplayCountry(String ctry, Locale target) {
+    private String getDisplayString(String key, Locale target) {
         if (!Utils.supportsLocale(availList, target)) {
             throw new IllegalArgumentException("locale is not supported: "+target);
         }
 
         String ret = null;
 
-        try {
-            ResourceBundle rb = ResourceBundle.getBundle("LocaleNames", target);
-            ret = rb.getString(ctry);
-        } catch (MissingResourceException mre) {
-        }
+        if (target.getLanguage().equals("yy") &&
+            target.getCountry().equals("YY")) {
+            String vrnt = target.getVariant();
+            if (vrnt.startsWith("YYYY")) {
+                switch (key) {
+                    case "yy":
+                    case "YY":
+                        ret = "waiwai";
+                        break;
 
-        return ret;
+                    case "YYYY":
+                        if (vrnt.equals("YYYY_suffix")) {
+                            // for LocaleNameProviderTest.variantFallbackTest()
+                            throw new RuntimeException(vrnt);
+                        } else {
+                            ret = "waiwai";
     }
-
-    public String getDisplayVariant(String vrnt, Locale target) {
-        if (!Utils.supportsLocale(availList, target)) {
-            throw new IllegalArgumentException("locale is not supported: "+target);
+                        break;
         }
-
-        String ret = null;
-
+            }
+        } else {
+            // resource bundle based (allows fallback)
         try {
-            ResourceBundle rb = ResourceBundle.getBundle("LocaleNames", target);
-            ret = rb.getString(vrnt);
+                ResourceBundle rb = ResourceBundle.getBundle("com.bar.LocaleNames", target);
+                ret = rb.getString(key);
         } catch (MissingResourceException mre) {
         }
+        }
 
         return ret;
     }
 }