I saw it suggested on a blog that the following was a reasonable way to do a \"reverse-lookup\" using the getCode(int)
in a Java enum:
public en
Maps.uniqueIndex from Google's Guava is quite handy for building lookup maps.
Update: Here is an example using Maps.uniqueIndex
with Java 8:
public enum MyEnum {
A(0), B(1), C(2);
private static final Map LOOKUP = Maps.uniqueIndex(
Arrays.asList(MyEnum.values()),
MyEnum::getStatus
);
private final int status;
MyEnum(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
@Nullable
public static MyEnum fromStatus(int status) {
return LOOKUP.get(status);
}
}