I have an enum where the reverse-lookup table I created for it returns null for all values. I added an assert after the lookup table is created, and sure enough, it’s empty. I feel like this is something simple and obvious that I’m overlooking because I use this exact same structure for all my enums and they work fine.
public enum TerrainType {
ROAD(1),
PLAINS(2),
FOREST(3),
MOUNTAINS(4);
private static final Map<Byte,TerrainType> lookup = new HashMap<Byte,TerrainType>();
private final byte terrainId;
static {
for(TerrainType type : EnumSet.allOf(TerrainType.class)) {
lookup.put(type.getByte(),type);
}
assert lookup.get(1) != null; // <--- THIS ASSERT FIRES, WHY?
}
TerrainType(int id) {
assert id > 0 && id < 128: "ID must be from 1 to 127";
this.terrainId = (byte) id;
}
public static TerrainType get(int id) {
return lookup.get(id);
}
public byte getByte() {
return this.terrainId;
}
}
The literal 1 is an integer.
This works.
Alternatively, you could change your
terrainIdto be of typeint