Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a value from properties file of an enum type?

I have an enum as below:

public enum EnvironmentType {PRODUCTION, TEST, DEVELOPMENT}

in properties file the key value is like :

app.environmentType = TEST

we know that when i read the value from properties file using key, it returns as String like

String envType = properties.getProperty("app.environmentType");

My requirement is,

EnvironmentType envType = EnvironmentType.TEST;

Now i want to know that is there a way to get the value as enum type? how can i parse or cast it ?

like image 836
dku.rajkumar Avatar asked Dec 19 '11 09:12

dku.rajkumar


2 Answers

EnvironmentType envType =   EnvironmentType.valueOf(envTypeString);
like image 54
jmj Avatar answered Oct 23 '22 06:10

jmj


You may use valueOf method as follows:

String envTypeStr = properties.getProperty("app.environmentType");
EnvironmentType envType = EnvironmentType.valueOf(envTypeStr);

The static methods valueOf() and values() are created at compile time and do not appear in source code. But they appear in documentation of some enumaration in java library. For example, see SortOrder and Normalizer.Form

like image 38
Jomoos Avatar answered Oct 23 '22 06:10

Jomoos