Java: ISO-3166 Java Enum

Posted on Updated on

Having worked software development in a bank before I know how important it is to work with proper data structures and enumerations.

I had a program I needed to write where I had to utilize ISO country codes and there was a lot of interoperability work that needed to be done. So I tried looking for some basic library that had the standard ISO-3166 country codes. Couldn’t find any. I really just needed something I could look up a country code quickly and get its description. Like saving “SLV” or “222” as a key in a database for El Salvador’s currency. Then I can retrieve it and do a look up on a table to get the country name. Or even vice-versa.

So I went “knee deep in the dead” and coded an enum for this. I used some online resources to make this work such as http://en.wikipedia.org/wiki/ISO_3166 and http://www.iso.org/iso/home/standards/country_codes.htm
I thought I’d share it here under BSD License so that others may find it useful.

/*
 * Copyright 2011 George El-Haddad. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY GEORGE EL-HADDAD ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGE EL-HADDAD OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of George El-Haddad.
 */
public enum ISO_3166_CountryCode {
	
	AALAND_ISLANDS("Aaland Islands", "AX", "ALA", "248"),
	AFGHANISTAN("Afghanistan", "AF", "AFG", "004"),
	ALBANIA("Albania", "AL", "ALB", "008"),
	ALGERIA("Algeria", "DZ", "DZA", "012"),
	AMERICAN_SAMOA("American Samoa", "AS", "ASM", "016"),
	ANDORRA("Andorra", "AD", "AND", "020"),
	ANGOLA("Angola", "AO", "AGO", "024"),
	ANGUILLA("Anguilla", "AI", "AIA", "660"),
	ANTARCTICA("Antarctica", "AQ", "ATA", "010"),
	ANTIGUA_AND_BARBUDA("Antigua and Barbuda", "AG", "ATG", "028"),
	ARGENTINA("Argentina", "AR", "ARG", "032"),
	ARMENIA("Armenia", "AM", "ARM", "051"),
	ARUBA("Aruba", "AW", "ABW", "533"),
	AUSTRALIA("Australia", "AU", "AUS", "036"),
	AUSTRIA("Austria", "AT", "AUT", "040"),
	AZERBAIJAN("Azerbaijan", "AZ", "AZE", "031"),
	BAHAMAS("Bahamas", "BS", "BHS", "044"),
	BAHRAIN("Bahrain", "BH", "BHR", "048"),
	BANGLADESH("Bangladesh", "BD", "BGD", "050"),
	BARBADOS("Barbados", "BB", "BRB", "052"),
	BELARUS("Belarus", "BY", "BLR", "112"),
	BELGIUM("Belgium", "BE", "BEL", "056"),
	BELIZE("Belize", "BZ", "BLZ", "084"),
	BENIN("Benin", "BJ", "BEN", "204"),
	BERMUDA("Bermuda", "BM", "BMU", "060"),
	BHUTAN("Bhutan", "BT", "BTN", "064"),
	BOLIVIA("Boliva", "BO", "BOL", "068"),
	BOSNIA_AND_HERZEGOWINA("Bosnia and Herzegowina", "BA", "BIH", "070"),
	BOTSWANA("Botswana", "BW", "BWA", "072"),
	BOUVET_ISLAND("Bouvet Island", "BV", "BVT", "074"),
	BRAZIL("Brazil", "BR", "BRA", "076"),
	BRITISH_INDIAN_OCEAN_TERRITORY("British Indian Ocean Territory", "IO", "IOT", "086"),
	BRUNEI_DARUSSALAM("Brunei Darussalam", "BN", "BRN", "096"),
	BULGARIA("Bulgaria", "BG", "BGR", "100"),
	BURKINA_FASO("Burkina Faso", "BF", "BFA", "854"),
	BURUNDI("Burundi", "BI", "BDI", "108"),
	CAMBODIA("Cambodia", "KH", "KHM", "116"),
	CAMEROON("Cameroon", "CM", "CMR", "120"),
	CANADA("Canada", "CA", "CAN", "124"),
	CAPE_VERDE("Cape Verde", "CV", "CPV", "132"),
	CAYMAN_ISLANDS("Cayman Islands", "KY", "CYM", "136"),
	CENTRAL_AFRICAN_REPUBLIC("Central African Republic", "CF", "CAF", "140"),
	CHAD("Chad", "TD", "TCD", "TCD"),
	CHILE("Chile", "CL", "CHL", "152"),
	CHINA("China", "CN", "CHN", "156"),
	CHRISTMAS_ISLAND("Christmas Island", "CX", "CXR", "162"),
	COCOS_KEELING_ISLANDS("Cocos Keeling Ilands", "CC", "CCK", "166"),
	COLOMBIA("Colombia", "CO", "COL", "170"),
	COMOROS("Comoros", "KM", "COM", "174"),
	CONGO("Congo", "CG", "COG", "178"),
	COOK_ISLANDS("Cook Islands", "CK", "COK", "184"),
	COSTA_RICA("Costa Rica", "CR", "CRI", "188"),
	COTE_DIVOIRE("Cote D'Ivoire", "CI", "CIV", "384"),
	CROATIA("Croatia", "HR", "HRV", "191"),
	CUBA("Cuba", "CU", "CUB", "192"),
	CYPRUS("Cyprus", "CY", "CYP", "196"),
	CZECH_REPUBLIC("Czech Republic", "CZ", "CZE", "203"),
	DENMARK("Denmark", "DK", "DNK", "208"),
	DJIBOUTI("Djibouti", "DJ", "DJI", "262"),
	DOMINICA("Dominica", "DM", "DMA", "212"),
	DOMINICAN_REPUBLIC("Dominican Republic", "DO", "DOM", "214"),
	EAST_TIMOR("East Timor", "TP", "TMP", "626"),
	ECUADOR("Ecuador", "EC", "ECU", "218"),
	EGYPT("Egypt", "EG", "EGY", "818"),
	EL_SALVADOR("El Salvador", "SV", "SLV", "222"),
	EQUATORIAL_GUINEA("Equatorial Guinea", "GQ", "GNQ", "226"),
	ERITREA("Eritera", "ER", "ERI", "232"),
	ESTONIA("Estonia", "EE", "EST", "233"),
	ETHIOPIA("Ethiopia", "ET", "ETH", "210"),
	FALKLAND_ISLANDS("Falkland Islands", "FK", "FLK", "238"),
	FAROE_ISLANDS("Faroe Islands", "FO", "FRO", "234"),
	FIJI("Fiji", "FJ", "FJI", "242"),
	FINLAND("Finland", "FI", "FIN", "246"),
	FRANCE("France", "FR", "FRA", "250"),
	FRANCE_METROPOLITAN("France Metropolitan", "FX", "FXX", "249"),
	FRENCH_GUIANA("French Guiana", "GF", "GUF", "254"),
	FRENCH_POLYNESIA("French Polynesia", "PF", "PYF", "258"),
	FRENCH_SOUTHERN_TERRITORIES("French Southern Territories", "TF", "ATF", "260"),
	GABON("Gabon", "GA", "GAB", "266"),
	GAMBIA("Gambia", "GM", "GMB", "270"),
	GEORGIA("Georgia", "GE", "GEO", "268"),
	GERMANY("Germany", "DE", "DE", "276"),
	GHANA("Ghana", "GH", "GHA", "288"),
	GIBRALTAR("Gibraltar", "GI", "GIB", "292"),
	GREECE("Greece", "GR", "GRC", "300"),
	GREENLAND("Greenland", "GL", "GRL", "304"),
	GRENADA("Grenada", "GD", "GRD", "308"),
	GUADELOUPE("Guadeloupe", "GP", "GLP", "312"),
	GUAM("Guam", "GU", "GUM", "316"),
	GUATEMALA("Guatemala", "GT", "GTM", "320"),
	GUINEA("Guinea", "GN", "GIN", "324"),
	GUINEA_BISSAU("Guinea Bissau", "GW", "GNB", "624"),
	GUYANA("Guyana", "GY", "GUY", "328"),
	HAITI("Haiti", "HT", "HTI", "332"),
	HEARD_AND_MC_DONALD_ISLANDS("Heard and Mc'Donald Islands", "HM", "HMD", "334"),
	HONDURAS("Honduras", "HN", "HND", "340"),
	HONG_KONG("Hong Kong", "HK", "HKG", "344"),
	HUNGARY("Hungary", "HU", "HUN", "348"),
	ICELAND("Iceland", "IS", "ISL", "352"),
	INDIA("India", "IN", "IND", "356"),
	INDONESIA("Indonesia", "ID", "IDN", "360"),
	IRAN_ISLAMIC_REPUBLIC_OF("Iran, Islamic Republic of", "IR", "IRN", "364"),
	IRAQ("Iraq", "IQ", "IRQ", "368"),
	IRELAND("Ireland", "IE", "IRL", "372"),
	ITALY("Italy", "IT", "ITA", "380"),
	JAMAICA("Jamaica", "JM", "JAM", "388"),
	JAPAN("Japan", "JP", "JPN", "392"),
	JORDAN("Jordan", "JO", "JOR", "400"),
	KAZAKHSTAN("Kazakhstan", "KZ", "KAZ", "398"),
	KENYA("Kenya", "KE", "KEN", "404"),
	KIRIBATI("Kiribati", "KI", "KIR", "296"),
	KOREA_DEMOCRATIC_PEOPLES_REPUBLIC_OF("Korea, Democratic People's Republic of", "KP", "PRK", "408"),
	KOREA_REPUBLIC_OF("Korea, Republic of", "KR", "KOR", "410"),
	KUWAIT("Kuwait", "KW", "KWT", "414"),
	KYRGYZSTAN("Kyrgyzstan", "KG", "KGZ", "417"),
	LAO_PEOPLES_DEMOCRATIC_REPUBLIC("Lao, People's Democratic Republic", "LA", "LAO", "418"),
	LATVIA("Latvia", "LV", "LVA", "428"),
	LEBANON("Lebanon", "LB", "LBN", "422"),
	LESOTHO("Lesotho", "LS", "LSO", "426"),
	LIBERIA("Liberia", "LR", "LBR", "430"),
	LIBYAN_ARAB_JAMAHIRIYA("Libyan Arab Jamahiriya", "LY", "LBY", "434"),
	LIECHTENSTEIN("Liechtenstein", "LI", "LIE", "438"),
	LITHUANIA("Lithuania", "LT", "LTU", "440"),
	LUXEMBOURG("Luxembourg", "LU", "LUX", "442"),
	MACAU("Macau", "MO", "MAC", "446"),
	MACEDONIA_THE_FORMER_YUGOSLAV_REPUBLIC_OF("Macedonia, The Former Yugoslav Republic of", "MK", "MKD", "807"),
	MADAGASCAR("Madagascar", "MG", "MDG", "450"),
	MALAWI("Malawi", "MW", "MWI", "454"),
	MALAYSIA("Malaysia", "MY", "MYS", "458"),
	MALDIVES("Maldives", "MV", "MDV", "462"),
	MALI("Mali", "ML", "MLI", "466"),
	MALTA("Malta", "MT", "MLT", "470"),
	MARSHALL_ISLANDS("Marshall Islands", "MH", "MHL", "584"),
	MARTINIQUE("Martinique", "MQ", "MTQ", "474"),
	MAURITANIA("Mauritania", "MR", "MRT", "478"),
	MAURITIUS("Mauritius", "MU", "MUS", "480"),
	MAYOTTE("Mayotte", "YT", "MYT", "175"),
	MEXICO("Mexico", "MX", "MEX", "484"),
	MICRONESIA_FEDERATED_STATES_OF("Micronesia, Federated States of", "FM", "FSM", "583"),
	MOLDOVA_REPUBLIC_OF("Moldova, Republic of", "MD", "MDA", "498"),
	MONACO("Monaco", "MC", "MCO", "492"),
	MONGOLIA("Mongolia", "MN", "MNG", "496"),
	MONTSERRAT("Montserrat", "MS", "MSR", "500"),
	MOROCCO("Morocco", "MA", "MAR", "504"),
	MOZAMBIQUE("Mozambique", "MZ", "MOZ", "508"),
	MYANMAR("Myanmar", "MM", "MMR", "104"),
	NAMIBIA("Namibia", "NA", "NAM", "516"),
	NAURU("Nauru", "NR", "NRU", "520"),
	NEPAL("Nepal", "NP", "NPL", "524"),
	NETHERLANDS("Netherlands", "NL", "NLD", "528"),
	NETHERLANDS_ANTILLES("Netherlands Antilles", "AN", "ANT", "530"),
	NEW_CALEDONIA("New Caledonia", "NC", "NCL", "540"),
	NEW_ZEALAND("New Zealand", "NZ", "NZL", "554"),
	NICARAGUA("Nicaragua", "NI", "NIC", "558"),
	NIGER("Niger", "NE", "NER", "562"),
	NIGERIA("Nigeria", "NG", "NGA", "566"),
	NIUE("Niue", "NU", "NIU", "570"),
	NORFOLK_ISLAND("Norfolk Island", "NF", "NFK", "574"),
	NORTHERN_MARIANA_ISLANDS("Northern Mariana Islands", "MP", "MNP", "580"),
	NORWAY("Norway", "NO", "NO", "578"),
	OMAN("Oman", "OM", "OMN", "512"),
	PAKISTAN("Pakistan", "PK", "PAK", "586"),
	PALAU("Palau", "PW", "PLW", "585"),
	PANAMA("Panama", "PA", "PAN", "PAN"),
	PAPUA_NEW_GUINEA("Papua New Guinea", "PG", "PG", "598"),
	PARAGUAY("Paraguay", "PY", "PRY", "PRY"),
	PERU("Peru", "PE", "PER", "604"),
	PHILIPPINES("Philippines", "PH", "PH", "608"),
	PITCAIRN("Pitcairn", "PN", "PCN", "612"),
	POLAND("Poland", "PL", "PL", "616"),
	PORTUGAL("Portugal", "PT", "PRT", "620"),
	PUERTO_RICO("Puerto Rico", "PR", "PRI", "630"),
	QATAR("Qatar", "QA", "QA", "634"),
	REUNION("Reunion", "634", "REU", "638"),
	ROMANIA("Romania", "RO", "ROM", "642"),
	RUSSIAN_FEDERATION("Russian Federation", "RU", "RUS", "643"),
	RWANDA("Rawanda", "RW", "RWA", "646"),
	SAINT_KITTS_AND_NEVIS("Saint Kitts and Nevis", "KN", "KNA", "659"),
	SAINT_LUCIA("Saint Lucia", "LC", "LCA", "662"),
	SAINT_VINCENT_AND_THE_GRENADINES("Saint Vincent and the Grenadines", "VC", "VCT", "670"),
	SAMOA("Samoa", "WS", "WSM", "882"),
	SAN_MARINO("San Marino", "SM", "SMR", "674"),
	SAO_TOME_AND_PRINCIPE("Sao Tome and Principe", "ST", "STP", "678"),
	SAUDI_ARABIA("Saudi Arabia", "SA", "SAU", "682"),
	SENEGAL("Senegal", "SN", "SEN", "686"),
	SEYCHELLES("Seychelles", "SC", "SYC", "690"),
	SIERRA_LEONE("Sierral Leone", "SL", "SLE", "694"),
	SINGAPORE("Singapore", "SG", "SGP", "702"),
	SLOVAKIA("Slovakia", "SK", "SVK", "703"),
	SLOVENIA("Slovenia", "SI", "SVN", "705"),
	SOLOMON_ISLANDS("Solomon Islands", "SB", "SLB", "090"),
	SOMALIA("Somalia", "SO", "SOM", "706"),
	SOUTH_AFRICA("South Africa", "ZA", "ZAF", "710"),
	SPAIN("Spain", "ES", "ESP", "724"),
	SRI_LANKA("Sri Lanka", "LK", "LKA", "144"),
	ST_HELENA("St. Helena", "SH", "SHN", "654"),
	ST_PIERRE_AND_MIQUELON("St. Pierre and Miquelon", "PM", "SPM", "666"),
	SUDAN("Sudan", "SD", "SDN", "736"),
	SURINAME("Suriname", "SR", "SUR", "740"),
	SVALBARD_AND_JAN_MAYEN_ISLANDS("Svalbard and Jan Mayen Islands", "SJ", "SJM", "744"),
	SWAZILAND("Swaziland", "SZ", "SWZ", "748"),
	SWEDEN("Sweden", "SE", "SWE", "752"),
	SWITZERLAND("Switzerland", "CH", "CHE", "756"),
	SYRIAN_ARAB_REPUBLIC("Syrian Arab Republic", "SY", "SYR", "760"),
	TAIWAN_PROVINCE_OF_CHINA("Taiwan, Province of China", "TW", "TWN", "158"),
	TAJIKISTAN("Tajikistan", "TJ", "TJK", "762"),
	TANZANIA_UNITED_REPUBLIC_OF("Tanzania, United Republic of", "TZ", "TZA", "834"),
	THAILAND("Thailand", "TH", "THA", "764"),
	TOGO("Togo", "TG", "TGO", "768"),
	TOKELAU("Tokelau", "TK", "TKL", "772"),
	TONGA("Tonga", "TO", "TON", "776"),
	TRINIDAD_AND_TOBAGO("Trinidad and Tobago", "TT", "TTO", "780"),
	TUNISIA("Tunisia", "TN", "TUN", "788"),
	TURKEY("Turkey", "TR", "TUR", "792"),
	TURKMENISTAN("Turkmenistan", "TM", "TKM", "795"),
	TURKS_AND_CAICOS_ISLANDS("Turks and Caicos Islands", "TC", "TCA", "796"),
	TUVALU("Tuvalu", "TV", "TUV", "798"),
	UGANDA("Uganda", "UG", "UGA", "800"),
	UKRAINE("Ukraine", "UA", "UKR", "804"),
	UNITED_ARAB_EMIRATES("United Arab Emirates", "AE", "ARE", "784"),
	UNITED_KINGDOM("United Kingdome", "GB", "GBR", "826"),
	UNITED_STATES("Unites States", "US", "USA", "840"),
	UNITED_STATES_MINOR_OUTLYING_ISLANDS("United States Minor Outlying Islands", "UM", "UMI", "581"),
	URUGUAY("Uruguay", "UY", "URY", "858"),
	UZBEKISTAN("Uzbekistan", "UZ", "UZB", "860"),
	VANUATU("Vanuatu", "VU", "VUT", "548"),
	VATICAN_CITY_STATE("Vatican City State", "VA", "VAT", "336"),
	VENEZUELA("Venezuela", "VE", "VEN", "862"),
	VIETNAM("Vietnam", "VN", "VNM", "704"),
	VIRGIN_ISLANDS_BRITISH("Virgin Islands, British", "VG", "VGB", "092"),
	VIRGIN_ISLANDS_US("Virgin Islands, U.S.", "VI", "VIR", "850"),
	WALLIS_AND_FUTUNA_ISLANDS("Wallis and Futuna Islands", "WF", "WLF", "876"),
	WESTERN_SAHARA("Western Sahara", "EH", "ESH", "732"),
	YEMEN("Yemen", "YE", "YEM", "887"),
	YUGOSLAVIA("Yugoslavia", "YU", "YUG", "891"),
	ZAIRE("Zaire", "ZR", "ZAR", "180"),
	ZAMBIA("Zambia", "ZM", "ZMB", "894"),
	ZIMBABWE("Zimbabwe", "ZW", "ZWE", "716");
	
	private String countryName;
	private String A2Code;
	private String A3Code;
	private String NMCode;
	
	ISO_3166_CountryCode(String countryName, String A2Code, String A3Code, String NMCode) {
		this.countryName = countryName;
		this.A2Code = A2Code;
		this.A3Code = A3Code;
		this.NMCode = NMCode;
	}
	
	/**
	 * <p>Returns the ISO-3166 country name.</p>
	 * 
	 * @return {@link String}
	 */
	public String getCountryName()
	{
		return countryName;
	}
	
	/**
	 * <p>Returns the ISO-3166 two character country code.</p>
	 * 
	 * @return {@link String}
	 */
	public String getCountryCodeA2()
	{
		return A2Code;
	}
	
	/**
	 * <p>Returns the ISO-3166 three character country code.</p>
	 * 
	 * @return {@link String}
	 */
	public String getCountryCodeA3()
	{
		return A3Code;
	}
	
	/**
	 * <p>Returns the ISO-3166 three digit numerical country code.</p>
	 * 
	 * @return {@link String}
	 */
	public String getCountryCodeNM()
	{
		return NMCode;
	}
}
Advertisements

One thought on “Java: ISO-3166 Java Enum

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s