I have a java component that was using a lot of string literals that I need to make comparisons on and return booleans based on these comparisons.
In order to make the code more robust I externalized these strings first as class constants, then after other classes started to use these constants I had to separate them to decrease the dependency between the classes.
Knowing that the best practice is not to use variable-classes dedicated for string for many reasons, and since I am using Java 6, I decided to go for enums. below is the implementation that I had in mind
public enum SecurityClassification {
SENSITIVE("Sensitive"), HIGHLY_SENSITIVE("Highly Sensitive"), PUBLIC("Public"), INTERNAL("Internal");
private String value;
private SecurityClassification(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public boolean hasValue(String param) {
return value.equalsIgnoreCase(param);
}
public static SecurityClassification enumForValue(String param){
for (SecurityClassification securityClassification : SecurityClassification.values()) {
if(securityClassification.getValue().equals(param)){
return securityClassification;
}
}
return null;
}
}
I was wondering specifically about the enumForValue method, is this an optimal solution? is there any other better way?