jdk/src/share/classes/java/time/format/DateTimeFormatStyleProvider.java
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
equal deleted inserted replaced
15657:c588664d547e 15658:55b829ca2334
    59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    61  */
    61  */
    62 package java.time.format;
    62 package java.time.format;
    63 
    63 
    64 import java.text.DateFormat;
       
    65 import java.text.SimpleDateFormat;
    64 import java.text.SimpleDateFormat;
    66 import java.time.temporal.Chrono;
    65 import java.time.chrono.Chronology;
    67 import java.util.Locale;
    66 import java.util.Locale;
    68 import java.util.concurrent.ConcurrentHashMap;
    67 import java.util.concurrent.ConcurrentHashMap;
    69 import java.util.concurrent.ConcurrentMap;
    68 import java.util.concurrent.ConcurrentMap;
       
    69 import sun.util.locale.provider.LocaleProviderAdapter;
       
    70 import sun.util.locale.provider.LocaleResources;
    70 
    71 
    71 /**
    72 /**
    72  * A provider to obtain date-time formatters for a style.
    73  * A provider to obtain date-time formatters for a style.
    73  * <p>
    74  * <p>
    74  *
    75  *
   108      * @param locale  the locale to use, not null
   109      * @param locale  the locale to use, not null
   109      * @return the date-time formatter, not null
   110      * @return the date-time formatter, not null
   110      * @throws IllegalArgumentException if both format styles are null or if the locale is not recognized
   111      * @throws IllegalArgumentException if both format styles are null or if the locale is not recognized
   111      */
   112      */
   112     public DateTimeFormatter getFormatter(
   113     public DateTimeFormatter getFormatter(
   113             FormatStyle dateStyle, FormatStyle timeStyle, Chrono<?> chrono, Locale locale) {
   114             FormatStyle dateStyle, FormatStyle timeStyle, Chronology chrono, Locale locale) {
   114         if (dateStyle == null && timeStyle == null) {
   115         if (dateStyle == null && timeStyle == null) {
   115             throw new IllegalArgumentException("Date and Time style must not both be null");
   116             throw new IllegalArgumentException("Date and Time style must not both be null");
   116         }
   117         }
   117         String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle;
   118         String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle;
   118         Object cached = FORMATTER_CACHE.get(key);
   119         Object cached = FORMATTER_CACHE.get(key);
   119         if (cached != null) {
   120         if (cached != null) {
   120             if (cached.equals("")) {
       
   121                 throw new IllegalArgumentException("Unable to convert DateFormat to DateTimeFormatter");
       
   122             }
       
   123             return (DateTimeFormatter) cached;
   121             return (DateTimeFormatter) cached;
   124         }
   122         }
   125         DateFormat dateFormat;
   123 
   126         if (dateStyle != null) {
   124         LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
   127             if (timeStyle != null) {
   125                                     .getLocaleResources(locale);
   128                 dateFormat = DateFormat.getDateTimeInstance(convertStyle(dateStyle), convertStyle(timeStyle), locale);
   126         String pattern = lr.getCldrDateTimePattern(convertStyle(timeStyle), convertStyle(dateStyle),
   129             } else {
   127                                                    chrono.getCalendarType());
   130                 dateFormat = DateFormat.getDateInstance(convertStyle(dateStyle), locale);
   128         DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale);
   131             }
   129         FORMATTER_CACHE.putIfAbsent(key, formatter);
   132         } else {
   130         return formatter;
   133             dateFormat = DateFormat.getTimeInstance(convertStyle(timeStyle), locale);
       
   134         }
       
   135         if (dateFormat instanceof SimpleDateFormat) {
       
   136             String pattern = ((SimpleDateFormat) dateFormat).toPattern();
       
   137             DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale);
       
   138             FORMATTER_CACHE.putIfAbsent(key, formatter);
       
   139             return formatter;
       
   140         }
       
   141         FORMATTER_CACHE.putIfAbsent(key, "");
       
   142         throw new IllegalArgumentException("Unable to convert DateFormat to DateTimeFormatter");
       
   143     }
   131     }
   144 
   132 
   145     /**
   133     /**
   146      * Converts the enum style to the old format style.
   134      * Converts the enum style to the java.util.Calendar style. Standalone styles
   147      * @param style  the enum style, not null
   135      * are not supported.
   148      * @return the int style
   136      *
       
   137      * @param style  the enum style
       
   138      * @return the int style, or -1 if style is null, indicating unrequired
   149      */
   139      */
   150     private int convertStyle(FormatStyle style) {
   140     private int convertStyle(FormatStyle style) {
       
   141         if (style == null) {
       
   142             return -1;
       
   143         }
   151         return style.ordinal();  // indices happen to align
   144         return style.ordinal();  // indices happen to align
   152     }
   145     }
   153 
   146 
   154 }
   147 }