I'm working with a (legacy, not changeable) enum that looks like this:
enum MsgEnum {
private int number;
private int msg;
MSG_OK("OK", 0);
MSG_NOK("NOK, 1);
//lots of other messages
private MsgEnum(String msg, int number) {
this.msg = msg;
this.number = number;
}
}
If I have the number
, how could I best find out which MsgEnum
matches the number (assuming they are unique)?
Is there any better way apart from the following?
class MyEnumHelper() {
public static MsgEnum MsgEnumfromNumber(int number) {
for (MsgEnum m : MsgEnum.values()) {
if (m.getNumber() == number) {
return m;
}
}
return null;
}
}