java property file as enum

前端 未结 8 1108
梦如初夏
梦如初夏 2020-12-24 09:47

Is it possible to convert a property file into an enum.

I have a propoerty file with lot of settings. for example

equipment.height
equipment.widht
e         


        
相关标签:
8条回答
  • 2020-12-24 10:08

    No. Well, I suppose you could if you could compile a properties file into a Java class (or enum). I can't find anything like that (but would be extremely cool)

    0 讨论(0)
  • 2020-12-24 10:09

    One way, I could imagine, to get all the properties in your IDE can be to define an Enum with all of them, like this :

    public enum Settings
    {
       EQUIPMENT_HEIGHT("equipment.height", "0"),
    
       EQUIPMENT_WIDTH("equipment.width", "0"),
    
       EQUIPMENT_DEPTH("equipment.depth", "0");
    
       private String property;
    
       private String value;
    
       Settings(final String aProperty, final String aValue)
       {
          property = aProperty;
          value = aValue;
       }
    
       public String getProperty()
       {
          return property;
       }
    
       public String getValue()
       {
          return value;
       }
    
       private void setValue(final String aValue)
       {
          value = aValue;
       }
    
       public static void initialize(final Properties aPropertyTable)
       {
          for(final Settings setting : values())
          {
             final String key = setting.getProperty();
             final String defaultValue = setting.getValue();
             setting.setValue(aPropertyTable.getProperty(key, defaultValue));
          }
       }
    }
    

    The initialization of the enum is self explained (method initialize()).

    After that, you can use it like this :

    Settings.EQUIPMENT_HEIGHT.getValue();
    

    Adding new property is just adding new enum-Constant.

    0 讨论(0)
  • 2020-12-24 10:13
    public enum ErrorCode{
    
    
        DB_ERROR( PropertiesUtil.getProperty("DB_ERRROR_CODE"), PropertiesUtil.getProperty("DB_ERROR")),
        APP_ERROR(PropertiesUtil.getProperty("APPLICATION_ERROR_CODE"), PropertiesUtil.getProperty("APPLICATION_ERROR")),
        ERROR_FOUND(PropertiesUtil.getProperty("ERROR_FOUND_CODE"), PropertiesUtil.getProperty("ERROR_FOUND"));
    
    
        private final String errorCode;
        private final String errorDesc;
    
    
    
        private ErrorCode(String errorCode, String errorDesc) {
            this.errorCode = errorCode;
            this.errorDesc = errorDesc;
        }
    
        public String getErrorDesc() {
            return errorDesc;
        }
    
        public String getErrorCode() {
            return errorCode;
        }
    
        public static String getError(String errorCode)
        { 
            System.out.println("errorCode in Enum"+errorCode);
            System.out.println(java.util.Arrays.asList(ErrorCode.values()));
            for (ErrorCode errorEnum : ErrorCode.values()) {
                System.out.println(errorEnum.errorCode);
                System.out.println(errorEnum.errorDesc);
            if ((errorEnum.errorCode).equals(errorCode)) {
                return errorEnum.getErrorDesc();
            }
            }
            return ERROR_FOUND.getErrorDesc();
    
        }
    
    
    public class PropertiesUtil {
    static  Properties prop = new Properties();
    
        static{
    
            try {
    
                  InputStream inputStream =
                          PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
    
                 prop.load(inputStream);
        }catch(Exception e)
            {
            e.printStackTrace();
            }
        }
    
    
    
            public static PropertiesUtil getInstance()
            {
    
                return new PropertiesUtil();
            }
    
            public Properties getProperties()
            {
                return prop;
            }
    
            public static String getProperty(String key)
            {
                return prop.getProperty(key);
            }
    
    
    }
    
    0 讨论(0)
  • 2020-12-24 10:15

    You can turn a properties file into an enum with generated code. You can do this staticly before the program is compiled and optionally at runtime by using the Compiler API. Doing this adds a a lot of complexity and usually its simpler to use a Map as has been suggested.

    0 讨论(0)
  • 2020-12-24 10:19

    The question was asked about enum, I wanted to create an enum based on property. I gave up the idea, because this enum needed to be dynamic, if we get a new error code, it should be added to property file. Sadly I see no way to do that with enum so I chose different path as I read everybody suggests Map based solution. Instead of enum I created a singleton which just reads a property file, and responds on the keywords to give back the value.

    the property file:

    C102 = Blablabla1
    C103 = Blablabla2
    C104 = Blablabla3
    

    the singleton code:

    package mypackage;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map.Entry;
    import java.util.Properties;
    
    public class ResponseValidationTypeCodes {
    
    private final HashMap<String, String> codes;
    
    private static ResponseValidationTypeCodes instance;
    
    public static ResponseValidationTypeCodes getInstance() {
        if (instance == null) {
            instance = new ResponseValidationTypeCodes();
        }
        return instance;
    }
    
    private ResponseValidationTypeCodes() {
        super();
        codes = new HashMap<String, String>();
        initEntry();
    }
    
    private void initEntry() {
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(
                    "src/main/resources/validationcodes.properties"));
            for (Entry<Object, Object> element : prop.entrySet()) {
                codes.put(element.getKey().toString(), element.getValue()
                        .toString());
            }
    
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public String getValueByCode(String code) {
        return codes.get(code);
    }
    }
    

    to get the values, simply call:

    ResponseValidationTypeCodes.getInstance()
                .getValueByCode("C102");
    

    the initial property reading runs only once. So you just expand the property when some change comes in, and redeploy your stuffs that time. I hope it helps for somebody who is open to use some alternative to enum.

    0 讨论(0)
  • 2020-12-24 10:24

    Java has static typing. That means, you can't create types dynamically. So, the answer is no. You can't convert a property file into a enum.

    What you could do is generate an enum from that properties file. Or, use a Dictionary (Map) to access your properties with something like:

    equipment.get("height");
    
    0 讨论(0)
提交回复
热议问题