I have an enumerator that takes an int and a string as fields. These are accessible via static methods or directly from the enumerator "instance" (not familiar with the proper wording for this). Here is what I mean by that:
public enum CreatureType {
POKE_PIG(1, "Piggley"), POKE_COW(2, "Cowbert");
private int number;
private String name;
CreatureType(int number, String name) {
this.number = number;
this.name = name;
}
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public static String getName(CreatureType type) {
return type.getName();
}
public static int getNumber(CreatureType type) {
return type.getNumber();
}
But now I need to get a CreatureType FROM it's int. So here's my pseudocode I've written that is my best guess at how to do this:
public static CreatureType getCreatureType(int number) {
switch (number) {
case 1:
return CreatureType.POKE_PIG;
case 2:
return CreatureType.POKE_COW;
default:
break;
}
return null;
}
This feels weird though. I don't love this technique, and even though in theory it should work, I feel like there HAS to be a better way of doing this. I don't have these enums stored in a list or set or anything so I can't iterate through them, and I'm just blanking on the proper way to grab this.