author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 25859 | 3317bb8137f4 |
child 33310 | 2701a9854bb5 |
permissions | -rw-r--r-- |
13583 | 1 |
/* |
16852 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
13583 | 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 |
||
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
28 |
import static java.util.Calendar.*; |
13583 | 29 |
import java.util.Locale; |
30 |
import java.util.Map; |
|
31 |
import java.util.spi.CalendarDataProvider; |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
32 |
import java.util.spi.CalendarNameProvider; |
13583 | 33 |
|
34 |
/** |
|
35 |
* {@code CalendarDataUtility} is a utility class for calling the |
|
36 |
* {@link CalendarDataProvider} methods. |
|
37 |
* |
|
38 |
* @author Masayoshi Okutsu |
|
39 |
* @author Naoto Sato |
|
40 |
*/ |
|
41 |
public class CalendarDataUtility { |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
42 |
public static final String FIRST_DAY_OF_WEEK = "firstDayOfWeek"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
43 |
public static final String MINIMAL_DAYS_IN_FIRST_WEEK = "minimalDaysInFirstWeek"; |
13583 | 44 |
|
45 |
// No instantiation |
|
46 |
private CalendarDataUtility() { |
|
47 |
} |
|
48 |
||
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
49 |
public static int retrieveFirstDayOfWeek(Locale locale) { |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
50 |
LocaleServiceProviderPool pool = |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
51 |
LocaleServiceProviderPool.getPool(CalendarDataProvider.class); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
52 |
Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE, |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
53 |
locale, FIRST_DAY_OF_WEEK); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
54 |
return (value != null && (value >= SUNDAY && value <= SATURDAY)) ? value : SUNDAY; |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
55 |
} |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
56 |
|
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
57 |
public static int retrieveMinimalDaysInFirstWeek(Locale locale) { |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
58 |
LocaleServiceProviderPool pool = |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
59 |
LocaleServiceProviderPool.getPool(CalendarDataProvider.class); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
60 |
Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE, |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
61 |
locale, MINIMAL_DAYS_IN_FIRST_WEEK); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
62 |
return (value != null && (value >= 1 && value <= 7)) ? value : 1; |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
63 |
} |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
64 |
|
13583 | 65 |
public static String retrieveFieldValueName(String id, int field, int value, int style, Locale locale) { |
66 |
LocaleServiceProviderPool pool = |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
67 |
LocaleServiceProviderPool.getPool(CalendarNameProvider.class); |
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
68 |
return pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id), |
15658 | 69 |
field, value, style, false); |
70 |
} |
|
71 |
||
16852 | 72 |
public static String retrieveJavaTimeFieldValueName(String id, int field, int value, int style, Locale locale) { |
15658 | 73 |
LocaleServiceProviderPool pool = |
74 |
LocaleServiceProviderPool.getPool(CalendarNameProvider.class); |
|
75 |
String name; |
|
76 |
name = pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id), |
|
77 |
field, value, style, true); |
|
78 |
if (name == null) { |
|
79 |
name = pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id), |
|
80 |
field, value, style, false); |
|
81 |
} |
|
82 |
return name; |
|
13583 | 83 |
} |
84 |
||
85 |
public static Map<String, Integer> retrieveFieldValueNames(String id, int field, int style, Locale locale) { |
|
86 |
LocaleServiceProviderPool pool = |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
87 |
LocaleServiceProviderPool.getPool(CalendarNameProvider.class); |
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
88 |
return pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale, |
15658 | 89 |
normalizeCalendarType(id), field, style, false); |
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
90 |
} |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
91 |
|
16852 | 92 |
public static Map<String, Integer> retrieveJavaTimeFieldValueNames(String id, int field, int style, Locale locale) { |
15658 | 93 |
LocaleServiceProviderPool pool = |
94 |
LocaleServiceProviderPool.getPool(CalendarNameProvider.class); |
|
95 |
Map<String, Integer> map; |
|
96 |
map = pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale, |
|
97 |
normalizeCalendarType(id), field, style, true); |
|
98 |
if (map == null) { |
|
99 |
map = pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale, |
|
100 |
normalizeCalendarType(id), field, style, false); |
|
101 |
} |
|
102 |
return map; |
|
103 |
} |
|
104 |
||
105 |
static String normalizeCalendarType(String requestID) { |
|
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
106 |
String type; |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
107 |
if (requestID.equals("gregorian") || requestID.equals("iso8601")) { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
108 |
type = "gregory"; |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
109 |
} else if (requestID.startsWith("islamic")) { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
110 |
type = "islamic"; |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
111 |
} else { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
112 |
type = requestID; |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
113 |
} |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
114 |
return type; |
13583 | 115 |
} |
116 |
||
117 |
/** |
|
118 |
* Obtains a localized field value string from a CalendarDataProvider |
|
119 |
* implementation. |
|
120 |
*/ |
|
121 |
private static class CalendarFieldValueNameGetter |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
122 |
implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider, |
13583 | 123 |
String> { |
124 |
private static final CalendarFieldValueNameGetter INSTANCE = |
|
125 |
new CalendarFieldValueNameGetter(); |
|
126 |
||
127 |
@Override |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
128 |
public String getObject(CalendarNameProvider calendarNameProvider, |
13583 | 129 |
Locale locale, |
130 |
String requestID, // calendarType |
|
131 |
Object... params) { |
|
15658 | 132 |
assert params.length == 4; |
13583 | 133 |
int field = (int) params[0]; |
134 |
int value = (int) params[1]; |
|
135 |
int style = (int) params[2]; |
|
16852 | 136 |
boolean javatime = (boolean) params[3]; |
15658 | 137 |
|
16852 | 138 |
// If javatime is true, resources from CLDR have precedence over JRE |
15658 | 139 |
// native resources. |
16852 | 140 |
if (javatime && calendarNameProvider instanceof CalendarNameProviderImpl) { |
15658 | 141 |
String name; |
142 |
name = ((CalendarNameProviderImpl)calendarNameProvider) |
|
16852 | 143 |
.getJavaTimeDisplayName(requestID, field, value, style, locale); |
15658 | 144 |
return name; |
145 |
} |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
146 |
return calendarNameProvider.getDisplayName(requestID, field, value, style, locale); |
13583 | 147 |
} |
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Obtains a localized field-value pairs from a CalendarDataProvider |
|
152 |
* implementation. |
|
153 |
*/ |
|
154 |
private static class CalendarFieldValueNamesMapGetter |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
155 |
implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider, |
13583 | 156 |
Map<String, Integer>> { |
157 |
private static final CalendarFieldValueNamesMapGetter INSTANCE = |
|
158 |
new CalendarFieldValueNamesMapGetter(); |
|
159 |
||
160 |
@Override |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
161 |
public Map<String, Integer> getObject(CalendarNameProvider calendarNameProvider, |
13583 | 162 |
Locale locale, |
163 |
String requestID, // calendarType |
|
164 |
Object... params) { |
|
15658 | 165 |
assert params.length == 3; |
13583 | 166 |
int field = (int) params[0]; |
167 |
int style = (int) params[1]; |
|
16852 | 168 |
boolean javatime = (boolean) params[2]; |
15658 | 169 |
|
16852 | 170 |
// If javatime is true, resources from CLDR have precedence over JRE |
15658 | 171 |
// native resources. |
16852 | 172 |
if (javatime && calendarNameProvider instanceof CalendarNameProviderImpl) { |
15658 | 173 |
Map<String, Integer> map; |
174 |
map = ((CalendarNameProviderImpl)calendarNameProvider) |
|
16852 | 175 |
.getJavaTimeDisplayNames(requestID, field, style, locale); |
15658 | 176 |
return map; |
177 |
} |
|
14502
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
178 |
return calendarNameProvider.getDisplayNames(requestID, field, style, locale); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
179 |
} |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
180 |
} |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
181 |
|
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
182 |
private static class CalendarWeekParameterGetter |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
183 |
implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarDataProvider, |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
184 |
Integer> { |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
185 |
private static final CalendarWeekParameterGetter INSTANCE = |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
186 |
new CalendarWeekParameterGetter(); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
187 |
|
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
188 |
@Override |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
189 |
public Integer getObject(CalendarDataProvider calendarDataProvider, |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
190 |
Locale locale, |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
191 |
String requestID, // resource key |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
192 |
Object... params) { |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
193 |
assert params.length == 0; |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
194 |
int value; |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
195 |
switch (requestID) { |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
196 |
case FIRST_DAY_OF_WEEK: |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
197 |
value = calendarDataProvider.getFirstDayOfWeek(locale); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
198 |
break; |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
199 |
case MINIMAL_DAYS_IN_FIRST_WEEK: |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
200 |
value = calendarDataProvider.getMinimalDaysInFirstWeek(locale); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
201 |
break; |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
202 |
default: |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
203 |
throw new InternalError("invalid requestID: " + requestID); |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
204 |
} |
d63fed06fed4
8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
13583
diff
changeset
|
205 |
return (value != 0) ? value : null; |
13583 | 206 |
} |
207 |
} |
|
208 |
} |