Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/util/cldr/CLDRCalendarDataProviderImpl.java
41159 views
1
/*
2
* Copyright (c) 2017, 2019, 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.cldr;
27
28
import static sun.util.locale.provider.LocaleProviderAdapter.Type;
29
30
import java.util.Arrays;
31
import java.util.Map;
32
import java.util.Locale;
33
import java.util.Set;
34
import java.util.Optional;
35
import java.util.concurrent.ConcurrentHashMap;
36
import sun.util.locale.provider.LocaleProviderAdapter;
37
import sun.util.locale.provider.LocaleResources;
38
import sun.util.locale.provider.CalendarDataProviderImpl;
39
import sun.util.locale.provider.CalendarDataUtility;
40
41
/**
42
* Concrete implementation of the
43
* {@link java.util.spi.CalendarDataProvider CalendarDataProvider} class
44
* for the CLDR LocaleProviderAdapter.
45
*
46
* @author Naoto Sato
47
*/
48
public class CLDRCalendarDataProviderImpl extends CalendarDataProviderImpl {
49
50
private static Map<String, Integer> firstDay = new ConcurrentHashMap<>();
51
private static Map<String, Integer> minDays = new ConcurrentHashMap<>();
52
53
public CLDRCalendarDataProviderImpl(Type type, Set<String> langtags) {
54
super(type, langtags);
55
}
56
57
@Override
58
public int getFirstDayOfWeek(Locale locale) {
59
return findValue(CalendarDataUtility.FIRST_DAY_OF_WEEK, locale);
60
}
61
62
@Override
63
public int getMinimalDaysInFirstWeek(Locale locale) {
64
return findValue(CalendarDataUtility.MINIMAL_DAYS_IN_FIRST_WEEK, locale);
65
}
66
67
/**
68
* Finds the requested integer value for the locale.
69
* Each resource consists of the following:
70
*
71
* (n: cc1 cc2 ... ccx;)*
72
*
73
* where 'n' is the integer for the following region codes, terminated by
74
* a ';'.
75
*
76
*/
77
private static int findValue(String key, Locale locale) {
78
Map<String, Integer> map = CalendarDataUtility.FIRST_DAY_OF_WEEK.equals(key) ?
79
firstDay : minDays;
80
String region = locale.getCountry();
81
82
if (region.isEmpty()) {
83
// Use "US" as default
84
region = "US";
85
}
86
87
Integer val = map.get(region);
88
if (val == null) {
89
String valStr =
90
LocaleProviderAdapter.forType(Type.CLDR).getLocaleResources(Locale.ROOT)
91
.getCalendarData(key);
92
val = retrieveInteger(valStr, region)
93
.orElse(retrieveInteger(valStr, "001").orElse(0));
94
map.putIfAbsent(region, val);
95
}
96
97
return val;
98
}
99
100
private static Optional<Integer> retrieveInteger(String src, String region) {
101
int regionIndex = src.indexOf(region);
102
if (regionIndex >= 0) {
103
int start = src.lastIndexOf(';', regionIndex) + 1;
104
return Optional.of(Integer.parseInt(src, start, src.indexOf(':', start), 10));
105
}
106
return Optional.empty();
107
}
108
}
109
110