I want to serialized/deserialize a java enum as an int. I'm using the Android Wear Wearable Data Layer API to transport an enum setting between Wear and Phone devices. This means I need to convert my enum to an int and back to an enum programatically. Note that I do NOT need to create new enums, only match an existing one. My concern is that although I can easily set a value using an enum method, it seems that I could easily set a non-existing value.
public enum Color {
Undefined (0), Red(1), Yellow(2), Blue(3), Green(4), Black(5);
private int mValue;
private Color(int value) { this.mValue = value;} // Constructor
public int id(){return mValue;} // Return enum index
public void setValue(int i){mValue = i;}; // Set index
}
Thus, I can do the following:
Color color = Color.Red;
int index = color.id();
putDataMapReq.getDataMap().putInt("color",index);
This value can be transported as an int, and I would like to reconstitute it as follows but I can't get the setValue() method to be accessible:
int newindex = dataMap.getInt ("color");
Color newColor = Color.Undefined;
newColor.setValue(newIndex);
What is this the right way to do this? Is there a way to validate that the new index value is legitimate?