1 /*
2  * Copyright (c) 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 
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 
CLDRCalendarDataProviderImpl(Type type, Set<String> langtags)53     public CLDRCalendarDataProviderImpl(Type type, Set<String> langtags) {
54         super(type, langtags);
55     }
56 
57     @Override
getFirstDayOfWeek(Locale locale)58     public int getFirstDayOfWeek(Locale locale) {
59         return findValue(CalendarDataUtility.FIRST_DAY_OF_WEEK, locale);
60     }
61 
62     @Override
getMinimalDaysInFirstWeek(Locale locale)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      */
findValue(String key, Locale locale)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             return 0;
84         }
85 
86         Integer val = map.get(region);
87         if (val == null) {
88             String valStr =
89                 LocaleProviderAdapter.forType(Type.CLDR).getLocaleResources(Locale.ROOT)
90                    .getCalendarData(key);
91             val = retrieveInteger(valStr, region)
92                 .orElse(retrieveInteger(valStr, "001").orElse(0));
93             map.putIfAbsent(region, val);
94         }
95 
96         return val;
97     }
98 
retrieveInteger(String src, String region)99     private static Optional<Integer> retrieveInteger(String src, String region) {
100         int regionIndex = src.indexOf(region);
101         if (regionIndex >= 0) {
102             int start = src.lastIndexOf(';', regionIndex) + 1;
103             return Optional.of(Integer.parseInt(src, start, src.indexOf(':', start), 10));
104         }
105         return Optional.empty();
106     }
107 }
108