Skip to content

Instantly share code, notes, and snippets.

@dominicthomas
Created September 2, 2014 10:14
Show Gist options
  • Save dominicthomas/2d9576e70e386c5ffa09 to your computer and use it in GitHub Desktop.
Save dominicthomas/2d9576e70e386c5ffa09 to your computer and use it in GitHub Desktop.
Use an enum to get a value from a string
private enum GravityExpandableText {
LEFT(Gravity.LEFT), RIGHT(Gravity.RIGHT), CENTER(Gravity.CENTER);
private final int mValue;
GravityExpandableText(int value) {
mValue = value;
}
public int getValue() {
return mValue;
}
public static int getGravity(String gravity) {
if (lowerCaseNameFor(LEFT).matches(gravity)) {
return LEFT.getValue();
} else if (lowerCaseNameFor(RIGHT).matches(gravity)) {
return RIGHT.getValue();
} else if (lowerCaseNameFor(CENTER).matches(gravity)) {
return CENTER.getValue();
}
return Gravity.CENTER;
}
private static String lowerCaseNameFor(GravityExpandableText item) {
return item.name().toLowerCase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment