Created
September 2, 2014 10:14
-
-
Save dominicthomas/2d9576e70e386c5ffa09 to your computer and use it in GitHub Desktop.
Use an enum to get a value from a string
This file contains hidden or 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
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