Generify LocaleUtils and its Test class

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@754603 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2009-03-15 02:50:18 +00:00
parent 856a80b608
commit 8a51b94300
2 changed files with 44 additions and 44 deletions

View File

@ -40,16 +40,16 @@ import java.util.Set;
public class LocaleUtils { public class LocaleUtils {
/** Unmodifiable list of available locales. */ /** Unmodifiable list of available locales. */
private static final List cAvailableLocaleList; private static final List<Locale> cAvailableLocaleList;
/** Unmodifiable set of available locales. */ /** Unmodifiable set of available locales. */
//@GuardedBy("this") //@GuardedBy("this")
private static Set cAvailableLocaleSet; // lazily created by availableLocaleSet() private static Set<Locale> cAvailableLocaleSet; // lazily created by availableLocaleSet()
/** Unmodifiable map of language locales by country. */ /** Unmodifiable map of language locales by country. */
private static final Map cLanguagesByCountry = Collections.synchronizedMap(new HashMap()); private static final Map<String, List<Locale>> cLanguagesByCountry = Collections.synchronizedMap(new HashMap<String, List<Locale>>());
/** Unmodifiable map of country locales by language. */ /** Unmodifiable map of country locales by language. */
private static final Map cCountriesByLanguage = Collections.synchronizedMap(new HashMap()); private static final Map<String, List<Locale>> cCountriesByLanguage = Collections.synchronizedMap(new HashMap<String, List<Locale>>());
static { static {
List list = Arrays.asList(Locale.getAvailableLocales()); List<Locale> list = Arrays.asList(Locale.getAvailableLocales());
cAvailableLocaleList = Collections.unmodifiableList(list); cAvailableLocaleList = Collections.unmodifiableList(list);
} }
@ -143,7 +143,7 @@ public class LocaleUtils {
* @param locale the locale to start from * @param locale the locale to start from
* @return the unmodifiable list of Locale objects, 0 being locale, never null * @return the unmodifiable list of Locale objects, 0 being locale, never null
*/ */
public static List localeLookupList(Locale locale) { public static List<Locale> localeLookupList(Locale locale) {
return localeLookupList(locale, locale); return localeLookupList(locale, locale);
} }
@ -165,8 +165,8 @@ public class LocaleUtils {
* @param defaultLocale the default locale to use if no other is found * @param defaultLocale the default locale to use if no other is found
* @return the unmodifiable list of Locale objects, 0 being locale, never null * @return the unmodifiable list of Locale objects, 0 being locale, never null
*/ */
public static List localeLookupList(Locale locale, Locale defaultLocale) { public static List<Locale> localeLookupList(Locale locale, Locale defaultLocale) {
List list = new ArrayList(4); List<Locale> list = new ArrayList<Locale>(4);
if (locale != null) { if (locale != null) {
list.add(locale); list.add(locale);
if (locale.getVariant().length() > 0) { if (locale.getVariant().length() > 0) {
@ -192,7 +192,7 @@ public class LocaleUtils {
* *
* @return the unmodifiable list of available locales * @return the unmodifiable list of available locales
*/ */
public static List availableLocaleList() { public static List<Locale> availableLocaleList() {
return cAvailableLocaleList; return cAvailableLocaleList;
} }
@ -206,10 +206,10 @@ public class LocaleUtils {
* *
* @return the unmodifiable set of available locales * @return the unmodifiable set of available locales
*/ */
public static synchronized Set availableLocaleSet() { public static synchronized Set<Locale> availableLocaleSet() {
Set set = cAvailableLocaleSet; Set<Locale> set = cAvailableLocaleSet;
if (set == null) { if (set == null) {
set = new HashSet(availableLocaleList()); set = new HashSet<Locale>(availableLocaleList());
set = Collections.unmodifiableSet(set); set = Collections.unmodifiableSet(set);
cAvailableLocaleSet = set; cAvailableLocaleSet = set;
} }
@ -237,14 +237,14 @@ public class LocaleUtils {
* @param countryCode the 2 letter country code, null returns empty * @param countryCode the 2 letter country code, null returns empty
* @return an unmodifiable List of Locale objects, never null * @return an unmodifiable List of Locale objects, never null
*/ */
public static List languagesByCountry(String countryCode) { public static List<Locale> languagesByCountry(String countryCode) {
List langs = (List) cLanguagesByCountry.get(countryCode); //syncd List<Locale> langs = cLanguagesByCountry.get(countryCode); //syncd
if (langs == null) { if (langs == null) {
if (countryCode != null) { if (countryCode != null) {
langs = new ArrayList(); langs = new ArrayList<Locale>();
List locales = availableLocaleList(); List<Locale> locales = availableLocaleList();
for (int i = 0; i < locales.size(); i++) { for (int i = 0; i < locales.size(); i++) {
Locale locale = (Locale) locales.get(i); Locale locale = locales.get(i);
if (countryCode.equals(locale.getCountry()) && if (countryCode.equals(locale.getCountry()) &&
locale.getVariant().length() == 0) { locale.getVariant().length() == 0) {
langs.add(locale); langs.add(locale);
@ -252,7 +252,7 @@ public class LocaleUtils {
} }
langs = Collections.unmodifiableList(langs); langs = Collections.unmodifiableList(langs);
} else { } else {
langs = Collections.EMPTY_LIST; langs = Collections.emptyList();
} }
cLanguagesByCountry.put(countryCode, langs); //syncd cLanguagesByCountry.put(countryCode, langs); //syncd
} }
@ -269,14 +269,14 @@ public class LocaleUtils {
* @param languageCode the 2 letter language code, null returns empty * @param languageCode the 2 letter language code, null returns empty
* @return an unmodifiable List of Locale objects, never null * @return an unmodifiable List of Locale objects, never null
*/ */
public static List countriesByLanguage(String languageCode) { public static List<Locale> countriesByLanguage(String languageCode) {
List countries = (List) cCountriesByLanguage.get(languageCode); //syncd List<Locale> countries = cCountriesByLanguage.get(languageCode); //syncd
if (countries == null) { if (countries == null) {
if (languageCode != null) { if (languageCode != null) {
countries = new ArrayList(); countries = new ArrayList<Locale>();
List locales = availableLocaleList(); List<Locale> locales = availableLocaleList();
for (int i = 0; i < locales.size(); i++) { for (int i = 0; i < locales.size(); i++) {
Locale locale = (Locale) locales.get(i); Locale locale = locales.get(i);
if (languageCode.equals(locale.getLanguage()) && if (languageCode.equals(locale.getLanguage()) &&
locale.getCountry().length() != 0 && locale.getCountry().length() != 0 &&
locale.getVariant().length() == 0) { locale.getVariant().length() == 0) {
@ -285,7 +285,7 @@ public class LocaleUtils {
} }
countries = Collections.unmodifiableList(countries); countries = Collections.unmodifiableList(countries);
} else { } else {
countries = Collections.EMPTY_LIST; countries = Collections.emptyList();
} }
cCountriesByLanguage.put(languageCode, countries); //syncd cCountriesByLanguage.put(languageCode, countries); //syncd
} }

View File

@ -89,7 +89,7 @@ public class LocaleUtilsTest extends TestCase {
*/ */
public void testConstructor() { public void testConstructor() {
assertNotNull(new LocaleUtils()); assertNotNull(new LocaleUtils());
Constructor[] cons = LocaleUtils.class.getDeclaredConstructors(); Constructor<?>[] cons = LocaleUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length); assertEquals(1, cons.length);
assertEquals(true, Modifier.isPublic(cons[0].getModifiers())); assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
assertEquals(true, Modifier.isPublic(LocaleUtils.class.getModifiers())); assertEquals(true, Modifier.isPublic(LocaleUtils.class.getModifiers()));
@ -260,7 +260,7 @@ public class LocaleUtilsTest extends TestCase {
* @param expected expected results * @param expected expected results
*/ */
private void assertLocaleLookupList(Locale locale, Locale defaultLocale, Locale[] expected) { private void assertLocaleLookupList(Locale locale, Locale defaultLocale, Locale[] expected) {
List localeList = defaultLocale == null ? List<Locale> localeList = defaultLocale == null ?
LocaleUtils.localeLookupList(locale) : LocaleUtils.localeLookupList(locale) :
LocaleUtils.localeLookupList(locale, defaultLocale); LocaleUtils.localeLookupList(locale, defaultLocale);
@ -347,14 +347,14 @@ public class LocaleUtilsTest extends TestCase {
* Test availableLocaleList() method. * Test availableLocaleList() method.
*/ */
public void testAvailableLocaleList() { public void testAvailableLocaleList() {
List list = LocaleUtils.availableLocaleList(); List<Locale> list = LocaleUtils.availableLocaleList();
List list2 = LocaleUtils.availableLocaleList(); List<Locale> list2 = LocaleUtils.availableLocaleList();
assertNotNull(list); assertNotNull(list);
assertSame(list, list2); assertSame(list, list2);
assertUnmodifiableCollection(list); assertUnmodifiableCollection(list);
Locale[] jdkLocaleArray = Locale.getAvailableLocales(); Locale[] jdkLocaleArray = Locale.getAvailableLocales();
List jdkLocaleList = Arrays.asList(jdkLocaleArray); List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
assertEquals(jdkLocaleList, list); assertEquals(jdkLocaleList, list);
} }
@ -363,15 +363,15 @@ public class LocaleUtilsTest extends TestCase {
* Test availableLocaleSet() method. * Test availableLocaleSet() method.
*/ */
public void testAvailableLocaleSet() { public void testAvailableLocaleSet() {
Set set = LocaleUtils.availableLocaleSet(); Set<Locale> set = LocaleUtils.availableLocaleSet();
Set set2 = LocaleUtils.availableLocaleSet(); Set<Locale> set2 = LocaleUtils.availableLocaleSet();
assertNotNull(set); assertNotNull(set);
assertSame(set, set2); assertSame(set, set2);
assertUnmodifiableCollection(set); assertUnmodifiableCollection(set);
Locale[] jdkLocaleArray = Locale.getAvailableLocales(); Locale[] jdkLocaleArray = Locale.getAvailableLocales();
List jdkLocaleList = Arrays.asList(jdkLocaleArray); List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
Set jdkLocaleSet = new HashSet(jdkLocaleList); Set<Locale> jdkLocaleSet = new HashSet<Locale>(jdkLocaleList);
assertEquals(jdkLocaleSet, set); assertEquals(jdkLocaleSet, set);
} }
@ -380,7 +380,7 @@ public class LocaleUtilsTest extends TestCase {
* Test availableLocaleSet() method. * Test availableLocaleSet() method.
*/ */
public void testIsAvailableLocale() { public void testIsAvailableLocale() {
Set set = LocaleUtils.availableLocaleSet(); Set<Locale> set = LocaleUtils.availableLocaleSet();
assertEquals(set.contains(LOCALE_EN), LocaleUtils.isAvailableLocale(LOCALE_EN)); assertEquals(set.contains(LOCALE_EN), LocaleUtils.isAvailableLocale(LOCALE_EN));
assertEquals(set.contains(LOCALE_EN_US), LocaleUtils.isAvailableLocale(LOCALE_EN_US)); assertEquals(set.contains(LOCALE_EN_US), LocaleUtils.isAvailableLocale(LOCALE_EN_US));
assertEquals(set.contains(LOCALE_EN_US_ZZZZ), LocaleUtils.isAvailableLocale(LOCALE_EN_US_ZZZZ)); assertEquals(set.contains(LOCALE_EN_US_ZZZZ), LocaleUtils.isAvailableLocale(LOCALE_EN_US_ZZZZ));
@ -401,17 +401,17 @@ public class LocaleUtilsTest extends TestCase {
* @param languages array of languages that should be returned * @param languages array of languages that should be returned
*/ */
private void assertLanguageByCountry(String country, String[] languages) { private void assertLanguageByCountry(String country, String[] languages) {
List list = LocaleUtils.languagesByCountry(country); List<Locale> list = LocaleUtils.languagesByCountry(country);
List list2 = LocaleUtils.languagesByCountry(country); List<Locale> list2 = LocaleUtils.languagesByCountry(country);
assertNotNull(list); assertNotNull(list);
assertSame(list, list2); assertSame(list, list2);
//search through langauges //search through langauges
for (int i = 0; i < languages.length; i++) { for (int i = 0; i < languages.length; i++) {
Iterator iterator = list.iterator(); Iterator<Locale> iterator = list.iterator();
boolean found = false; boolean found = false;
// see if it was returned by the set // see if it was returned by the set
while (iterator.hasNext()) { while (iterator.hasNext()) {
Locale locale = (Locale) iterator.next(); Locale locale = iterator.next();
// should have an en empty variant // should have an en empty variant
assertTrue(locale.getVariant() == null assertTrue(locale.getVariant() == null
|| locale.getVariant().length() == 0); || locale.getVariant().length() == 0);
@ -451,17 +451,17 @@ public class LocaleUtilsTest extends TestCase {
* @param countries array of countries that should be returned * @param countries array of countries that should be returned
*/ */
private void assertCountriesByLanguage(String language, String[] countries) { private void assertCountriesByLanguage(String language, String[] countries) {
List list = LocaleUtils.countriesByLanguage(language); List<Locale> list = LocaleUtils.countriesByLanguage(language);
List list2 = LocaleUtils.countriesByLanguage(language); List<Locale> list2 = LocaleUtils.countriesByLanguage(language);
assertNotNull(list); assertNotNull(list);
assertSame(list, list2); assertSame(list, list2);
//search through langauges //search through langauges
for (int i = 0; i < countries.length; i++) { for (int i = 0; i < countries.length; i++) {
Iterator iterator = list.iterator(); Iterator<Locale> iterator = list.iterator();
boolean found = false; boolean found = false;
// see if it was returned by the set // see if it was returned by the set
while (iterator.hasNext()) { while (iterator.hasNext()) {
Locale locale = (Locale) iterator.next(); Locale locale = iterator.next();
// should have an en empty variant // should have an en empty variant
assertTrue(locale.getVariant() == null assertTrue(locale.getVariant() == null
|| locale.getVariant().length() == 0); || locale.getVariant().length() == 0);
@ -492,9 +492,9 @@ public class LocaleUtilsTest extends TestCase {
/** /**
* @param coll the collection to check * @param coll the collection to check
*/ */
private static void assertUnmodifiableCollection(Collection coll) { private static void assertUnmodifiableCollection(Collection<?> coll) {
try { try {
coll.add("Unmodifiable"); coll.add(null);
fail(); fail();
} catch (UnsupportedOperationException ex) {} } catch (UnsupportedOperationException ex) {}
} }