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

View File

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