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:31

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);
    }
}
查看更多
女痞
3楼-- · 2019-01-13 02:43

This still does not answer the question. I was also looking for a kind of enumerator for this, and did not find anything. Some examples using hashtable here, but represent the same as the built-in get

I would go for a different approach. So I created a script in python to automatically generate the list in Java:

#!/usr/bin/python
f = open("data.txt", 'r')
data = []
cc = {}

for l in f:
    t = l.split('\t')
    cc = { 'code': str(t[0]).strip(), 
           'name': str(t[1]).strip()
    }
    data.append(cc)
f.close()

for c in data:
    print """
/**
 * Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a> 
 * for <b><i>%(name)s</i></b>.
 * <p>
 * This constant holds the value of <b>{@value}</b>.
 *
 * @since 1.0
 *
 */
 public static final String %(code)s = \"%(code)s\";""" % c

where the data.txt file is a simple copy&paste from Wikipedia table (just remove all extra lines, making sure you have a country code and country name per line).

Then just place this into your static class:

/**
 * Holds <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a>
 * constant values for all countries. 
 * 
 * @since 1.0
 * 
 * </p>
 */
public class CountryCode {

    /**
     * Constructor defined as <code>private</code> purposefully to ensure this 
     * class is only used to access its static properties and/or methods.  
     */
    private CountryCode() { }

    /**
     * Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a> 
     * for <b><i>Andorra</i></b>.
     * <p>
     * This constant holds the value of <b>{@value}</b>.
     *
     * @since 1.0
     *
     */
     public static final String AD = "AD";

         //
         // and the list goes on! ...
         //
}
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-13 02:44

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) + "\"),");
  }
 }
查看更多
我只想做你的唯一
5楼-- · 2019-01-13 02:44

Not a java enum, but a JSON version of this is available at http://country.io/names.json

查看更多
来,给爷笑一个
6楼-- · 2019-01-13 02:46

I didn't know about this question till I had just recently open-sourced my Java enum for exactly this purpose! Amazing coincidence!

I put the whole source code on my blog with BSD caluse 3 license so I don't think anyone would have any beefs about it.

Can be found here. https://subversivebytes.wordpress.com/2013/10/07/java-iso-3166-java-enum/

Hope it is useful and eases development pains.

查看更多
我命由我不由天
7楼-- · 2019-01-13 02:47

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.22</version>
</dependency>

Gradle

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

GitHub

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

Javadoc

http://takahikokawasaki.github.com/nv-i18n/

OSGi

Bundle-SymbolicName: com.neovisionaries.i18n
Export-Package: com.neovisionaries.i18n;version="1.22.0"
查看更多
登录 后发表回答