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