1 /* 2 * Copyright (c) 1999, 2014, 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 package sun.util.locale.provider; 27 28 import java.text.DateFormat; 29 import java.text.SimpleDateFormat; 30 import java.text.spi.DateFormatProvider; 31 import java.util.Calendar; 32 import java.util.Locale; 33 import java.util.MissingResourceException; 34 import java.util.Set; 35 36 /** 37 * Concrete implementation of the {@link java.text.spi.DateFormatProvider 38 * DateFormatProvider} class for the JRE LocaleProviderAdapter. 39 * 40 * @author Naoto Sato 41 * @author Masayoshi Okutsu 42 */ 43 public class DateFormatProviderImpl extends DateFormatProvider implements AvailableLanguageTags { 44 private final LocaleProviderAdapter.Type type; 45 private static Set<String> langtags; 46 47 public DateFormatProviderImpl(LocaleProviderAdapter.Type type) { 48 this.type = type; 49 } 50 51 /** 52 * Returns an array of all locales for which this locale service provider 53 * can provide localized objects or names. 54 * 55 * @return An array of all locales for which this locale service provider 56 * can provide localized objects or names. 57 */ 58 @Override 59 public Locale[] getAvailableLocales() { 60 return LocaleProviderAdapter.toLocaleArray(getLangTags(type)); 61 } 62 63 @Override 64 public boolean isSupportedLocale(Locale locale) { 65 return LocaleProviderAdapter.isSupportedLocale(locale, type, getLangTags(type)); 66 } 67 68 /** 69 * Returns a new <code>DateFormat</code> instance which formats time 70 * with the given formatting style for the specified locale. 71 * @param style the given formatting style. Either one of 72 * {@link java.text.DateFormat#SHORT DateFormat.SHORT}, 73 * {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM}, 74 * {@link java.text.DateFormat#LONG DateFormat.LONG}, or 75 * {@link java.text.DateFormat#FULL DateFormat.FULL}. 76 * @param locale the desired locale. 77 * @exception IllegalArgumentException if <code>style</code> is invalid, 78 * or if <code>locale</code> isn't 79 * one of the locales returned from 80 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 81 * getAvailableLocales()}. 82 * @exception NullPointerException if <code>locale</code> is null 83 * @return a time formatter. 84 * @see java.text.DateFormat#getTimeInstance(int, java.util.Locale) 85 */ 86 @Override 87 public DateFormat getTimeInstance(int style, Locale locale) { 88 return getInstance(-1, style, locale); 89 } 90 91 /** 92 * Returns a new <code>DateFormat</code> instance which formats date 93 * with the given formatting style for the specified locale. 94 * @param style the given formatting style. Either one of 95 * {@link java.text.DateFormat#SHORT DateFormat.SHORT}, 96 * {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM}, 97 * {@link java.text.DateFormat#LONG DateFormat.LONG}, or 98 * {@link java.text.DateFormat#FULL DateFormat.FULL}. 99 * @param locale the desired locale. 100 * @exception IllegalArgumentException if <code>style</code> is invalid, 101 * or if <code>locale</code> isn't 102 * one of the locales returned from 103 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 104 * getAvailableLocales()}. 105 * @exception NullPointerException if <code>locale</code> is null 106 * @return a date formatter. 107 * @see java.text.DateFormat#getDateInstance(int, java.util.Locale) 108 */ 109 @Override 110 public DateFormat getDateInstance(int style, Locale locale) { 111 return getInstance(style, -1, locale); 112 } 113 114 /** 115 * Returns a new <code>DateFormat</code> instance which formats date and time 116 * with the given formatting style for the specified locale. 117 * @param dateStyle the given date formatting style. Either one of 118 * {@link java.text.DateFormat#SHORT DateFormat.SHORT}, 119 * {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM}, 120 * {@link java.text.DateFormat#LONG DateFormat.LONG}, or 121 * {@link java.text.DateFormat#FULL DateFormat.FULL}. 122 * @param timeStyle the given time formatting style. Either one of 123 * {@link java.text.DateFormat#SHORT DateFormat.SHORT}, 124 * {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM}, 125 * {@link java.text.DateFormat#LONG DateFormat.LONG}, or 126 * {@link java.text.DateFormat#FULL DateFormat.FULL}. 127 * @param locale the desired locale. 128 * @exception IllegalArgumentException if <code>dateStyle</code> or 129 * <code>timeStyle</code> is invalid, 130 * or if <code>locale</code> isn't 131 * one of the locales returned from 132 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 133 * getAvailableLocales()}. 134 * @exception NullPointerException if <code>locale</code> is null 135 * @return a date/time formatter. 136 * @see java.text.DateFormat#getDateTimeInstance(int, int, java.util.Locale) 137 */ 138 @Override 139 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle, 140 Locale locale) { 141 return getInstance(dateStyle, timeStyle, locale); 142 } 143 144 private DateFormat getInstance(int dateStyle, int timeStyle, Locale locale) { 145 if (locale == null) { 146 throw new NullPointerException(); 147 } 148 149 SimpleDateFormat sdf = new SimpleDateFormat("", locale); 150 Calendar cal = sdf.getCalendar(); 151 try { 152 String pattern = LocaleProviderAdapter.forType(type) 153 .getLocaleResources(locale).getDateTimePattern(timeStyle, dateStyle, 154 cal); 155 sdf.applyPattern(pattern); 156 } catch (MissingResourceException mre) { 157 // Specify the fallback pattern 158 sdf.applyPattern("M/d/yy h:mm a"); 159 } 160 161 return sdf; 162 } 163 164 @Override 165 public Set<String> getAvailableLanguageTags() { 166 return getLangTags(type); 167 } 168 169 /** 170 * Available language tags for FormatData resources. Shared with other "format" 171 * provider implementations. 172 * */ 173 static Set<String> getLangTags(LocaleProviderAdapter.Type t) { 174 if (langtags == null) { 175 langtags = ((JRELocaleProviderAdapter)LocaleProviderAdapter.forType(t)).getLanguageTagSet("FormatData"); 176 } 177 return langtags; 178 } 179 }