1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file:
  31  *
  32  * Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos
  33  *
  34  * All rights reserved.
  35  *
  36  * Redistribution and use in source and binary forms, with or without
  37  * modification, are permitted provided that the following conditions are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright notice,
  40  *    this list of conditions and the following disclaimer.
  41  *
  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  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.
  61  */
  62 package java.time.format;
  63 
  64 import java.text.DateFormat;
  65 import java.text.SimpleDateFormat;
  66 import java.time.temporal.Chrono;
  67 import java.util.Locale;
  68 import java.util.concurrent.ConcurrentHashMap;
  69 import java.util.concurrent.ConcurrentMap;
  70 
  71 /**
  72  * A provider to obtain date-time formatters for a style.
  73  * <p>
  74  *
  75  * <h3>Specification for implementors</h3>
  76  * This implementation is based on extraction of data from a {@link SimpleDateFormat}.
  77  * This class is immutable and thread-safe.
  78  * This Implementations caches the returned formatters.
  79  *
  80  * @since 1.8
  81  */
  82 final class DateTimeFormatStyleProvider {
  83     // TODO: Better implementation based on CLDR
  84 
  85     /** Cache of formatters. */
  86     private static final ConcurrentMap<String, Object> FORMATTER_CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);
  87 
  88     private DateTimeFormatStyleProvider() {}
  89 
  90     /**
  91      * Gets an Instance of the provider of format styles.
  92      *
  93      * @return the provider, not null
  94      */
  95     static DateTimeFormatStyleProvider getInstance() {
  96         return new DateTimeFormatStyleProvider();
  97     }
  98 
  99     /**
 100      * Gets a localized date, time or date-time formatter.
 101      * <p>
 102      * The formatter will be the most appropriate to use for the date and time style in the locale.
 103      * For example, some locales will use the month name while others will use the number.
 104      *
 105      * @param dateStyle  the date formatter style to obtain, null to obtain a time formatter
 106      * @param timeStyle  the time formatter style to obtain, null to obtain a date formatter
 107      * @param chrono  the chronology to use, not null
 108      * @param locale  the locale to use, not null
 109      * @return the date-time formatter, not null
 110      * @throws IllegalArgumentException if both format styles are null or if the locale is not recognized
 111      */
 112     public DateTimeFormatter getFormatter(
 113             FormatStyle dateStyle, FormatStyle timeStyle, Chrono<?> chrono, Locale locale) {
 114         if (dateStyle == null && timeStyle == null) {
 115             throw new IllegalArgumentException("Date and Time style must not both be null");
 116         }
 117         String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle;
 118         Object cached = FORMATTER_CACHE.get(key);
 119         if (cached != null) {
 120             if (cached.equals("")) {
 121                 throw new IllegalArgumentException("Unable to convert DateFormat to DateTimeFormatter");
 122             }
 123             return (DateTimeFormatter) cached;
 124         }
 125         DateFormat dateFormat;
 126         if (dateStyle != null) {
 127             if (timeStyle != null) {
 128                 dateFormat = DateFormat.getDateTimeInstance(convertStyle(dateStyle), convertStyle(timeStyle), locale);
 129             } else {
 130                 dateFormat = DateFormat.getDateInstance(convertStyle(dateStyle), locale);
 131             }
 132         } else {
 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     }
 144 
 145     /**
 146      * Converts the enum style to the old format style.
 147      * @param style  the enum style, not null
 148      * @return the int style
 149      */
 150     private int convertStyle(FormatStyle style) {
 151         return style.ordinal();  // indices happen to align
 152     }
 153 
 154 }