Created
March 29, 2013 16:38
-
-
Save webdevwilson/5271984 to your computer and use it in GitHub Desktop.
Enum containing states with abbreviations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
public enum State { | |
ALABAMA("Alabama", "AL"), ALASKA("Alaska", "AK"), AMERICAN_SAMOA("American Samoa", "AS"), ARIZONA("Arizona", "AZ"), ARKANSAS( | |
"Arkansas", "AR"), CALIFORNIA("California", "CA"), COLORADO("Colorado", "CO"), CONNECTICUT("Connecticut", "CT"), DELAWARE( | |
"Delaware", "DE"), DISTRICT_OF_COLUMBIA("District of Columbia", "DC"), FEDERATED_STATES_OF_MICRONESIA( | |
"Federated States of Micronesia", "FM"), FLORIDA("Florida", "FL"), GEORGIA("Georgia", "GA"), GUAM("Guam", "GU"), HAWAII( | |
"Hawaii", "HI"), IDAHO("Idaho", "ID"), ILLINOIS("Illinois", "IL"), INDIANA("Indiana", "IN"), IOWA("Iowa", "IA"), KANSAS( | |
"Kansas", "KS"), KENTUCKY("Kentucky", "KY"), LOUISIANA("Louisiana", "LA"), MAINE("Maine", "ME"), MARYLAND("Maryland", "MD"), MARSHALL_ISLANDS( | |
"Marshall Islands", "MH"), MASSACHUSETTS("Massachusetts", "MA"), MICHIGAN("Michigan", "MI"), MINNESOTA("Minnesota", "MN"), MISSISSIPPI( | |
"Mississippi", "MS"), MISSOURI("Missouri", "MO"), MONTANA("Montana", "MT"), NEBRASKA("Nebraska", "NE"), NEVADA("Nevada", | |
"NV"), NEW_HAMPSHIRE("New Hampshire", "NH"), NEW_JERSEY("New Jersey", "NJ"), NEW_MEXICO("New Mexico", "NM"), NEW_YORK( | |
"New York", "NY"), NORTH_CAROLINA("North Carolina", "NC"), NORTH_DAKOTA("North Dakota", "ND"), NORTHERN_MARIANA_ISLANDS( | |
"Northern Mariana Islands", "MP"), OHIO("Ohio", "OH"), OKLAHOMA("Oklahoma", "OK"), OREGON("Oregon", "OR"), PALAU("Palau", | |
"PW"), PENNSYLVANIA("Pennsylvania", "PA"), PUERTO_RICO("Puerto Rico", "PR"), RHODE_ISLAND("Rhode Island", "RI"), SOUTH_CAROLINA( | |
"South Carolina", "SC"), SOUTH_DAKOTA("South Dakota", "SD"), TENNESSEE("Tennessee", "TN"), TEXAS("Texas", "TX"), UTAH( | |
"Utah", "UT"), VERMONT("Vermont", "VT"), VIRGIN_ISLANDS("Virgin Islands", "VI"), VIRGINIA("Virginia", "VA"), WASHINGTON( | |
"Washington", "WA"), WEST_VIRGINIA("West Virginia", "WV"), WISCONSIN("Wisconsin", "WI"), WYOMING("Wyoming", "WY"), UNKNOWN( | |
"Unknown", ""); | |
/** | |
* The state's name. | |
*/ | |
private String name; | |
/** | |
* The state's abbreviation. | |
*/ | |
private String abbreviation; | |
/** | |
* The set of states addressed by abbreviations. | |
*/ | |
private static final Map<String, State> STATES_BY_ABBR = new HashMap<String, State>(); | |
/* static initializer */ | |
static { | |
for (State state : values()) { | |
STATES_BY_ABBR.put(state.getAbbreviation(), state); | |
} | |
} | |
/** | |
* Constructs a new state. | |
* | |
* @param name the state's name. | |
* @param abbreviation the state's abbreviation. | |
*/ | |
State(String name, String abbreviation) { | |
this.name = name; | |
this.abbreviation = abbreviation; | |
} | |
/** | |
* Returns the state's abbreviation. | |
* | |
* @return the state's abbreviation. | |
*/ | |
public String getAbbreviation() { | |
return abbreviation; | |
} | |
/** | |
* Gets the enum constant with the specified abbreviation. | |
* | |
* @param abbr the state's abbreviation. | |
* @return the enum constant with the specified abbreviation. | |
* @throws SunlightException if the abbreviation is invalid. | |
*/ | |
public static State valueOfAbbreviation(final String abbr) { | |
final State state = STATES_BY_ABBR.get(abbr); | |
if (state != null) { | |
return state; | |
} else { | |
return UNKNOWN; | |
} | |
} | |
public static State valueOfName(final String name) { | |
final String enumName = name.toUpperCase().replaceAll(" ", "_"); | |
try { | |
return valueOf(enumName); | |
} catch (final IllegalArgumentException e) { | |
return State.UNKNOWN; | |
} | |
} | |
@Override | |
public String toString() { | |
return name; | |
} | |
} |
Here is a public domain licensed version which includes ISO and ANSI abbreviations and parsing.
https://github.com/AustinC/UnitedStates/blob/master/src/main/java/unitedstates/US.java
name
and abbreviation
should also be final
Thanks a ton for this. :) made my day
name
andabbreviation
should also befinal
Not required. Enum name
and ordinal
are already final
(ref. java.lang.Enum
). Initialization is only happening within the Enum, not from outside.
TIL about Federated States of Micronesia.
TIL about Federated States of Micronesia.
haha, and now I get nothing done today as I wiki and become an expert on micronesian stone money
They use US currency.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Kerry. It would encourage reuse if you listed an explicit license.