Last active
December 15, 2015 10:59
-
-
Save friek/5249745 to your computer and use it in GitHub Desktop.
A java enumerate with a static fromString method to get an enum by String value.
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
| public enum Enumerate | |
| { | |
| VALUE1("VALUE1"), | |
| VALUE2("VALUE2"), | |
| UNKNOWN("UNKNOWN"); | |
| private String value; | |
| private Enumerate(String s) | |
| { | |
| value = s; | |
| } | |
| @Override | |
| public String toString() | |
| { | |
| return value; | |
| } | |
| /** | |
| * Get the enumerate by enum value | |
| * @param s | |
| * @return | |
| */ | |
| public static Enumerate fromString(String s) | |
| { | |
| if (s != null) | |
| for (Enumerate e : Enumerate.values()) | |
| if (e.toString().equals(s)) | |
| return e; | |
| return Enumerate.UNKNOWN; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment