Skip to content

Instantly share code, notes, and snippets.

@daiplusplus
Last active November 1, 2022 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daiplusplus/942148c1f5dbe6d456a38c6d4a938525 to your computer and use it in GitHub Desktop.
Save daiplusplus/942148c1f5dbe6d456a38c6d4a938525 to your computer and use it in GitHub Desktop.
Countries-and-US-CA-subdivisions
public class AddressDatabase
{
public static AddressDatabase Instance { get; } = new AddressDatabase();
private AddressDatabase()
{
this.CountriesByAllNames = Country.Countries.SelectMany( c => c.AllNames.Select( n => ( country: c, name: n ) ) ).ToDictionary( t => t.name, t => t.country, StringComparer.OrdinalIgnoreCase );
this.CountriesByIso31661Alpha2 = Country.Countries.ToDictionary( c => c.Iso31661Alpha2, StringComparer.OrdinalIgnoreCase );
this.CountriesByIso31661Alpha3 = Country.Countries.ToDictionary( c => c.Iso31661Alpha3, StringComparer.OrdinalIgnoreCase );
this.USStatesByName = Subdivision.USStates.ToDictionary( s => s.Name , StringComparer.OrdinalIgnoreCase );
this.USStatesByAbbreviation = Subdivision.USStates.ToDictionary( s => s.Abbreviation, StringComparer.OrdinalIgnoreCase );
this.CanadianProvincesByName = Subdivision.CanadianSubdivisions.ToDictionary( s => s.Name , StringComparer.OrdinalIgnoreCase );
this.CanadianProvincesByAbbreviation = Subdivision.CanadianSubdivisions.ToDictionary( s => s.Abbreviation, StringComparer.OrdinalIgnoreCase );
this.UnitedStates = Country.Countries.First( c => c.Iso31661Alpha2 == "US" );
this.Canada = Country.Countries.First( c => c.Iso31661Alpha2 == "CA" );
//
this.USStatesByNameOrAbbreviation = Array.Empty<( Subdivision s, String key )>()
.Concat( Subdivision.USStates.Select( s => ( s, key: s.Name ) ) )
.Concat( Subdivision.USStates.Select( s => ( s, key: s.Abbreviation ) ) )
.ToDictionary( t => t.key, t => t.s, StringComparer.OrdinalIgnoreCase );
this.CanadianProvincesByNameOrAbbreviation = Array.Empty<( Subdivision s, String key )>()
.Concat( Subdivision.CanadianSubdivisions.Select( s => ( s, key: s.Name ) ) )
.Concat( Subdivision.CanadianSubdivisions.Select( s => ( s, key: s.Abbreviation ) ) )
.ToDictionary( t => t.key, t => t.s, StringComparer.OrdinalIgnoreCase );
}
public IReadOnlyList<Country> Countries => Country.Countries;
public IReadOnlyDictionary<String,Country> CountriesByAllNames { get; }
public IReadOnlyDictionary<String,Country> CountriesByIso31661Alpha2 { get; }
public IReadOnlyDictionary<String,Country> CountriesByIso31661Alpha3 { get; }
public Country UnitedStates { get; }
public Country Canada { get; }
public IReadOnlyList<Subdivision> USStates => Subdivision.USStates;
public IReadOnlyDictionary<String,Subdivision> USStatesByName { get; }
public IReadOnlyDictionary<String,Subdivision> USStatesByAbbreviation { get; }
public IReadOnlyDictionary<String,Subdivision> USStatesByNameOrAbbreviation { get; }
public IReadOnlyList<Subdivision> CanadianProvinces => Subdivision.CanadianSubdivisions;
public IReadOnlyDictionary<String,Subdivision> CanadianProvincesByName { get; }
public IReadOnlyDictionary<String,Subdivision> CanadianProvincesByAbbreviation { get; }
public IReadOnlyDictionary<String,Subdivision> CanadianProvincesByNameOrAbbreviation { get; }
}
public sealed class Country : IEquatable<Country>
{
// This list was pulled from Wikipedia's https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes which in-turn is a regurgitation of the ISO standard.
// ...consequently it contains entries some might deem problematic, such as how Taiwan is named "Taiwan (Province of China)" or the absence of Kosovo, etc.
public static IReadOnlyList<Country> Countries { get; } = new Country[]
{
new Country( name: "Afghanistan" , alpha2: "AF", alpha3: "AFG", e164Code: "+93" ),
new Country( name: "Åland Islands" , alpha2: "AX", alpha3: "ALA", e164Code: "+35818" ),
new Country( name: "Albania" , alpha2: "AL", alpha3: "ALB", e164Code: "+355" ),
new Country( name: "Algeria" , alpha2: "DZ", alpha3: "DZA", e164Code: "+213" ),
new Country( name: "American Samoa" , alpha2: "AS", alpha3: "ASM" ),
new Country( name: "Andorra" , alpha2: "AD", alpha3: "AND" ),
new Country( name: "Angola" , alpha2: "AO", alpha3: "AGO" ),
new Country( name: "Anguilla" , alpha2: "AI", alpha3: "AIA" ),
new Country( name: "Antarctica" , alpha2: "AQ", alpha3: "ATA" ),
new Country( name: "Antigua and Barbuda" , alpha2: "AG", alpha3: "ATG" ),
new Country( name: "Argentina" , alpha2: "AR", alpha3: "ARG" ),
new Country( name: "Armenia" , alpha2: "AM", alpha3: "ARM" ),
new Country( name: "Aruba" , alpha2: "AW", alpha3: "ABW" ),
new Country( name: "Australia" , alpha2: "AU", alpha3: "AUS" ),
new Country( name: "Austria" , alpha2: "AT", alpha3: "AUT" ),
new Country( name: "Azerbaijan" , alpha2: "AZ", alpha3: "AZE" ),
new Country( name: "Bahamas, The" , alpha2: "BS", alpha3: "BHS" ),
new Country( name: "Bahrain" , alpha2: "BH", alpha3: "BHR" ),
new Country( name: "Bangladesh" , alpha2: "BD", alpha3: "BGD" ),
new Country( name: "Barbados" , alpha2: "BB", alpha3: "BRB" ),
new Country( name: "Belarus" , alpha2: "BY", alpha3: "BLR" ),
new Country( name: "Belgium" , alpha2: "BE", alpha3: "BEL" ),
new Country( name: "Belize" , alpha2: "BZ", alpha3: "BLZ" ),
new Country( name: "Benin" , alpha2: "BJ", alpha3: "BEN" ),
new Country( name: "Bermuda" , alpha2: "BM", alpha3: "BMU" ),
new Country( name: "Bhutan" , alpha2: "BT", alpha3: "BTN" ),
new Country( name: "Bolivia (Plurinational State of)" , alpha2: "BO", alpha3: "BOL" ),
new Country( name: "Bonaire" , alpha2: "BQ", alpha3: "BES" ),
new Country( name: "Bosnia and Herzegovina" , alpha2: "BA", alpha3: "BIH" ),
new Country( name: "Botswana" , alpha2: "BW", alpha3: "BWA" ),
new Country( name: "Bouvet Island" , alpha2: "BV", alpha3: "BVT" ),
new Country( name: "Brazil" , alpha2: "BR", alpha3: "BRA" ),
new Country( name: "British Indian Ocean Territory, The" , alpha2: "IO", alpha3: "IOT" ),
new Country( name: "Brunei Darussalam" , alpha2: "BN", alpha3: "BRN" ),
new Country( name: "Bulgaria" , alpha2: "BG", alpha3: "BGR" ),
new Country( name: "Burkina Faso" , alpha2: "BF", alpha3: "BFA" ),
new Country( name: "Burundi" , alpha2: "BI", alpha3: "BDI" ),
new Country( name: "Cabo Verde" , alpha2: "CV", alpha3: "CPV" ),
new Country( name: "Cambodia" , alpha2: "KH", alpha3: "KHM" ),
new Country( name: "Cameroon" , alpha2: "CM", alpha3: "CMR" ),
new Country( name: "Canada" , alpha2: "CA", alpha3: "CAN", e164Code: "+1" ),
new Country( name: "Cayman Islands, The" , alpha2: "KY", alpha3: "CYM" ),
new Country( name: "Central African Republic, The" , alpha2: "CF", alpha3: "CAF" ),
new Country( name: "Chad" , alpha2: "TD", alpha3: "TCD" ),
new Country( name: "Chile" , alpha2: "CL", alpha3: "CHL" ),
new Country( name: "China" , alpha2: "CN", alpha3: "CHN" ),
new Country( name: "Christmas Island" , alpha2: "CX", alpha3: "CXR" ),
new Country( name: "Cocos (Keeling) Islands, The" , alpha2: "CC", alpha3: "CCK" ),
new Country( name: "Colombia" , alpha2: "CO", alpha3: "COL" ),
new Country( name: "Comoros, The" , alpha2: "KM", alpha3: "COM" ),
new Country( name: "Congo (the Democratic Republic of the)" , alpha2: "CD", alpha3: "COD" ),
new Country( name: "Congo, The" , alpha2: "CG", alpha3: "COG" ),
new Country( name: "Cook Islands, The" , alpha2: "CK", alpha3: "COK" ),
new Country( name: "Costa Rica" , alpha2: "CR", alpha3: "CRI" ),
new Country( name: "Côte d'Ivoire" , alpha2: "CI", alpha3: "CIV" ),
new Country( name: "Croatia" , alpha2: "HR", alpha3: "HRV" ),
new Country( name: "Cuba" , alpha2: "CU", alpha3: "CUB" ),
new Country( name: "Curaçao" , alpha2: "CW", alpha3: "CUW" ),
new Country( name: "Cyprus" , alpha2: "CY", alpha3: "CYP" ),
new Country( name: "Czechia" , alpha2: "CZ", alpha3: "CZE" ),
new Country( name: "Denmark" , alpha2: "DK", alpha3: "DNK" ),
new Country( name: "Djibouti" , alpha2: "DJ", alpha3: "DJI" ),
new Country( name: "Dominica" , alpha2: "DM", alpha3: "DMA" ),
new Country( name: "Dominican Republic, The" , alpha2: "DO", alpha3: "DOM" ),
new Country( name: "Ecuador" , alpha2: "EC", alpha3: "ECU" ),
new Country( name: "Egypt" , alpha2: "EG", alpha3: "EGY" ),
new Country( name: "El Salvador" , alpha2: "SV", alpha3: "SLV" ),
new Country( name: "Equatorial Guinea" , alpha2: "GQ", alpha3: "GNQ" ),
new Country( name: "Eritrea" , alpha2: "ER", alpha3: "ERI" ),
new Country( name: "Estonia" , alpha2: "EE", alpha3: "EST" ),
new Country( name: "Eswatini" , alpha2: "SZ", alpha3: "SWZ" ),
new Country( name: "Ethiopia" , alpha2: "ET", alpha3: "ETH" ),
new Country( name: "Falkland Islands, The [Malvinas]" , alpha2: "FK", alpha3: "FLK" ),
new Country( name: "Faroe Islands, The" , alpha2: "FO", alpha3: "FRO" ),
new Country( name: "Fiji" , alpha2: "FJ", alpha3: "FJI" ),
new Country( name: "Finland" , alpha2: "FI", alpha3: "FIN" ),
new Country( name: "France" , alpha2: "FR", alpha3: "FRA" ),
new Country( name: "French Guiana" , alpha2: "GF", alpha3: "GUF" ),
new Country( name: "French Polynesia" , alpha2: "PF", alpha3: "PYF" ),
new Country( name: "French Southern Territories, The" , alpha2: "TF", alpha3: "ATF" ),
new Country( name: "Gabon" , alpha2: "GA", alpha3: "GAB" ),
new Country( name: "Gambia, The" , alpha2: "GM", alpha3: "GMB" ),
new Country( name: "Georgia" , alpha2: "GE", alpha3: "GEO" ),
new Country( name: "Germany" , alpha2: "DE", alpha3: "DEU" ),
new Country( name: "Ghana" , alpha2: "GH", alpha3: "GHA" ),
new Country( name: "Gibraltar" , alpha2: "GI", alpha3: "GIB" ),
new Country( name: "Greece" , alpha2: "GR", alpha3: "GRC" ),
new Country( name: "Greenland" , alpha2: "GL", alpha3: "GRL" ),
new Country( name: "Grenada" , alpha2: "GD", alpha3: "GRD" ),
new Country( name: "Guadeloupe" , alpha2: "GP", alpha3: "GLP" ),
new Country( name: "Guam" , alpha2: "GU", alpha3: "GUM" ),
new Country( name: "Guatemala" , alpha2: "GT", alpha3: "GTM" ),
new Country( name: "Guernsey" , alpha2: "GG", alpha3: "GGY" ),
new Country( name: "Guinea" , alpha2: "GN", alpha3: "GIN" ),
new Country( name: "Guinea-Bissau" , alpha2: "GW", alpha3: "GNB" ),
new Country( name: "Guyana" , alpha2: "GY", alpha3: "GUY" ),
new Country( name: "Haiti" , alpha2: "HT", alpha3: "HTI" ),
new Country( name: "Heard Island and McDonald Islands" , alpha2: "HM", alpha3: "HMD" ),
new Country( name: "Holy See, The" , alpha2: "VA", alpha3: "VAT" ),
new Country( name: "Honduras" , alpha2: "HN", alpha3: "HND" ),
new Country( name: "Hong Kong" , alpha2: "HK", alpha3: "HKG" ),
new Country( name: "Hungary" , alpha2: "HU", alpha3: "HUN" ),
new Country( name: "Iceland" , alpha2: "IS", alpha3: "ISL" ),
new Country( name: "India" , alpha2: "IN", alpha3: "IND" ),
new Country( name: "Indonesia" , alpha2: "ID", alpha3: "IDN" ),
new Country( name: "Iran (Islamic Republic of)" , alpha2: "IR", alpha3: "IRN" ),
new Country( name: "Iraq" , alpha2: "IQ", alpha3: "IRQ" ),
new Country( name: "Ireland" , alpha2: "IE", alpha3: "IRL" ),
new Country( name: "Isle of Man" , alpha2: "IM", alpha3: "IMN" ),
new Country( name: "Israel" , alpha2: "IL", alpha3: "ISR" ),
new Country( name: "Italy" , alpha2: "IT", alpha3: "ITA" ),
new Country( name: "Jamaica" , alpha2: "JM", alpha3: "JAM" ),
new Country( name: "Japan" , alpha2: "JP", alpha3: "JPN" ),
new Country( name: "Jersey" , alpha2: "JE", alpha3: "JEY" ),
new Country( name: "Jordan" , alpha2: "JO", alpha3: "JOR" ),
new Country( name: "Kazakhstan" , alpha2: "KZ", alpha3: "KAZ" ),
new Country( name: "Kenya" , alpha2: "KE", alpha3: "KEN" ),
new Country( name: "Kiribati" , alpha2: "KI", alpha3: "KIR" ),
new Country( name: "Korea (the Democratic People's Republic of)" , alpha2: "KP", alpha3: "PRK" ),
new Country( name: "Korea (the Republic of)" , alpha2: "KR", alpha3: "KOR" ),
new Country( name: "Kuwait" , alpha2: "KW", alpha3: "KWT" ),
new Country( name: "Kyrgyzstan" , alpha2: "KG", alpha3: "KGZ" ),
new Country( name: "Lao People's Democratic Republic, The" , alpha2: "LA", alpha3: "LAO" ),
new Country( name: "Latvia" , alpha2: "LV", alpha3: "LVA" ),
new Country( name: "Lebanon" , alpha2: "LB", alpha3: "LBN" ),
new Country( name: "Lesotho" , alpha2: "LS", alpha3: "LSO" ),
new Country( name: "Liberia" , alpha2: "LR", alpha3: "LBR" ),
new Country( name: "Libya" , alpha2: "LY", alpha3: "LBY" ),
new Country( name: "Liechtenstein" , alpha2: "LI", alpha3: "LIE" ),
new Country( name: "Lithuania" , alpha2: "LT", alpha3: "LTU" ),
new Country( name: "Luxembourg" , alpha2: "LU", alpha3: "LUX" ),
new Country( name: "Macao" , alpha2: "MO", alpha3: "MAC" ),
new Country( name: "North Macedonia" , alpha2: "MK", alpha3: "MKD" ),
new Country( name: "Madagascar" , alpha2: "MG", alpha3: "MDG" ),
new Country( name: "Malawi" , alpha2: "MW", alpha3: "MWI" ),
new Country( name: "Malaysia" , alpha2: "MY", alpha3: "MYS" ),
new Country( name: "Maldives" , alpha2: "MV", alpha3: "MDV" ),
new Country( name: "Mali" , alpha2: "ML", alpha3: "MLI" ),
new Country( name: "Malta" , alpha2: "MT", alpha3: "MLT" ),
new Country( name: "Marshall Islands, The" , alpha2: "MH", alpha3: "MHL" ),
new Country( name: "Martinique" , alpha2: "MQ", alpha3: "MTQ" ),
new Country( name: "Mauritania" , alpha2: "MR", alpha3: "MRT" ),
new Country( name: "Mauritius" , alpha2: "MU", alpha3: "MUS" ),
new Country( name: "Mayotte" , alpha2: "YT", alpha3: "MYT" ),
new Country( name: "Mexico" , alpha2: "MX", alpha3: "MEX", e164Code: "+52" ),
new Country( name: "Micronesia (Federated States of)" , alpha2: "FM", alpha3: "FSM" ),
new Country( name: "Moldova (the Republic of)" , alpha2: "MD", alpha3: "MDA" ),
new Country( name: "Monaco" , alpha2: "MC", alpha3: "MCO" ),
new Country( name: "Mongolia" , alpha2: "MN", alpha3: "MNG" ),
new Country( name: "Montenegro" , alpha2: "ME", alpha3: "MNE" ),
new Country( name: "Montserrat" , alpha2: "MS", alpha3: "MSR" ),
new Country( name: "Morocco" , alpha2: "MA", alpha3: "MAR" ),
new Country( name: "Mozambique" , alpha2: "MZ", alpha3: "MOZ" ),
new Country( name: "Myanmar" , alpha2: "MM", alpha3: "MMR" ),
new Country( name: "Namibia" , alpha2: "NA", alpha3: "NAM" ),
new Country( name: "Nauru" , alpha2: "NR", alpha3: "NRU" ),
new Country( name: "Nepal" , alpha2: "NP", alpha3: "NPL" ),
new Country( name: "Netherlands, The" , alpha2: "NL", alpha3: "NLD" ),
new Country( name: "New Caledonia" , alpha2: "NC", alpha3: "NCL" ),
new Country( name: "New Zealand" , alpha2: "NZ", alpha3: "NZL" ),
new Country( name: "Nicaragua" , alpha2: "NI", alpha3: "NIC" ),
new Country( name: "Niger, The" , alpha2: "NE", alpha3: "NER" ),
new Country( name: "Nigeria" , alpha2: "NG", alpha3: "NGA" ),
new Country( name: "Niue" , alpha2: "NU", alpha3: "NIU" ),
new Country( name: "Norfolk Island" , alpha2: "NF", alpha3: "NFK" ),
new Country( name: "Northern Mariana Islands, The" , alpha2: "MP", alpha3: "MNP" ),
new Country( name: "Norway" , alpha2: "NO", alpha3: "NOR" ),
new Country( name: "Oman" , alpha2: "OM", alpha3: "OMN" ),
new Country( name: "Pakistan" , alpha2: "PK", alpha3: "PAK" ),
new Country( name: "Palau" , alpha2: "PW", alpha3: "PLW" ),
new Country( name: "Palestine, State of" , alpha2: "PS", alpha3: "PSE" ),
new Country( name: "Panama" , alpha2: "PA", alpha3: "PAN" ),
new Country( name: "Papua New Guinea" , alpha2: "PG", alpha3: "PNG" ),
new Country( name: "Paraguay" , alpha2: "PY", alpha3: "PRY" ),
new Country( name: "Peru" , alpha2: "PE", alpha3: "PER" ),
new Country( name: "Philippines, The" , alpha2: "PH", alpha3: "PHL" ),
new Country( name: "Pitcairn" , alpha2: "PN", alpha3: "PCN" ),
new Country( name: "Poland" , alpha2: "PL", alpha3: "POL" ),
new Country( name: "Portugal" , alpha2: "PT", alpha3: "PRT" ),
new Country( name: "Puerto Rico" , alpha2: "PR", alpha3: "PRI" ),
new Country( name: "Qatar" , alpha2: "QA", alpha3: "QAT" ),
new Country( name: "Réunion" , alpha2: "RE", alpha3: "REU" ),
new Country( name: "Romania" , alpha2: "RO", alpha3: "ROU" ),
new Country( name: "Russian Federation, The" , alpha2: "RU", alpha3: "RUS" ),
new Country( name: "Rwanda" , alpha2: "RW", alpha3: "RWA" ),
new Country( name: "Saint Barthélemy" , alpha2: "BL", alpha3: "BLM" ),
new Country( name: "Saint Helena" , alpha2: "SH", alpha3: "SHN" ),
new Country( name: "Saint Kitts and Nevis" , alpha2: "KN", alpha3: "KNA" ),
new Country( name: "Saint Lucia" , alpha2: "LC", alpha3: "LCA" ),
new Country( name: "Saint Martin (French part)" , alpha2: "MF", alpha3: "MAF" ),
new Country( name: "Saint Pierre and Miquelon" , alpha2: "PM", alpha3: "SPM" ),
new Country( name: "Saint Vincent and the Grenadines" , alpha2: "VC", alpha3: "VCT" ),
new Country( name: "Samoa" , alpha2: "WS", alpha3: "WSM" ),
new Country( name: "San Marino" , alpha2: "SM", alpha3: "SMR" ),
new Country( name: "Sao Tome and Principe" , alpha2: "ST", alpha3: "STP" ),
new Country( name: "Saudi Arabia" , alpha2: "SA", alpha3: "SAU" ),
new Country( name: "Senegal" , alpha2: "SN", alpha3: "SEN" ),
new Country( name: "Serbia" , alpha2: "RS", alpha3: "SRB" ),
new Country( name: "Seychelles" , alpha2: "SC", alpha3: "SYC" ),
new Country( name: "Sierra Leone" , alpha2: "SL", alpha3: "SLE" ),
new Country( name: "Singapore" , alpha2: "SG", alpha3: "SGP" ),
new Country( name: "Sint Maarten (Dutch part)" , alpha2: "SX", alpha3: "SXM" ),
new Country( name: "Slovakia" , alpha2: "SK", alpha3: "SVK" ),
new Country( name: "Slovenia" , alpha2: "SI", alpha3: "SVN" ),
new Country( name: "Solomon Islands" , alpha2: "SB", alpha3: "SLB" ),
new Country( name: "Somalia" , alpha2: "SO", alpha3: "SOM" ),
new Country( name: "South Africa" , alpha2: "ZA", alpha3: "ZAF" ),
new Country( name: "South Georgia and the South Sandwich Islands", alpha2: "GS", alpha3: "SGS" ),
new Country( name: "South Sudan" , alpha2: "SS", alpha3: "SSD" ),
new Country( name: "Spain" , alpha2: "ES", alpha3: "ESP" ),
new Country( name: "Sri Lanka" , alpha2: "LK", alpha3: "LKA" ),
new Country( name: "Sudan, The" , alpha2: "SD", alpha3: "SDN" ),
new Country( name: "Suriname" , alpha2: "SR", alpha3: "SUR" ),
new Country( name: "Svalbard" , alpha2: "SJ", alpha3: "SJM" ),
new Country( name: "Sweden" , alpha2: "SE", alpha3: "SWE" ),
new Country( name: "Switzerland" , alpha2: "CH", alpha3: "CHE" ),
new Country( name: "Syrian Arab Republic, The" , alpha2: "SY", alpha3: "SYR" ),
new Country( name: "Taiwan (Province of China)" , alpha2: "TW", alpha3: "TWN" ),
new Country( name: "Tajikistan" , alpha2: "TJ", alpha3: "TJK" ),
new Country( name: "Tanzania, the United Republic of" , alpha2: "TZ", alpha3: "TZA" ),
new Country( name: "Thailand" , alpha2: "TH", alpha3: "THA" ),
new Country( name: "Timor-Leste" , alpha2: "TL", alpha3: "TLS" ),
new Country( name: "Togo" , alpha2: "TG", alpha3: "TGO" ),
new Country( name: "Tokelau" , alpha2: "TK", alpha3: "TKL" ),
new Country( name: "Tonga" , alpha2: "TO", alpha3: "TON" ),
new Country( name: "Trinidad and Tobago" , alpha2: "TT", alpha3: "TTO" ),
new Country( name: "Tunisia" , alpha2: "TN", alpha3: "TUN" ),
new Country( name: "Turkey" , alpha2: "TR", alpha3: "TUR" ),
new Country( name: "Turkmenistan" , alpha2: "TM", alpha3: "TKM" ),
new Country( name: "Turks and Caicos Islands, The" , alpha2: "TC", alpha3: "TCA" ),
new Country( name: "Tuvalu" , alpha2: "TV", alpha3: "TUV" ),
new Country( name: "Uganda" , alpha2: "UG", alpha3: "UGA" ),
new Country( name: "Ukraine" , alpha2: "UA", alpha3: "UKR" ),
new Country( name: "United Arab Emirates, The" , alpha2: "AE", alpha3: "ARE" ),
new Country( name: "United Kingdom" , alpha2: "GB", alpha3: "GBR", e164Code: "+44", synonyms: new String[] { "United Kingdom of Great Britain and Northern Ireland, The", "Great Britain", "Britain", "UK" } ),
new Country( name: "United States Minor Outlying Islands" , alpha2: "UM", alpha3: "UMI", synonyms: new String[] { "United States Minor Outlying Islands, The" } ),
new Country( name: "United States" , alpha2: "US", alpha3: "USA", e164Code: "+1", synonyms: new String[] { "United States of America, The", "United States of America" } ),
new Country( name: "Uruguay" , alpha2: "UY", alpha3: "URY" ),
new Country( name: "Uzbekistan" , alpha2: "UZ", alpha3: "UZB" ),
new Country( name: "Vanuatu" , alpha2: "VU", alpha3: "VUT" ),
new Country( name: "Venezuela (Bolivarian Republic of)" , alpha2: "VE", alpha3: "VEN" ),
new Country( name: "Vietnam" , alpha2: "VN", alpha3: "VNM", synonyms: new String[] { "Viet Nam" } ),
new Country( name: "Virgin Islands (British)" , alpha2: "VG", alpha3: "VGB" ),
new Country( name: "Virgin Islands (U.S.)" , alpha2: "VI", alpha3: "VIR" ),
new Country( name: "Wallis and Futuna" , alpha2: "WF", alpha3: "WLF" ),
new Country( name: "Western Sahara" , alpha2: "EH", alpha3: "ESH" ),
new Country( name: "Yemen" , alpha2: "YE", alpha3: "YEM" ),
new Country( name: "Zambia" , alpha2: "ZM", alpha3: "ZMB" ),
new Country( name: "Zimbabwe" , alpha2: "ZW", alpha3: "ZWE" )
};
//
public Country( String name, String alpha2, String alpha3, IEnumerable<String>? synonyms = null, String? e164Code = null )
{
if( name is null ) throw new ArgumentNullException( nameof( name ) );
{
const String THE_SUFFIX = @", The";
synonyms = synonyms ?? Array.Empty<String>();
if( name.EndsWith( THE_SUFFIX, StringComparison.OrdinalIgnoreCase ) )
{
String nameWithThe = name;
synonyms = synonyms.Append( nameWithThe );
name = name.Substring( startIndex: 0, length: name.Length - THE_SUFFIX.Length );
}
}
//
this.Name = name ?? throw new ArgumentNullException( nameof( name ) );
this.Iso31661Alpha2 = alpha2 ?? throw new ArgumentNullException( nameof( alpha2 ) );
this.Iso31661Alpha3 = alpha3 ?? throw new ArgumentNullException( nameof( alpha3 ) );
if( alpha2.Length != 2 ) throw new ArgumentOutOfRangeException( paramName: nameof(alpha2), actualValue: alpha2, message: "Value must be 2 characters in length." );
if( alpha3.Length != 3 ) throw new ArgumentOutOfRangeException( paramName: nameof(alpha3), actualValue: alpha3, message: "Value must be 3 characters in length." );
this.E164Code = e164Code;
if( e164Code != null && !e164Code.StartsWith('+') ) throw new ArgumentException( message: "Value must start with \"+\".", paramName: nameof(e164Code) );
if( synonyms is IReadOnlyList<String> roList )
{
this.Synonyms = roList;
}
else
{
this.Synonyms = synonyms.ToList();
}
}
public String Name { get; }
public String Iso31661Alpha2 { get; }
public String Iso31661Alpha3 { get; }
public String? E164Code { get; }
public IReadOnlyList<String> Synonyms { get; }
public IEnumerable<String> AllNames => Array
.Empty<String>()
.Append( this.Name )
.Append( this.Iso31661Alpha2 )
.Append( this.Iso31661Alpha3 )
.Concat( this.Synonyms )
.Distinct();
//
/// <summary>Returns the case-insensitive hashcode of only <see cref="Iso31661Alpha3"/>.</summary>
public override Int32 GetHashCode()
{
return StringComparer.OrdinalIgnoreCase.GetHashCode( this.Iso31661Alpha3 );
}
public override Boolean Equals( Object? obj )
{
if( Object.ReferenceEquals( this, obj ) ) return true;
return obj is Country other && this.Equals( other: other );
}
public Boolean Equals( Country? other )
{
if( Object.ReferenceEquals( this, other ) ) return true;
return
other != null
&&
StringComparer.OrdinalIgnoreCase.Equals( other.Name, this.Name )
&&
StringComparer.OrdinalIgnoreCase.Equals( other.Iso31661Alpha2, this.Iso31661Alpha2 )
&&
StringComparer.OrdinalIgnoreCase.Equals( other.Iso31661Alpha3, this.Iso31661Alpha3 )
;
// No point comparing `Synonyms` though.
}
}
public sealed class Subdivision
{
public static IReadOnlyList<Subdivision> USStates { get; } = new Subdivision[]
{
new Subdivision( name: "Alabama" , abbreviation: "AL" ),
new Subdivision( name: "Alaska" , abbreviation: "AK" ),
new Subdivision( name: "American Samoa" , abbreviation: "AS" ),
new Subdivision( name: "Arizona" , abbreviation: "AZ" ),
new Subdivision( name: "Arkansas" , abbreviation: "AR" ),
new Subdivision( name: "California" , abbreviation: "CA" ),
new Subdivision( name: "Colorado" , abbreviation: "CO" ),
new Subdivision( name: "Connecticut" , abbreviation: "CT" ),
new Subdivision( name: "Delaware" , abbreviation: "DE" ),
new Subdivision( name: "District of Columbia" , abbreviation: "DC" ),
new Subdivision( name: "Florida" , abbreviation: "FL" ),
new Subdivision( name: "Georgia" , abbreviation: "GA" ),
new Subdivision( name: "Guam" , abbreviation: "GU" ),
new Subdivision( name: "Hawaii" , abbreviation: "HI" ),
new Subdivision( name: "Idaho" , abbreviation: "ID" ),
new Subdivision( name: "Illinois" , abbreviation: "IL" ),
new Subdivision( name: "Indiana" , abbreviation: "IN" ),
new Subdivision( name: "Iowa" , abbreviation: "IA" ),
new Subdivision( name: "Kansas" , abbreviation: "KS" ),
new Subdivision( name: "Kentucky" , abbreviation: "KY" ),
new Subdivision( name: "Louisiana" , abbreviation: "LA" ),
new Subdivision( name: "Maine" , abbreviation: "ME" ),
new Subdivision( name: "Maryland" , abbreviation: "MD" ),
new Subdivision( name: "Massachusetts" , abbreviation: "MA" ),
new Subdivision( name: "Michigan" , abbreviation: "MI" ),
new Subdivision( name: "Minnesota" , abbreviation: "MN" ),
new Subdivision( name: "Mississippi" , abbreviation: "MS" ),
new Subdivision( name: "Missouri" , abbreviation: "MO" ),
new Subdivision( name: "Montana" , abbreviation: "MT" ),
new Subdivision( name: "Nebraska" , abbreviation: "NE" ),
new Subdivision( name: "Nevada" , abbreviation: "NV" ),
new Subdivision( name: "New Hampshire" , abbreviation: "NH" ),
new Subdivision( name: "New Jersey" , abbreviation: "NJ" ),
new Subdivision( name: "New Mexico" , abbreviation: "NM" ),
new Subdivision( name: "New York" , abbreviation: "NY" ),
new Subdivision( name: "North Carolina" , abbreviation: "NC" ),
new Subdivision( name: "North Dakota" , abbreviation: "ND" ),
new Subdivision( name: "Northern Mariana Islands", abbreviation: "MP" ),
new Subdivision( name: "Ohio" , abbreviation: "OH" ),
new Subdivision( name: "Oklahoma" , abbreviation: "OK" ),
new Subdivision( name: "Oregon" , abbreviation: "OR" ),
new Subdivision( name: "Pennsylvania" , abbreviation: "PA" ),
new Subdivision( name: "Puerto Rico" , abbreviation: "PR" ),
new Subdivision( name: "Rhode Island" , abbreviation: "RI" ),
new Subdivision( name: "South Carolina" , abbreviation: "SC" ),
new Subdivision( name: "South Dakota" , abbreviation: "SD" ),
new Subdivision( name: "Tennessee" , abbreviation: "TN" ),
new Subdivision( name: "Texas" , abbreviation: "TX" ),
new Subdivision( name: "U.S. Virgin Islands" , abbreviation: "VI" ),
new Subdivision( name: "Utah" , abbreviation: "UT" ),
new Subdivision( name: "Vermont" , abbreviation: "VT" ),
new Subdivision( name: "Virginia" , abbreviation: "VA" ),
new Subdivision( name: "Washington" , abbreviation: "WA" ),
new Subdivision( name: "West Virginia" , abbreviation: "WV" ),
new Subdivision( name: "Wisconsin" , abbreviation: "WI" ),
new Subdivision( name: "Wyoming" , abbreviation: "WY" )
};
// https://en.wikipedia.org/wiki/Provinces_and_territories_of_Canada
public static IReadOnlyList<Subdivision> CanadianSubdivisions { get; } = new Subdivision[]
{
new Subdivision( name: "Alberta" , abbreviation: "AB" ),
new Subdivision( name: "British Columbia" , abbreviation: "BC" ),
new Subdivision( name: "Manitoba" , abbreviation: "MB" ),
new Subdivision( name: "New Brunswick" , abbreviation: "NB" ),
new Subdivision( name: "Newfoundland and Labrador", abbreviation: "NL" ),
new Subdivision( name: "Northwest Territories" , abbreviation: "NT" ),
new Subdivision( name: "Nova Scotia" , abbreviation: "NS" ),
new Subdivision( name: "Nunavut" , abbreviation: "NU" ),
new Subdivision( name: "Ontario" , abbreviation: "ON" ),
new Subdivision( name: "Prince Edward Island" , abbreviation: "PE" ),
new Subdivision( name: "Quebec" , abbreviation: "QC" ),
new Subdivision( name: "Saskatchewan" , abbreviation: "SK" ),
new Subdivision( name: "Yukon" , abbreviation: "YT" )
};
public Subdivision( String name, String abbreviation )
{
this.Name = name ?? throw new ArgumentNullException( nameof( name ) );
this.Abbreviation = abbreviation ?? throw new ArgumentNullException( nameof( abbreviation ) );
}
public String Name { get; }
public String Abbreviation { get; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment