1 /*
2 * Copyright (c) 1999, 2012, 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
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 final Set<String> langtags;
46
47 public DateFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
48 this.type = type;
49 this.langtags = langtags;
50 }
51
52 /**
53 * Returns an array of all locales for which this locale service provider
54 * can provide localized objects or names.
55 *
56 * @return An array of all locales for which this locale service provider
57 * can provide localized objects or names.
58 */
59 @Override
60 public Locale[] getAvailableLocales() {
61 return LocaleProviderAdapter.toLocaleArray(langtags);
62 }
63
64 @Override
65 public boolean isSupportedLocale(Locale locale) {
66 return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
67 }
68
69 /**
70 * Returns a new <code>DateFormat</code> instance which formats time
71 * with the given formatting style for the specified locale.
72 * @param style the given formatting style. Either one of
73 * {@link java.text.DateFormat#SHORT DateFormat.SHORT},
74 * {@link java.text.DateFormat#MEDIUM DateFormat.MEDIUM},
75 * {@link java.text.DateFormat#LONG DateFormat.LONG}, or
76 * {@link java.text.DateFormat#FULL DateFormat.FULL}.
77 * @param locale the desired locale.
78 * @exception IllegalArgumentException if <code>style</code> is invalid,
79 * or if <code>locale</code> isn't
80 * one of the locales returned from
81 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
82 * getAvailableLocales()}.
83 * @exception NullPointerException if <code>locale</code> is null
84 * @return a time formatter.
85 * @see java.text.DateFormat#getTimeInstance(int, java.util.Locale)
86 */
147 throw new NullPointerException();
148 }
149
150 SimpleDateFormat sdf = new SimpleDateFormat("", locale);
151 Calendar cal = sdf.getCalendar();
152 try {
153 String pattern = LocaleProviderAdapter.forType(type)
154 .getLocaleResources(locale).getDateTimePattern(timeStyle, dateStyle,
155 cal);
156 sdf.applyPattern(pattern);
157 } catch (MissingResourceException mre) {
158 // Specify the fallback pattern
159 sdf.applyPattern("M/d/yy h:mm a");
160 }
161
162 return sdf;
163 }
164
165 @Override
166 public Set<String> getAvailableLanguageTags() {
167 return langtags;
168 }
169 }
|
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
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 */
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 }
|