Path: blob/master/src/java.base/share/classes/sun/util/cldr/CLDRCalendarDataProviderImpl.java
41159 views
/*1* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.util.cldr;2627import static sun.util.locale.provider.LocaleProviderAdapter.Type;2829import java.util.Arrays;30import java.util.Map;31import java.util.Locale;32import java.util.Set;33import java.util.Optional;34import java.util.concurrent.ConcurrentHashMap;35import sun.util.locale.provider.LocaleProviderAdapter;36import sun.util.locale.provider.LocaleResources;37import sun.util.locale.provider.CalendarDataProviderImpl;38import sun.util.locale.provider.CalendarDataUtility;3940/**41* Concrete implementation of the42* {@link java.util.spi.CalendarDataProvider CalendarDataProvider} class43* for the CLDR LocaleProviderAdapter.44*45* @author Naoto Sato46*/47public class CLDRCalendarDataProviderImpl extends CalendarDataProviderImpl {4849private static Map<String, Integer> firstDay = new ConcurrentHashMap<>();50private static Map<String, Integer> minDays = new ConcurrentHashMap<>();5152public CLDRCalendarDataProviderImpl(Type type, Set<String> langtags) {53super(type, langtags);54}5556@Override57public int getFirstDayOfWeek(Locale locale) {58return findValue(CalendarDataUtility.FIRST_DAY_OF_WEEK, locale);59}6061@Override62public int getMinimalDaysInFirstWeek(Locale locale) {63return findValue(CalendarDataUtility.MINIMAL_DAYS_IN_FIRST_WEEK, locale);64}6566/**67* Finds the requested integer value for the locale.68* Each resource consists of the following:69*70* (n: cc1 cc2 ... ccx;)*71*72* where 'n' is the integer for the following region codes, terminated by73* a ';'.74*75*/76private static int findValue(String key, Locale locale) {77Map<String, Integer> map = CalendarDataUtility.FIRST_DAY_OF_WEEK.equals(key) ?78firstDay : minDays;79String region = locale.getCountry();8081if (region.isEmpty()) {82// Use "US" as default83region = "US";84}8586Integer val = map.get(region);87if (val == null) {88String valStr =89LocaleProviderAdapter.forType(Type.CLDR).getLocaleResources(Locale.ROOT)90.getCalendarData(key);91val = retrieveInteger(valStr, region)92.orElse(retrieveInteger(valStr, "001").orElse(0));93map.putIfAbsent(region, val);94}9596return val;97}9899private static Optional<Integer> retrieveInteger(String src, String region) {100int regionIndex = src.indexOf(region);101if (regionIndex >= 0) {102int start = src.lastIndexOf(';', regionIndex) + 1;103return Optional.of(Integer.parseInt(src, start, src.indexOf(':', start), 10));104}105return Optional.empty();106}107}108109110