de.thksystems.util.text.LocaleUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cumin Show documentation
Show all versions of cumin Show documentation
Commons for lang, crypto, xml, dom, text, csv, reflection, annotations, parsing, ...
/*
* tksCommons
*
* Author : Thomas Kuhlmann (ThK-Systems, http://www.thk-systems.de) License : LGPL (https://www.gnu.org/licenses/lgpl.html)
*/
package de.thksystems.util.text;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ClassUtils;
import com.ibm.icu.util.ULocale;
import de.thksystems.util.lang.Deferred;
/**
* Accesses locales from JDK or ICU (preferred).
*/
public final class LocaleUtils {
private static final Logger LOG = Logger.getLogger(LocaleUtils.class.getName());
private static final Deferred localeDelegate = new Deferred<>(() -> LocaleDelegate.getImplementation());
private static final Deferred> countryCodes = new Deferred<>(() -> localeDelegate.get().getCountryCodes());
private static final Deferred> currencyCodes = new Deferred<>(() -> localeDelegate.get().getCurrencyCodes());
private LocaleUtils() {
// It is a util class
}
public static boolean isValidCountryCode(String countryCode) {
return countryCodes.get().contains(countryCode);
}
public static boolean isValidCurrencyCode(String currencyCode) {
return currencyCodes.get().contains(currencyCode);
}
static interface LocaleDelegate {
Collection getCountryCodes();
Collection getCurrencyCodes();
static LocaleDelegate getImplementation() {
try {
ClassUtils.getClass("com.ibm.icu.util.ULocale");
ClassUtils.getClass("com.ibm.icu.util.Currency");
LOG.info("Using ICU implementation");
return new ICUDelegate();
} catch (ClassNotFoundException e) {
LOG.info("Using JDK implementation");
return new JDKDelegate();
}
}
};
static class JDKDelegate implements LocaleDelegate {
@Override
public Collection getCountryCodes() {
return Arrays.asList(Locale.getISOCountries());
}
@Override
public Collection getCurrencyCodes() {
return java.util.Currency.getAvailableCurrencies().stream().map(c -> c.getCurrencyCode()).collect(Collectors.toSet());
}
}
static class ICUDelegate implements LocaleDelegate {
@Override
public Collection getCountryCodes() {
return Arrays.asList(ULocale.getISOCountries());
}
@Override
public Collection getCurrencyCodes() {
return com.ibm.icu.util.Currency.getAvailableCurrencies().stream().map(c -> c.getCurrencyCode()).collect(Collectors.toSet());
}
}
}