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

前端 未结 11 2618
南笙
南笙 2020-12-04 10:57

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

11条回答
  •  一生所求
    2020-12-04 11:13

    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.

提交回复
热议问题