Parsing permissions from a properties file

I have a simple key value properties file where I need to parse the value, which then needs to be assigned to an enum type. What's the best way to do this?

The only thing that comes to my mind is something like iterating through all possible values ​​of enums.toString and see if it is equal to any of them.

+2


source to share


1 answer


Enum.valueOf (or rather its wrapper, which is synthesized in and for each class enum

) does what you want.



enum Color { RED, GREEN, BLUE }

// somewhere in your code
String colorName = "GREEN";
try {
    Color color = Color.valueOf(colorName);
} catch (IllegalArgumentException e){
    // colorName was not the name of a member of the enum
}

      

+7


source







All Articles