All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.thksystems.util.text.LocaleUtils Maven / Gradle / Ivy

/*
 * 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.List;
import java.util.Locale;
import java.util.logging.Logger;

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 LocaleUtils() {
	}

	public static boolean isValidCountryCode(String countryCode) {
		return countryCodes.get().contains(countryCode);
	}

	static interface LocaleDelegate {

		List getCountryCodes();

		static LocaleDelegate getImplementation() {
			try {
				ClassUtils.getClass("com.ibm.icu.util.ULocale");
				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 List getCountryCodes() {
			return Arrays.asList(Locale.getISOCountries());
		}

	}

	static class ICUDelegate implements LocaleDelegate {

		@Override
		public List getCountryCodes() {
			return Arrays.asList(ULocale.getISOCountries());
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy