• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 2009,2016 International Business Machines Corporation and
7  * others. All Rights Reserved.
8  *******************************************************************************
9  */
10 package ohos.global.icu.impl;
11 
12 import java.util.Locale;
13 import java.util.Map;
14 import java.util.MissingResourceException;
15 import java.util.TreeMap;
16 
17 import ohos.global.icu.util.ULocale;
18 import ohos.global.icu.util.UResourceBundle;
19 
20 /**
21  * Calendar utilities.
22  *
23  * Date/time format service classes in ohos.global.icu.text packages
24  * sometimes need to access calendar internal APIs.  But calendar
25  * classes are in ohos.global.icu.util package, so the package local
26  * cannot be used.  This class is added in ohos.global.icu.impl
27  * package for sharing some calendar internal code for calendar
28  * and date format.
29  * @hide exposed on OHOS
30  */
31 public final class CalendarUtil {
32     private static final String CALKEY = "calendar";
33     private static final String DEFCAL = "gregorian";
34 
35     /**
36      * Returns a calendar type for the given locale.
37      * When the given locale has calendar keyword, the
38      * value of calendar keyword is returned.  Otherwise,
39      * the default calendar type for the locale is returned.
40      * @param loc The locale
41      * @return Calendar type string, such as "gregorian"
42      */
getCalendarType(ULocale loc)43     public static String getCalendarType(ULocale loc) {
44         String calType = loc.getKeywordValue(CALKEY);
45         if (calType != null) {
46             // Convert to lower case, because getKeywordValue does not
47             // canonicalize keyword value.
48             return calType.toLowerCase(Locale.ROOT);
49         }
50 
51         // Canonicalize, so grandfathered variant will be transformed to keywords
52         ULocale canonical = ULocale.createCanonical(loc.toString());
53         calType = canonical.getKeywordValue(CALKEY);
54         if (calType != null) {
55             return calType;
56         }
57 
58         // When calendar keyword is not available, use the locale's
59         // region to get the default calendar type
60         String region = ULocale.getRegionForSupplementalData(canonical, true);
61         return CalendarPreferences.INSTANCE.getCalendarTypeForRegion(region);
62     }
63 
64     private static final class CalendarPreferences extends UResource.Sink {
65         private static final CalendarPreferences INSTANCE = new CalendarPreferences();
66         // A TreeMap should be good because we expect very few entries.
67         Map<String, String> prefs = new TreeMap<String, String>();
68 
CalendarPreferences()69         CalendarPreferences() {
70             try {
71                 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(
72                         ICUData.ICU_BASE_NAME, "supplementalData");
73                 rb.getAllItemsWithFallback("calendarPreferenceData", this);
74             } catch (MissingResourceException mre) {
75                 // Always use "gregorian".
76             }
77         }
78 
getCalendarTypeForRegion(String region)79         String getCalendarTypeForRegion(String region) {
80             String type = prefs.get(region);
81             return type == null ? DEFCAL : type;
82         }
83 
84         @Override
put(UResource.Key key, UResource.Value value, boolean noFallback)85         public void put(UResource.Key key, UResource.Value value, boolean noFallback) {
86             UResource.Table calendarPreferenceData = value.getTable();
87             for (int i = 0; calendarPreferenceData.getKeyAndValue(i, key, value); ++i) {
88                 UResource.Array types = value.getArray();
89                 // The first calendar type is the default for the region.
90                 if (types.getValue(0, value)) {
91                     String type = value.getString();
92                     if (!type.equals(DEFCAL)) {
93                         prefs.put(key.toString(), type);
94                     }
95                 }
96             }
97         }
98     }
99 }
100