Is there an open source java enum of ISO 3166-1 co

2019-01-13 02:14发布

Does anyone know of a freely available java 1.5 package that provides a list of ISO 3166-1 country codes as a enum or EnumMap? Specifically I need the "ISO 3166-1-alpha-2 code elements", i.e. the 2 character country code like "us", "uk", "de", etc. Creating one is simple enough (although tedious), but if there's a standard one already out there in apache land or the like it would save a little time.

10条回答
够拽才男人
2楼-- · 2019-01-13 02:47

If anyone is already using the Amazon AWS SDK it includes com.amazonaws.services.route53domains.model.CountryCode. I know this is not ideal but it's an alternative if you already use the AWS SDK. For most cases I would use Takahiko's nv-i18n since, as he mentions, it implements ISO 3166-1.

查看更多
在下西门庆
3楼-- · 2019-01-13 02:50

I have created an enum, which you address by the english country name. See country-util.
On each enum you can call getLocale() to get the Java Locale.

From the Locale you can get all the information you are used to, fx the ISO-3166-1 two letter country code.

public enum Country{

    ANDORRA(new Locale("AD")),
    AFGHANISTAN(new Locale("AF")),
    ANTIGUA_AND_BARBUDA(new Locale("AG")),
    ANGUILLA(new Locale("AI")),
    //etc
    ZAMBIA(new Locale("ZM")),
    ZIMBABWE(new Locale("ZW"));

    private Locale locale;

    private Country(Locale locale){
        this.locale = locale;
    }

    public Locale getLocale(){
        return locale;
    }

Pro:

  • Light weight
  • Maps to Java Locales
  • Addressable by full country name
  • Enum values are not hardcoded, but generated by a call to Locale.getISOCountries(). That is: Simply recompile the project against the newest java version to get any changes made to the list of countries reflected in the enum.

Con:

  • Not in Maven repository
  • Most likely simpler / less expressive than the other solutions, which I don't know.
  • Created for my own needs / not as such maintained. - You should probably clone the repo.
查看更多
可以哭但决不认输i
4楼-- · 2019-01-13 02:51

This code gets 242 countries in Sun Java 6:

String[] countryCodes = Locale.getISOCountries();

Though the ISO website claims there are 249 ISO 3166-1-alpha-2 code elements, though the javadoc links to the same information.

查看更多
5楼-- · 2019-01-13 02:52

If you are already going to rely on Java locale, then I suggest using a simple HashMap instead of creating new classes for countries etc.

Here's how I would use it if I were to rely on the Java Localization only:

private HashMap<String, String> countries = new HashMap<String, String>();
String[] countryCodes = Locale.getISOCountries();

for (String cc : countryCodes) {
    // country name , country code map
    countries.put(new Locale("", cc).getDisplayCountry(), cc.toUpperCase());
}

After you fill the map, you can get the ISO code from the country name whenever you need it. Or you can make it a ISO code to Country name map as well, just modify the 'put' method accordingly.

查看更多
登录 后发表回答