Given this enum:
public enum FileTypes {
FILEA, FILEB, FILEC;
}
Say you want to see if a String value matches one of the 3 enum types, I have this method:
public static boolean validFileType(String value) {
for (FileTypes fileTypes : FileTypes.values()) {
if (fileTypes.name().equals(value)) {
return true;
}
}
return false;
}
That works fine, now I’d like to change things a bit. Can I add multiple Strings to match for a single enum? Let’s say the String value is “FILEA” or “example-file”, could I have both of those specified in the enum? I’d like to keep those in the enum rather than have if statements outside the enum performing the mapping.
Hope that makes sense, thanks for any ideas
You should add field(s) to your enum and then implement kind of your own
valueOf()method. Please take a look on article that gives more details: http://java.dzone.com/articles/enum-tricks-customized-valueof