ICU-7137 Moved getCalendarType from Calendar to new class CalendarUtil in impl package and use it from both Calendar and DateFormatSymbols.

X-SVN-Rev: 26643
This commit is contained in:
Yoshito Umaoka 2009-09-17 16:43:35 +00:00
parent 058950ccfb
commit 2c2d76044f
3 changed files with 105 additions and 61 deletions

View File

@ -0,0 +1,100 @@
/*
*******************************************************************************
* Copyright (C) 2009, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.impl;
import java.util.MissingResourceException;
import com.ibm.icu.util.ULocale;
import com.ibm.icu.util.UResourceBundle;
/**
* Calendar utilities.
*
* Date/time format service classes in com.ibm.icu.text packages
* sometimes need to access calendar internal APIs. But calendar
* classes are in com.ibm.icu.util package, so the package local
* cannot be used. This class is added in com.ibm.icu.impl
* package for sharing some calendar internal code for calendar
* and date format.
*/
public class CalendarUtil {
private static ICUCache<String, String> CALTYPE_CACHE = new SimpleCache<String, String>();
private static final String CALKEY = "calendar";
private static final String DEFCAL = "gregorian";
/**
* Returns a calendar type for the given locale.
* When the given locale has calendar keyword, the
* value of calendar keyword is returned. Otherwise,
* the default calendar type for the locale is returned.
* @param loc The locale
* @return Calendar type string, such as "gregorian"
*/
public static String getCalendarType(ULocale loc) {
String calType = null;
calType = loc.getKeywordValue(CALKEY);
if (calType != null) {
return calType;
}
String baseLoc = loc.getBaseName();
// Check the cache
calType = CALTYPE_CACHE.get(baseLoc);
if (calType != null) {
return calType;
}
// Canonicalize, so grandfathered variant will be transformed to keywords
ULocale canonical = ULocale.createCanonical(loc.toString());
calType = canonical.getKeywordValue("calendar");
if (calType == null) {
// When calendar keyword is not available, use the locale's
// region to get the default calendar type
String region = canonical.getCountry();
if (region.length() == 0) {
ULocale fullLoc = ULocale.addLikelySubtags(canonical);
region = fullLoc.getCountry();
}
// Read supplementalData to get the default calendar type for
// the locale's region
try {
UResourceBundle rb = UResourceBundle.getBundleInstance(
ICUResourceBundle.ICU_BASE_NAME,
"supplementalData",
ICUResourceBundle.ICU_DATA_CLASS_LOADER);
UResourceBundle calPref = rb.get("calendarPreferenceData");
UResourceBundle order = null;
try {
order = calPref.get(region);
} catch (MissingResourceException mre) {
// use "001" as fallback
order = calPref.get("001");
}
// the first calendar type is the default for the region
calType = order.getString(0);
} catch (MissingResourceException mre) {
// fall through
}
if (calType == null) {
// Use "gregorian" as the last resort fallback.
calType = DEFCAL;
}
}
// Cache the resolved value for the next time
CALTYPE_CACHE.put(baseLoc, calType);
return calType;
}
}

View File

@ -15,6 +15,7 @@ import java.util.MissingResourceException;
import java.util.ResourceBundle;
import com.ibm.icu.impl.CalendarData;
import com.ibm.icu.impl.CalendarUtil;
import com.ibm.icu.impl.ICUCache;
import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.SimpleCache;
@ -161,7 +162,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/
public DateFormatSymbols(ULocale locale)
{
initializeData(locale, getCalendarType(locale));
initializeData(locale, CalendarUtil.getCalendarType(locale));
}
/**
@ -1530,7 +1531,7 @@ public class DateFormatSymbols implements Serializable, Cloneable {
*/
public DateFormatSymbols(ResourceBundle bundle, ULocale locale) {
initializeData(locale,
new CalendarData((ICUResourceBundle)bundle, getCalendarType(locale)));
new CalendarData((ICUResourceBundle)bundle, CalendarUtil.getCalendarType(locale)));
}
/**
@ -1603,18 +1604,6 @@ public class DateFormatSymbols implements Serializable, Cloneable {
return null;
}
// Return the calendar type string for the given locale
private static String getCalendarType(ULocale locale) {
String calType = locale.getKeywordValue("calendar");
if (calType == null) {
ClassLoader cl = DateFormatSymbols.class.getClassLoader();
locale = ICUResourceBundle.getFunctionalEquivalent(
ICUResourceBundle.ICU_BASE_NAME, cl, "calendar", "calendar", locale, null, false);
calType = locale.getKeywordValue("calendar");
}
return calType;
}
// -------- BEGIN ULocale boilerplate --------
/**

View File

@ -18,6 +18,7 @@ import java.util.MissingResourceException;
import java.util.Set;
import com.ibm.icu.impl.CalendarData;
import com.ibm.icu.impl.CalendarUtil;
import com.ibm.icu.impl.ICUCache;
import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.SimpleCache;
@ -1667,53 +1668,7 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
private static final int CALTYPE_UNKNOWN = -1;
private static int getCalendarTypeForLocale(ULocale l) {
int calType = CALTYPE_UNKNOWN;
// canonicalize, so grandfathered variant will be transformed to keywords
ULocale canonical = ULocale.createCanonical(l.toString());
String calTypeStr = canonical.getKeywordValue("calendar");
if (calTypeStr != null) {
calType = getCalendarType(calTypeStr);
if (calType != CALTYPE_UNKNOWN) {
return calType;
}
}
// when calendar keyword is not available or not supported, read supplementalData
// to get the default calendar type for the locale's region
String region = canonical.getCountry();
if (region.length() == 0) {
ULocale fullLoc = ULocale.addLikelySubtags(canonical);
region = fullLoc.getCountry();
}
try {
UResourceBundle rb = UResourceBundle.getBundleInstance(
ICUResourceBundle.ICU_BASE_NAME,
"supplementalData",
ICUResourceBundle.ICU_DATA_CLASS_LOADER);
UResourceBundle calPref = rb.get("calendarPreferenceData");
UResourceBundle order = null;
try {
order = calPref.get(region);
} catch (MissingResourceException mre) {
// use "001" as fallback
order = calPref.get("001");
}
// the first calendar type is the default for the region
calTypeStr = order.getString(0);
calType = getCalendarType(calTypeStr);
} catch (MissingResourceException mre) {
// fall through
}
if (calType == CALTYPE_UNKNOWN) {
return CALTYPE_GREGORIAN;
}
return calType;
}
private static int getCalendarType(String s) {
String s = CalendarUtil.getCalendarType(l);
if (s != null) {
s = s.toLowerCase();
for (int i = 0; i < calTypes.length; ++i) {