My Enum original java code is:
public enum CarModel {
NOMODEL("NOMODEL");
X("X"),
XS("XS"),
XSI("XS-I"); //NOTE the special - character. Can't be declared XS-I
XSI2("XS-I.2"); //NOTE the special . character. Can't be declared XS-I.2
private final String carModel;
CarModel(String carModel) {
this.carModel = carModel;
}
public String getCarModel() { return carModel; }
public static CarModel fromString(String text) {
if (text != null) {
for (CarModel c : CarModel.values()) {
if (text.equals(c.carModel)) {
return c;
}
}
}
return NOMODEL; //default
}
}
Now if I use protobuf I get in the .proto file:
enum CarModel {
NOMODEL = 0;
X = 1;
XS = 2;
XSI = 3;
XSI2 = 4;
}
from my earlier question I know I can call the enum generated by protoc and remove my own class (and thus avoid the duplicate value definitions) but I still need to define somewhere (In a wrapper class? wrapper enum class?) the alternate fromString()
method that will return the right string per the enum. How do I do that?
EDIT: How do I implement the following:
String carModel = CarModel.XSI.toString();
This will return "XS-I"
and:
CarModel carModel = CarModel.fromString("XS-I.2");