Example usage for java.util Locale getISOCountries

List of usage examples for java.util Locale getISOCountries

Introduction

In this page you can find the example usage for java.util Locale getISOCountries.

Prototype

public static String[] getISOCountries() 

Source Link

Document

Returns a list of all 2-letter country codes defined in ISO 3166.

Usage

From source file:im.neon.util.PhoneNumberUtils.java

/**
 * Build the country codes list/*from   w w  w.j a v  a2  s. c o  m*/
 */
private static void buildCountryCodesList() {
    if (null == mCountryCodes) {
        // retrieve the ISO country code
        String[] isoCountryCodes = Locale.getISOCountries();
        List<Pair<String, String>> countryCodes = new ArrayList<>();

        // retrieve the human display name
        for (String countryCode : isoCountryCodes) {
            Locale locale = new Locale("", countryCode);
            countryCodes.add(new Pair<>(countryCode, locale.getDisplayCountry()));
        }

        // sort by human display names
        Collections.sort(countryCodes, new Comparator<Pair<String, String>>() {
            @Override
            public int compare(Pair<String, String> lhs, Pair<String, String> rhs) {
                return lhs.second.compareTo(rhs.second);
            }
        });

        mCountryNameByCC = new HashMap<>(isoCountryCodes.length);
        mCountryCodes = new String[isoCountryCodes.length];
        mCountryNames = new String[isoCountryCodes.length];

        for (int index = 0; index < isoCountryCodes.length; index++) {
            Pair<String, String> pair = countryCodes.get(index);

            mCountryCodes[index] = pair.first;
            mCountryNames[index] = pair.second;
            mCountryNameByCC.put(pair.first, pair.second);
        }
    }
}

From source file:com.trenako.validation.ISOCountryValidator.java

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    if (StringUtils.isBlank(value)) {
        return true;
    }/*from  w  w  w .j a v  a 2  s.  c  om*/
    return Arrays.binarySearch(Locale.getISOCountries(), value.toUpperCase()) >= 0;
}

From source file:org.openestate.io.core.LocaleUtils.java

/**
 * Create an ISO-2 country code from an ISO-3 country code.
 *
 * @param iso3Code//from  ww  w . j  av a 2 s .  c  o  m
 * ISO-3 country code
 *
 * @return
 * ISO-2 country code or null, if no code was found
 */
public static String getCountryISO2FromISO3(String iso3Code) {
    iso3Code = StringUtils.trimToNull(iso3Code);
    if (iso3Code == null)
        return null;
    if (iso3Code.length() == 3) {
        for (String iso2Code : Locale.getISOCountries()) {
            Locale countryLocale = new Locale(iso2Code, iso2Code);
            String countryISO3 = StringUtils.trimToNull(countryLocale.getISO3Country());
            if (countryISO3 != null && countryISO3.equalsIgnoreCase(iso3Code)) {
                return iso2Code;
            }
        }
    }
    return null;
}

From source file:com.prowidesoftware.swift.utils.IsoUtils.java

private IsoUtils() {
    /*/*w ww . j av a  2  s .co m*/
     * load currencies from all available locale instances
     * 
     * TODO This should be replaced by Currency.getAvailableCurrencies() once Prowide Core in migrated to Java7
     */
    currencies = new HashSet<String>();
    for (Locale locale : Locale.getAvailableLocales()) {
        try {
            String val = Currency.getInstance(locale).getCurrencyCode();
            if (!currencies.contains(val)) {
                currencies.add(val);
            }
        } catch (Exception e) {
            log.log(Level.FINEST, "error loading currencies from locale " + locale, e);
        }
    }

    countries = new HashSet<String>(Arrays.asList(Locale.getISOCountries()));

    // Add country code for Kosovo, not yet in ISO but used by SWIFT
    addCountry("XK");
}

From source file:org.alfresco.wcm.client.interceptor.ApplicationDataInterceptor.java

public void init() {
    countryCodes.addAll(Arrays.asList(Locale.getISOCountries()));
    languageCodes.addAll(Arrays.asList(Locale.getISOLanguages()));
}

From source file:org.openestate.io.core.LocaleUtils.java

/**
 * Return an ISO-3 country code from a country name.
 *
 * @param country//from   w w  w .  j a  va2  s  .c  o  m
 * country name
 *
 * @return
 * ISO-3 country code or null, if no code was found
 */
public static String getCountryISO3(String country) {
    country = StringUtils.trimToNull(country);
    if (country == null)
        return null;
    if (country.length() == 3)
        return country;

    String[] iso2Codes = Locale.getISOCountries();
    if (country.length() == 2) {
        String iso3code = LocaleUtils.getCountryISO3FromISO2(country);
        if (iso3code != null)
            return iso3code;
    }

    for (String iso2Code : iso2Codes) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        String iso3Code = StringUtils.trimToNull(countryLocale.getISO3Country());
        if (iso3Code == null)
            continue;
        for (Locale translationLocale : LocaleUtils.availableLocaleList()) {
            String name = StringUtils.trimToNull(countryLocale.getDisplayCountry(translationLocale));
            if (name != null && name.equalsIgnoreCase(country))
                return iso3Code;
        }
    }
    return null;
}

From source file:org.sisto.jeeplate.jsf.Oracle.java

private void populateCountries() {
    String[] allCountries = Locale.getISOCountries();
    for (String countryCode : allCountries) {
        Locale l = new Locale("", countryCode);
        this.countries.put(l.getCountry(), countryCode);
    }//from   w ww.  j  a  v a 2 s. c  o  m
}

From source file:com.baruckis.nanodegree.spotifystreamer.fragments.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);

    // Getting preferences from the preference resource.
    mCountryListPreference = (ListPreference) getPreferenceManager()
            .findPreference(getString(R.string.list_preference_country_code_key));
    mNotificationSwitchPreference = (SwitchPreference) getPreferenceManager()
            .findPreference(getString(R.string.switch_preference_notification_key));
    Preference sourcePreference = getPreferenceManager()
            .findPreference(getString(R.string.preference_source_key));
    Preference authorPreference = getPreferenceManager()
            .findPreference(getString(R.string.preference_author_key));
    Preference licencePreference = getPreferenceManager()
            .findPreference(getString(R.string.preference_licence_key));
    Preference versionPreference = getPreferenceManager()
            .findPreference(getString(R.string.preference_version_key));

    // Setting up to display a list of countries
    List<String> countriesCodesList = new ArrayList<String>();
    List<String> countriesNamesList = new ArrayList<String>();

    String[] countries = Locale.getISOCountries(); // will return a list of all 2-letter country codes defined in ISO 3166

    for (String countryCode : countries) {
        Locale locale = new Locale("", countryCode);
        countriesCodesList.add(locale.getCountry());
        countriesNamesList.add(locale.getDisplayCountry());
    }/*ww w .  j  a  v  a2s  .  c  o m*/

    final CharSequence[] countriesCodes = countriesCodesList
            .toArray(new CharSequence[countriesCodesList.size()]);
    final CharSequence[] countriesNames = countriesNamesList
            .toArray(new CharSequence[countriesNamesList.size()]);

    mCountryListPreference.setEntryValues(countriesCodes);
    mCountryListPreference.setEntries(countriesNames);

    mCountryListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            updateCountryListPreferenceSummary((String) newValue);
            return true;
        }
    });

    // set initial default country
    if (mCountryListPreference.getValue() == null) {
        mCountryListPreference.setValue(Locale.getDefault().getCountry());
    }
    updateCountryListPreferenceSummary(mCountryListPreference.getValue());

    mNotificationSwitchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            ((SwitchPreference) preference).setChecked((boolean) newValue);
            updateNotificationSwitchPreferenceIcon();

            // When notification visibility is changed, inform service immediately to show or stop showing notification.
            Intent notificationIntent = new Intent(PlayerService.RECEIVE_BROADCAST_INTENT_NOTIFICATION);
            notificationIntent.putExtra(PlayerService.RECEIVE_BROADCAST_INTENT_NOTIFICATION,
                    (boolean) newValue);
            LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(notificationIntent);

            return true;
        }
    });
    updateNotificationSwitchPreferenceIcon();

    // on source preference click open web browser with the url link
    sourcePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            String url = getString(R.string.preference_source_url);
            Intent viewIntent = new Intent(Intent.ACTION_VIEW);
            viewIntent.setData(Uri.parse(url));
            startActivity(viewIntent);
            return true;
        }
    });

    // on author preference click open web browser with the url link
    authorPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            String url = getString(R.string.preference_author_url);
            Intent viewIntent = new Intent(Intent.ACTION_VIEW);
            viewIntent.setData(Uri.parse(url));
            startActivity(viewIntent);
            return true;
        }
    });

    // on licence preference click open fragment dialog with the message
    licencePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {

            MessageDialogFragment
                    .newInstance(R.drawable.ic_certificate_grey600_36dp, getString(R.string.licence_title),
                            getString(R.string.licence_body, getString(R.string.app_name),
                                    Utils.getAppVersionNumber(getActivity())))
                    .show(getFragmentManager(), MessageDialogFragment.TAG);

            return true;
        }
    });

    // setting app version number
    String version = getString(R.string.preference_version_summary) + Utils.getAppVersionNumber(getActivity());
    versionPreference.setSummary(version);
}

From source file:de.cosmocode.commons.converter.LocaleCountryIsoConverter.java

@Override
public String toAlpha2(String iso3166Alpha3) {
    Preconditions.checkNotNull(iso3166Alpha3, "iso3166Alpha3 must not be null");
    if (Patterns.ISO_3166_1_ALPHA_2.matcher(iso3166Alpha3).matches()) {
        // already ISO 3166-1 alpha-2 (two letter)
        return iso3166Alpha3;
    } else if (StringUtils.isBlank(iso3166Alpha3)) {
        // this is here for convenience, to allow empty languages
        return TrimMode.EMPTY.apply(iso3166Alpha3);
    }//  w w  w  .jav a2  s  .  c  o  m
    Preconditions.checkArgument(Patterns.ISO_3166_1_ALPHA_3.matcher(iso3166Alpha3).matches(),
            "Language Code %s not in ISO 3166 alpha-3", iso3166Alpha3);

    // try to get two letter code from cache
    final String fromCache = cache.get(iso3166Alpha3);
    if (fromCache != null) {
        return fromCache;
    }

    // search for three letter code in the known country codes of Locale
    for (final String alpha2 : Locale.getISOCountries()) {
        final String alpha3 = new Locale("", alpha2).getISO3Country();
        if (iso3166Alpha3.equals(alpha3)) {
            LOG.trace("Found alpha-2: {} for alpha-3: {}", alpha2, alpha3);
            cache.put(iso3166Alpha3, alpha2);
            return alpha2;
        }
    }

    // if we arrive here then the Locale class could not find the iso3 code
    throw new IsoConversionException("No known alpha-2 code for " + iso3166Alpha3);
}

From source file:SimpleRefDataExample.java

protected void processMessage(Message msg) throws Exception {

    HashMap<String, String> map = new HashMap<String, String>();

    String[] locales = Locale.getISOCountries();
    for (String countryCode : locales) {
        Locale loc = new Locale("", countryCode);
        map.put(loc.getDisplayCountry().toLowerCase(), loc.getCountry());
    }/*from  w  ww  . j av  a  2  s  . c  o m*/
    //System.out.println (map);
    //   Gson gson = null;
    //StringBuffer sb = new StringBuffer();
    String s = null;
    Element securityDataArray = msg.getElement(SECURITY_DATA);
    int numSecurities = securityDataArray.numValues();
    for (int i = 0; i < numSecurities; ++i) {
        Element securityData = securityDataArray.getValueAsElement(i);
        //System.out.println(securityData.getElementAsString(SECURITY));
        Element fieldData = securityData.getElement(FIELD_DATA);
        for (int j = 0; j < fieldData.numElements(); ++j) {
            Element field = fieldData.getElement(j);

            if (field.isNull()) {
                System.out.println(field.name() + " is NULL.");
            } else {
                String str = field.getValueAsString();
                if (j % 2 == 0)
                    s = str;
                else {
                    String countryCode = map.get(s.toLowerCase());
                    if (countryCode != null) {
                        //                System.out.println(str + " " + countryCode);
                        //if (map.containsKey(s))
                        obj.put(countryCode, Double.parseDouble(str));
                    }

                }
                //field.getValueAsString().put(obj);
                //obj.put(str[0], str[1]);

            }
        }

        Element fieldExceptionArray = securityData.getElement(FIELD_EXCEPTIONS);
        for (int k = 0; k < fieldExceptionArray.numValues(); ++k) {
            Element fieldException = fieldExceptionArray.getValueAsElement(k);
            /* System.out.println(
                    fieldException.getElement(ERROR_INFO).getElementAsString("category")
                    + ": " + fieldException.getElementAsString(FIELD_ID));*/
        }
    }

}