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 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 } | 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.SimpleDateFormat; 65 import java.time.chrono.Chronology; 66 import java.util.Locale; 67 import java.util.concurrent.ConcurrentHashMap; 68 import java.util.concurrent.ConcurrentMap; 69 import sun.util.locale.provider.LocaleProviderAdapter; 70 import sun.util.locale.provider.LocaleResources; 71 72 /** 73 * A provider to obtain date-time formatters for a style. 74 * <p> 75 * 76 * <h3>Specification for implementors</h3> 77 * This implementation is based on extraction of data from a {@link SimpleDateFormat}. 78 * This class is immutable and thread-safe. 79 * This Implementations caches the returned formatters. 80 * 81 * @since 1.8 82 */ 83 final class DateTimeFormatStyleProvider { 84 // TODO: Better implementation based on CLDR 85 86 /** Cache of formatters. */ 87 private static final ConcurrentMap<String, Object> FORMATTER_CACHE = new ConcurrentHashMap<>(16, 0.75f, 2); 88 89 private DateTimeFormatStyleProvider() {} 90 94 * @return the provider, not null 95 */ 96 static DateTimeFormatStyleProvider getInstance() { 97 return new DateTimeFormatStyleProvider(); 98 } 99 100 /** 101 * Gets a localized date, time or date-time formatter. 102 * <p> 103 * The formatter will be the most appropriate to use for the date and time style in the locale. 104 * For example, some locales will use the month name while others will use the number. 105 * 106 * @param dateStyle the date formatter style to obtain, null to obtain a time formatter 107 * @param timeStyle the time formatter style to obtain, null to obtain a date formatter 108 * @param chrono the chronology to use, not null 109 * @param locale the locale to use, not null 110 * @return the date-time formatter, not null 111 * @throws IllegalArgumentException if both format styles are null or if the locale is not recognized 112 */ 113 public DateTimeFormatter getFormatter( 114 FormatStyle dateStyle, FormatStyle timeStyle, Chronology chrono, Locale locale) { 115 if (dateStyle == null && timeStyle == null) { 116 throw new IllegalArgumentException("Date and Time style must not both be null"); 117 } 118 String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle; 119 Object cached = FORMATTER_CACHE.get(key); 120 if (cached != null) { 121 return (DateTimeFormatter) cached; 122 } 123 124 LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased() 125 .getLocaleResources(locale); 126 String pattern = lr.getCldrDateTimePattern(convertStyle(timeStyle), convertStyle(dateStyle), 127 chrono.getCalendarType()); 128 DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale); 129 FORMATTER_CACHE.putIfAbsent(key, formatter); 130 return formatter; 131 } 132 133 /** 134 * Converts the enum style to the java.util.Calendar style. Standalone styles 135 * are not supported. 136 * 137 * @param style the enum style 138 * @return the int style, or -1 if style is null, indicating unrequired 139 */ 140 private int convertStyle(FormatStyle style) { 141 if (style == null) { 142 return -1; 143 } 144 return style.ordinal(); // indices happen to align 145 } 146 147 } |