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

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.


Solution 1:

Now an implementation of country code (ISO 3166-1 alpha-2/alpha-3/numeric) list as Java enum is available at GitHub under Apache License version 2.0.

Example:

CountryCode cc = CountryCode.getByCode("JP");

System.out.println("Country name = " + cc.getName());                // "Japan"
System.out.println("ISO 3166-1 alpha-2 code = " + cc.getAlpha2());   // "JP"
System.out.println("ISO 3166-1 alpha-3 code = " + cc.getAlpha3());   // "JPN"
System.out.println("ISO 3166-1 numeric code = " + cc.getNumeric());  // 392

Last Edit 2016-Jun-09

CountryCode enum was packaged into com.neovisionaries.i18n with other Java enums, LanguageCode (ISO 639-1), LanguageAlpha3Code (ISO 639-2), LocaleCode, ScriptCode (ISO 15924) and CurrencyCode (ISO 4217) and registered into the Maven Central Repository.

Maven

<dependency>
  <groupId>com.neovisionaries</groupId>
  <artifactId>nv-i18n</artifactId>
  <version>1.29</version>
</dependency>

Gradle

dependencies {
  compile 'com.neovisionaries:nv-i18n:1.29'
}

GitHub

https://github.com/TakahikoKawasaki/nv-i18n

Javadoc

https://takahikokawasaki.github.io/nv-i18n/

OSGi

Bundle-SymbolicName: com.neovisionaries.i18n
Export-Package: com.neovisionaries.i18n;version="1.28.0"

Solution 2:

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.

Solution 3:

Here's how I generated an enum with country code + country name:

package countryenum;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class CountryEnumGenerator {
    public static void main(String[] args) {
        String[] countryCodes = Locale.getISOCountries();
        List<Country> list = new ArrayList<Country>(countryCodes.length);

        for (String cc : countryCodes) {
            list.add(new Country(cc.toUpperCase(), new Locale("", cc).getDisplayCountry()));
        }

        Collections.sort(list);

        for (Country c : list) {
            System.out.println("/**" + c.getName() + "*/");
            System.out.println(c.getCode() + "(\"" + c.getName() + "\"),");
        }

    }
}

class Country implements Comparable<Country> {
    private String code;
    private String name;

    public Country(String code, String name) {
        super();
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    @Override
    public int compareTo(Country o) {
        return this.name.compareTo(o.name);
    }
}

Solution 4:

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.

Solution 5:

There is an easy way to generate this enum with the language name. Execute this code to generate the list of enum fields to paste :

 /**
  * This is the code used to generate the enum content
  */
 public static void main(String[] args) {
  String[] codes = java.util.Locale.getISOLanguages();
  for (String isoCode: codes) {
   Locale locale = new Locale(isoCode);
   System.out.println(isoCode.toUpperCase() + "(\"" + locale.getDisplayLanguage(locale) + "\"),");
  }
 }