There are two easy approaches you could use depends on logic you want to use
I would suggest to use second one
public enum SubSectorEnum {
Crops(11), Plantation(12);
public int id;
SubSectorEnum(int id) {
this.id = id;
}
public int getId() {
return id;
}
// 1 approach using switch
public SubSectorEnum convertIdToEnum(int id) {
SubSectorEnum subSectorEnum = Crops; // Default
switch (id) {
case 11:
subSectorEnum = Crops;
break;
case 12:
subSectorEnum = Plantation;
break;
default:
break;
}
return subSectorEnum;
}
// 2 approach using a map
private static final Map<Integer, SubSectorEnum> map = new HashMap<Integer, SubSectorEnum>();
static {
for (SubSectorEnum subSectorEnum : SubSectorEnum.values())
map.put(subSectorEnum.getId(), subSectorEnum);
}
public static SubSectorEnum getEnumFromMap(int id) {
return map.get(id);
}
}
if you have only these two values in your DB and you are always getting either 11 or 12 and you don't plan to add anything in future you could use following method.
public SubSectorEnum getEnumById(int id) {
return id==Crops.getId()?Crops:Plantation;
}