Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Last active June 1, 2019 14:07
Show Gist options
  • Save codethereforam/9b8a93ff46fabe278ce4af833c87976c to your computer and use it in GitHub Desktop.
Save codethereforam/9b8a93ff46fabe278ce4af833c87976c to your computer and use it in GitHub Desktop.
Enum getByValue example
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