Last active
June 1, 2019 14:07
-
-
Save codethereforam/9b8a93ff46fabe278ce4af833c87976c to your computer and use it in GitHub Desktop.
Enum getByValue example
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
public enum FooEnum { | |
/** | |
* foo | |
*/ | |
FOO(0, "foo"), | |
/** | |
* bar | |
*/ | |
BAR(0, "bar"); | |
/** | |
* enum value | |
*/ | |
private final int value; | |
/** | |
* enum desc | |
*/ | |
private final String desc; | |
FooEnum(int value, String desc) { | |
this.value = value; | |
this.desc = desc; | |
} | |
/** | |
* get value | |
* | |
* @return value | |
*/ | |
public int getValue() { | |
return this.value; | |
} | |
/** | |
* get desc | |
* | |
* @return desc | |
*/ | |
public String getDesc() { | |
return desc; | |
} | |
/** | |
* static map {'key': 'value', 'value': 'FooEnum'} | |
*/ | |
private static final Map<Integer, FooEnum> MAP = Arrays.stream(FooEnum.values()) | |
.collect(Collectors.toMap(FooEnum::getValue, e -> e)); | |
/** | |
* get FooEnum by value | |
* | |
* @param value value | |
* @return FooEnum | |
*/ | |
public static FooEnum getByValue(int value) { | |
return MAP.get(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment