I would like to have a class that emulates an EnumMap but stores int values ββinstead of some kind of object. Now, obviously, you can make EnumMap that maps to integers, but it's a lot of autoboxing that I would like to avoid if possible.
So I need a class like this:
public class EnumIntAttributeMap { enum Attribute { Height, Weight; } private final int[] values; public EnumIntAttributeMap() { this.values = new int[Attribute.values().length]; } public int getValue(Attribute a) { return this.values[a.ordinal()]; } public void setValue(Attribute a, int value) { this.values[a.ordinal()] = value; } }
In addition, I would like to make a version that is common to all listings. Now, since the .values ββ() and .ordinal () methods are implicitly added by the compiler, it seems that the only way to access them will be with reflection, which will increase the performance that I try to get by avoiding auto-boxing, but, maybe there is something missing for me.
Any thoughts?
EDIT:
I think my initial question was unclear. I need a class that takes (as a general parameter) an enumeration, and then can use the same operations.
Therefore, I could use it with any enumeration without having to write a class for each type of enumeration every time. For example:
enum Attribute { Height, Weight } enum AbilityScore {Str, Dex, Con, Int, Wis, Cha} IdealClass<Attribute> attributeVersion; IdealClass<AbilityScore> abilityScoreVersion;
etc.
Lokathor
source share