jdk/src/share/classes/java/text/DateFormatSymbols.java
changeset 13583 dc0017b1a452
parent 12549 db2c42dabfa4
child 14172 fad44417edf8
--- a/jdk/src/share/classes/java/text/DateFormatSymbols.java	Tue Aug 21 13:42:08 2012 +0100
+++ b/jdk/src/share/classes/java/text/DateFormatSymbols.java	Tue Aug 21 11:00:30 2012 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,17 +44,14 @@
 import java.lang.ref.SoftReference;
 import java.text.spi.DateFormatSymbolsProvider;
 import java.util.Arrays;
-import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
 import java.util.TimeZone;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.spi.LocaleServiceProvider;
-import sun.util.LocaleServiceProviderPool;
-import sun.util.TimeZoneNameUtility;
-import sun.util.calendar.ZoneInfo;
-import sun.util.resources.LocaleData;
+import sun.util.locale.provider.LocaleProviderAdapter;
+import sun.util.locale.provider.LocaleServiceProviderPool;
+import sun.util.locale.provider.TimeZoneNameUtility;
 
 /**
  * <code>DateFormatSymbols</code> is a public class for encapsulating
@@ -227,7 +224,7 @@
      * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
      * All locales use the same these unlocalized pattern characters.
      */
-    static final String  patternChars = "GyMdkHmsSEDFwWahKzZYuX";
+    static final String  patternChars = "GyMdkHmsSEDFwWahKzZYuXL";
 
     static final int PATTERN_ERA                  =  0; // G
     static final int PATTERN_YEAR                 =  1; // y
@@ -251,6 +248,7 @@
     static final int PATTERN_WEEK_YEAR            = 19; // Y
     static final int PATTERN_ISO_DAY_OF_WEEK      = 20; // u
     static final int PATTERN_ISO_ZONE             = 21; // X
+    static final int PATTERN_MONTH_STANDALONE     = 22; // L
 
     /**
      * Localized date-time pattern characters. For example, a locale may
@@ -326,7 +324,7 @@
         if (dfs != null) {
             return dfs;
         }
-        return (DateFormatSymbols) getCachedInstance(locale).clone();
+        throw new RuntimeException("DateFormatSymbols instance creation failed.");
     }
 
     /**
@@ -340,46 +338,18 @@
         if (dfs != null) {
             return dfs;
         }
-        return getCachedInstance(locale);
+        throw new RuntimeException("DateFormatSymbols instance creation failed.");
     }
 
     private static DateFormatSymbols getProviderInstance(Locale locale) {
-        DateFormatSymbols providersInstance = null;
-
-        // Check whether a provider can provide an implementation that's closer
-        // to the requested locale than what the Java runtime itself can provide.
-        LocaleServiceProviderPool pool =
-            LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);
-        if (pool.hasProviders()) {
-            providersInstance = pool.getLocalizedObject(
-                                    DateFormatSymbolsGetter.INSTANCE, locale);
+        LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
+        DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider();
+        DateFormatSymbols dfsyms = provider.getInstance(locale);
+        if (dfsyms == null) {
+            provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider();
+            dfsyms = provider.getInstance(locale);
         }
-        return providersInstance;
-    }
-
-    /**
-     * Returns a cached DateFormatSymbols if it's found in the
-     * cache. Otherwise, this method returns a newly cached instance
-     * for the given locale.
-     */
-    private static DateFormatSymbols getCachedInstance(Locale locale) {
-        SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
-        DateFormatSymbols dfs = null;
-        if (ref == null || (dfs = ref.get()) == null) {
-            dfs = new DateFormatSymbols(locale);
-            ref = new SoftReference<DateFormatSymbols>(dfs);
-            SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
-            if (x != null) {
-                DateFormatSymbols y = x.get();
-                if (y != null) {
-                    dfs = y;
-                } else {
-                    // Replace the empty SoftReference with ref.
-                    cachedInstances.put(locale, ref);
-                }
-            }
-        }
-        return dfs;
+        return dfsyms;
     }
 
     /**
@@ -400,6 +370,17 @@
 
     /**
      * Gets month strings. For example: "January", "February", etc.
+     *
+     * <p>If the language requires different forms for formatting and
+     * stand-alone usages, this method returns month names in the
+     * formatting form. For example, the preferred month name for
+     * January in the Czech language is <em>ledna</em> in the
+     * formatting form, while it is <em>leden</em> in the stand-alone
+     * form. This method returns {@code "ledna"} in this case. Refer
+     * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
+     * Calendar Elements in the Unicode Locale Data Markup Language
+     * (LDML) specification</a> for more details.
+     *
      * @return the month strings.
      */
     public String[] getMonths() {
@@ -416,6 +397,17 @@
 
     /**
      * Gets short month strings. For example: "Jan", "Feb", etc.
+     *
+     * <p>If the language requires different forms for formatting and
+     * stand-alone usages, This method returns short month names in
+     * the formatting form. For example, the preferred abbreviation
+     * for January in the Catalan language is <em>de gen.</em> in the
+     * formatting form, while it is <em>gen.</em> in the stand-alone
+     * form. This method returns {@code "de gen."} in this case. Refer
+     * to the <a href="http://unicode.org/reports/tr35/#Calendar_Elements">
+     * Calendar Elements in the Unicode Locale Data Markup Language
+     * (LDML) specification</a> for more details.
+     *
      * @return the short month strings.
      */
     public String[] getShortMonths() {
@@ -645,7 +637,7 @@
      * Cache to hold DateFormatSymbols instances per Locale.
      */
     private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
-        = new ConcurrentHashMap<Locale, SoftReference<DateFormatSymbols>>(3);
+        = new ConcurrentHashMap<>(3);
 
     private transient int lastZoneIndex = 0;
 
@@ -661,7 +653,15 @@
         }
 
         // Initialize the fields from the ResourceBundle for locale.
-        ResourceBundle resource = LocaleData.getDateFormatData(locale);
+        LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
+        // Avoid any potential recursions
+        switch (adapter.getAdapterType()) {
+        case HOST:
+        case SPI:
+            adapter = LocaleProviderAdapter.getResourceBundleBased();
+            break;
+        }
+        ResourceBundle resource = adapter.getLocaleData().getDateFormatData(locale);
 
         eras = resource.getStringArray("Eras");
         months = resource.getStringArray("MonthNames");
@@ -672,6 +672,17 @@
         // Day of week names are stored in a 1-based array.
         weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
         shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
+
+        // Put a clone in the cache
+        ref = new SoftReference<>((DateFormatSymbols)this.clone());
+        SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
+        if (x != null) {
+            DateFormatSymbols y = x.get();
+            if (y == null) {
+                // Replace the empty SoftReference with ref.
+                cachedInstances.put(locale, ref);
+            }
+        }
     }
 
     private static String[] toOneBasedArray(String[] src) {
@@ -730,7 +741,7 @@
         }
     }
 
-    private final String[][] getZoneStringsImpl(boolean needsCopy) {
+    private String[][] getZoneStringsImpl(boolean needsCopy) {
         if (zoneStrings == null) {
             zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
         }
@@ -747,7 +758,7 @@
         return aCopy;
     }
 
-    private final boolean isSubclassObject() {
+    private boolean isSubclassObject() {
         return !getClass().getName().equals("java.text.DateFormatSymbols");
     }
 
@@ -757,7 +768,7 @@
      * @param src the source DateFormatSymbols.
      * @param dst the target DateFormatSymbols.
      */
-    private final void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
+    private void copyMembers(DateFormatSymbols src, DateFormatSymbols dst)
     {
         dst.eras = Arrays.copyOf(src.eras, src.eras.length);
         dst.months = Arrays.copyOf(src.months, src.months.length);
@@ -786,23 +797,4 @@
         }
         stream.defaultWriteObject();
     }
-
-    /**
-     * Obtains a DateFormatSymbols instance from a DateFormatSymbolsProvider
-     * implementation.
-     */
-    private static class DateFormatSymbolsGetter
-        implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatSymbolsProvider,
-                                                                   DateFormatSymbols> {
-        private static final DateFormatSymbolsGetter INSTANCE =
-            new DateFormatSymbolsGetter();
-
-        public DateFormatSymbols getObject(DateFormatSymbolsProvider dateFormatSymbolsProvider,
-                                Locale locale,
-                                String key,
-                                Object... params) {
-            assert params.length == 0;
-            return dateFormatSymbolsProvider.getInstance(locale);
-        }
-    }
 }