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 package sun.util.locale.provider;
26
27 import static java.util.Calendar.*;
28 import java.util.Comparator;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.TreeMap;
33 import java.util.spi.CalendarNameProvider;
34
35 /**
36 * Concrete implementation of the {@link java.util.spi.CalendarDataProvider
37 * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
38 *
39 * @author Masayoshi Okutsu
40 * @author Naoto Sato
41 */
42 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
43 private final LocaleProviderAdapter.Type type;
44 private final Set<String> langtags;
45
46 public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
47 this.type = type;
48 this.langtags = langtags;
49 }
50
51 @Override
52 public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
53 return getDisplayNameImpl(calendarType, field, value, style, locale, false);
58 }
59
60 public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) {
61 String name = null;
62 String key = getResourceKey(calendarType, field, style, javatime);
63 if (key != null) {
64 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);
65 String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
66
67 // If standalone names are requested and no "standalone." resources are found,
68 // try the default ones instead.
69 if (strings == null && key.indexOf("standalone.") != -1) {
70 key = key.replaceFirst("standalone.", "");
71 strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
72 }
73
74 if (strings != null && strings.length > 0) {
75 if (field == DAY_OF_WEEK || field == YEAR) {
76 --value;
77 }
78 if (value < 0 || value >= strings.length) {
79 return null;
80 }
81 name = strings[value];
82 // If name is empty in standalone, try its `format' style.
83 if (name.length() == 0
84 && (style == SHORT_STANDALONE || style == LONG_STANDALONE
85 || style == NARROW_STANDALONE)) {
86 name = getDisplayName(calendarType, field, value,
87 getBaseStyle(style),
88 locale);
89 }
90 }
91 }
92 return name;
93 }
94
95 private static int[] REST_OF_STYLES = {
96 SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
97 NARROW_FORMAT, NARROW_STANDALONE
98 };
|
1 /*
2 * Copyright (c) 2012, 2017, 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 package sun.util.locale.provider;
26
27 import static java.util.Calendar.*;
28 import java.util.Comparator;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.TreeMap;
33 import java.util.spi.CalendarNameProvider;
34 import sun.util.calendar.CalendarSystem;
35 import sun.util.calendar.Era;
36
37 /**
38 * Concrete implementation of the {@link java.util.spi.CalendarDataProvider
39 * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
40 *
41 * @author Masayoshi Okutsu
42 * @author Naoto Sato
43 */
44 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
45 private final LocaleProviderAdapter.Type type;
46 private final Set<String> langtags;
47
48 public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
49 this.type = type;
50 this.langtags = langtags;
51 }
52
53 @Override
54 public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
55 return getDisplayNameImpl(calendarType, field, value, style, locale, false);
60 }
61
62 public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) {
63 String name = null;
64 String key = getResourceKey(calendarType, field, style, javatime);
65 if (key != null) {
66 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);
67 String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
68
69 // If standalone names are requested and no "standalone." resources are found,
70 // try the default ones instead.
71 if (strings == null && key.indexOf("standalone.") != -1) {
72 key = key.replaceFirst("standalone.", "");
73 strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
74 }
75
76 if (strings != null && strings.length > 0) {
77 if (field == DAY_OF_WEEK || field == YEAR) {
78 --value;
79 }
80 if (value < 0 || value > strings.length) {
81 return null;
82 } else if (value == strings.length) {
83 if (field == ERA && "japanese".equals(calendarType)) {
84 // get the supplemental era, if any, specified through
85 // the property "jdk.calendar.japanese.supplemental.era"
86 // which is always the last element.
87 Era[] jeras = CalendarSystem.forName("japanese").getEras();
88 if (jeras.length == value) {
89 Era supEra = jeras[value - 1]; // 0-based index
90 return style == LONG ?
91 supEra.getName() :
92 supEra.getAbbreviation();
93 }
94 }
95 return null;
96 }
97 name = strings[value];
98 // If name is empty in standalone, try its `format' style.
99 if (name.length() == 0
100 && (style == SHORT_STANDALONE || style == LONG_STANDALONE
101 || style == NARROW_STANDALONE)) {
102 name = getDisplayName(calendarType, field, value,
103 getBaseStyle(style),
104 locale);
105 }
106 }
107 }
108 return name;
109 }
110
111 private static int[] REST_OF_STYLES = {
112 SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
113 NARROW_FORMAT, NARROW_STANDALONE
114 };
|