是否有ISO 3166-1国家代码的开源java枚举

是否有ISO 3166-1国家代码的开源java枚举,java,internationalization,country-codes,iso-3166,Java,Internationalization,Country Codes,Iso 3166,有人知道免费提供的java 1.5包提供了一个ISO 3166-1国家代码列表作为枚举或枚举映射吗?具体来说,我需要“ISO 3166-1-alpha-2代码元素”,即2个字符的国家代码,如“us”、“uk”、“de”等。创建一个国家代码非常简单(虽然繁琐),但如果在apache land或类似产品中已经有一个标准国家代码,则可以节省一点时间。此代码在Sun Java 6中获得242个国家代码: String[] countryCodes = Locale.getISOCountries();

有人知道免费提供的java 1.5包提供了一个ISO 3166-1国家代码列表作为枚举或枚举映射吗?具体来说,我需要“ISO 3166-1-alpha-2代码元素”,即2个字符的国家代码,如“us”、“uk”、“de”等。创建一个国家代码非常简单(虽然繁琐),但如果在apache land或类似产品中已经有一个标准国家代码,则可以节省一点时间。

此代码在Sun Java 6中获得242个国家代码:

String[] countryCodes = Locale.getISOCountries();

尽管声称有249个ISO 3166-1-alpha-2代码元素,但链接指向相同的信息。

有一种简单的方法可以使用语言名称生成此枚举。 执行此代码以生成要粘贴的枚举字段列表:

 /**
  * 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) + "\"),");
  }
 }

以下是我如何生成带有国家代码+国家名称的枚举:

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);
    }
}
包countryenum;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Locale;
公共类枚举生成器{
公共静态void main(字符串[]args){
字符串[]countryCodes=Locale.getISOCountries();
列表=新的ArrayList(countryCodes.length);
用于(字符串cc:countrycode){
添加(新国家(cc.toUpperCase(),新地区(“,cc.getDisplayCountry()));
}
集合。排序(列表);
(c国:名单){
System.out.println(“/**”+c.getName()+“*/”;
System.out.println(c.getCode()+“(\”+c.getName()+“\”);
}
}
}
阶级国家实行可比性{
私有字符串码;
私有字符串名称;
公共国家(字符串代码、字符串名称){
超级();
this.code=代码;
this.name=名称;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@凌驾
公共国际比较(o国){
返回此.name.compareTo(o.name);
}
}

这仍然不能回答问题。我也在找一个这样的统计员,但什么也没找到。这里的一些示例使用hashtable,但表示与内置get相同

我会选择另一种方法。因此,我用python创建了一个脚本,用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

现在,国家代码(//)列表作为Java enum的实现在GitHub的Apache许可证版本2.0下可用

示例:

CountryCode cc=CountryCode.getByCode(“JP”);
System.out.println(“Country name=“+cc.getName());//“日本”
System.out.println(“ISO 3166-1 alpha-2代码=“+cc.getAlpha 2());//“JP”
System.out.println(“ISO 3166-1 alpha-3代码=“+cc.getAlpha3());//“JPN”
System.out.println(“ISO 3166-1数字代码=“+cc.getNumeric());//392

最后一次编辑2016-06-09

CountryCode枚举与其他Java枚举、LanguageCode()、LanguageAlpha3Code()、LocaleCode、ScriptCode()和CurrencyCode()一起打包到com.neovisionaries.i18n中,并注册到Maven中央存储库中

Maven


com.neovisionaries

Javadoc

OSGi

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

我不知道这个问题,直到我最近才为这个目的开放了我的Java enum!惊人的巧合

我用BSD caluse 3许可证将整个源代码放在我的博客上,所以我认为没有人会对此感到不满

可以在这里找到。


希望它有用并减轻开发痛苦。

如果您已经打算依赖Java语言环境,那么我建议使用一个简单的HashMap,而不是为国家/地区等创建新类

如果我只依赖Java本地化,我将如何使用它:

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());
}
private HashMap countries=new HashMap();
字符串[]countryCodes=Locale.getISOCountries();
用于(字符串cc:countrycode){
//国家名称、国家代码地图
countries.put(新区域设置(“,cc.getDisplayCountry(),cc.toUpperCase());
}
填完地图后,您可以随时从国家名称中获取ISO代码。
或者,您也可以将其设置为一个ISO代码到国家名称的映射,只需相应地修改“put”方法。

不是java枚举,但如果有人已经在使用Amazon AWS SDK,则可以在上获得该映射的JSON版本,其中包括
com.amazonaws.services.route53domains.model.CountryCode>。我知道这并不理想,但如果您已经使用了AWS SDK,那么这是一个替代方案。在大多数情况下,我会使用Takahiko的
nv-i18n
,因为正如他所提到的,它实现了ISO 3166-1。

我创建了一个enum,您可以用英文国家名来称呼它。请参阅。
在每个枚举上,可以调用
getLocale()
来获取Java语言环境

从区域设置中,您可以获得所有用于输入ISO-3166-1两个字母的国家代码的信息

公共枚举国家/地区{
安道尔(新地区(“AD”),
阿富汗(新地点(“AF”),
安提瓜和巴布达(新地点(“AG”),
安圭拉(新地区(“AI”),
//等
赞比亚(新地点(“ZM”),
津巴布韦(新地点(“ZW”);
私人场所;
私人国家(地区){
this.locale=locale;
}
公共语言环境getLocale(){
返回区域设置;
}
赞成者:

  • 轻量
  • 映射到Java语言环境
  • 可通过全名寻址
  • 枚举值不是硬编码的,而是通过调用Locale.getISOCountries()生成的。也就是说:只需根据最新的java版本重新编译项目,即可获得对枚举中反映的国家列表所做的任何更改
反对:

  • 不在Maven存储库中
  • 很可能比其他解决方案更简单/表达能力差,我不知道
  • 创造
    Bundle-SymbolicName: com.neovisionaries.i18n
    Export-Package: com.neovisionaries.i18n;version="1.22.0"
    
    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());
    }