Skip to content

Instantly share code, notes, and snippets.

@webdevwilson
Created March 29, 2013 16:38
Show Gist options
  • Save webdevwilson/5271984 to your computer and use it in GitHub Desktop.
Save webdevwilson/5271984 to your computer and use it in GitHub Desktop.
Enum containing states with abbreviations
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;
}
}
@AustinC
Copy link

AustinC commented Oct 27, 2014

Thanks Kerry. It would encourage reuse if you listed an explicit license.

@AustinC
Copy link

AustinC commented Mar 18, 2015

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

@johncarl81
Copy link

name and abbreviation should also be final

@xinjteem
Copy link

xinjteem commented Apr 7, 2017

Thanks a ton for this. :) made my day

@mohnish82
Copy link

mohnish82 commented Jun 23, 2020

name and abbreviation should also be final

Not required. Enum name and ordinal are already final (ref. java.lang.Enum). Initialization is only happening within the Enum, not from outside.

@BrentonPoke
Copy link

TIL about Federated States of Micronesia.

@webdevwilson
Copy link
Author

webdevwilson commented Aug 24, 2020

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

@BrentonPoke
Copy link

They use US currency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment