]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/CalendarUtil.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / CalendarUtil.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2009, International Business Machines Corporation and         *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.impl;\r
8 \r
9 import java.util.MissingResourceException;\r
10 \r
11 import com.ibm.icu.util.ULocale;\r
12 import com.ibm.icu.util.UResourceBundle;\r
13 \r
14 /**\r
15  * Calendar utilities.\r
16  * \r
17  * Date/time format service classes in com.ibm.icu.text packages\r
18  * sometimes need to access calendar internal APIs.  But calendar\r
19  * classes are in com.ibm.icu.util package, so the package local\r
20  * cannot be used.  This class is added in com.ibm.icu.impl\r
21  * package for sharing some calendar internal code for calendar\r
22  * and date format.\r
23  */\r
24 public class CalendarUtil {\r
25 \r
26     private static ICUCache<String, String> CALTYPE_CACHE = new SimpleCache<String, String>();\r
27 \r
28     private static final String CALKEY = "calendar";\r
29     private static final String DEFCAL = "gregorian";\r
30 \r
31     /**\r
32      * Returns a calendar type for the given locale.\r
33      * When the given locale has calendar keyword, the\r
34      * value of calendar keyword is returned.  Otherwise,\r
35      * the default calendar type for the locale is returned.\r
36      * @param loc The locale\r
37      * @return Calendar type string, such as "gregorian"\r
38      */\r
39     public static String getCalendarType(ULocale loc) {\r
40         String calType = null;\r
41 \r
42         calType = loc.getKeywordValue(CALKEY);\r
43         if (calType != null) {\r
44             return calType;\r
45         }\r
46 \r
47         String baseLoc = loc.getBaseName();\r
48 \r
49         // Check the cache\r
50         calType = CALTYPE_CACHE.get(baseLoc);\r
51         if (calType != null) {\r
52             return calType;\r
53         }\r
54 \r
55         // Canonicalize, so grandfathered variant will be transformed to keywords\r
56         ULocale canonical = ULocale.createCanonical(loc.toString());\r
57         calType = canonical.getKeywordValue("calendar");\r
58 \r
59         if (calType == null) {\r
60             // When calendar keyword is not available, use the locale's\r
61             // region to get the default calendar type\r
62             String region = canonical.getCountry();\r
63             if (region.length() == 0) {\r
64                 ULocale fullLoc = ULocale.addLikelySubtags(canonical);\r
65                 region = fullLoc.getCountry();\r
66             }\r
67 \r
68             // Read supplementalData to get the default calendar type for\r
69             // the locale's region\r
70             try {\r
71                 UResourceBundle rb = UResourceBundle.getBundleInstance(\r
72                                         ICUResourceBundle.ICU_BASE_NAME,\r
73                                         "supplementalData",\r
74                                         ICUResourceBundle.ICU_DATA_CLASS_LOADER);\r
75                 UResourceBundle calPref = rb.get("calendarPreferenceData");\r
76                 UResourceBundle order = null;\r
77                 try {\r
78                     order = calPref.get(region);\r
79                 } catch (MissingResourceException mre) {\r
80                     // use "001" as fallback\r
81                     order = calPref.get("001");\r
82                 }\r
83                 // the first calendar type is the default for the region\r
84                 calType = order.getString(0);\r
85             } catch (MissingResourceException mre) {\r
86                 // fall through\r
87             }\r
88 \r
89             if (calType == null) {\r
90                 // Use "gregorian" as the last resort fallback.\r
91                 calType = DEFCAL;\r
92             }\r
93         }\r
94 \r
95         // Cache the resolved value for the next time\r
96         CALTYPE_CACHE.put(baseLoc, calType);\r
97 \r
98         return calType;\r
99     }\r
100 }\r