简体   繁体   中英

Saving values of different types, with methods

I was to save some configuration values. :) These values are might be defined in a xml file and might be of the types String, boolean, String[], ... or others. These values might be defined in a xml file. Is there a best practise way to save these values, together with their tagname in the xml file and a default value. I want to clean up some ultra bad legacy code, where things look like this:

    public static final String VIEWCOOKIE_MAXLENGTH_VALUENAME = "view_cookie_max_length";
    public static final int VIEWCOOKIE_MAXLENGTH_DEFAULT = 512;
    public static int viewCookie_maxLength = VIEWCOOKIE_MAXLENGTH_DEFAULT;

    Integer temInt = basic.getTimedBaseIntegerValue(basic.c_adclear_section, null,
    CookieValues.VIEWCOOKIE_MAXLENGTH_VALUENAME,
    CookieValues.VIEWCOOKIE_MAXLENGTH_DEFAULT, currentTime);
if (firstLoad || basic.checkParamChanged(0,CookieValues.VIEWCOOKIE_MAXLENGTH_VALUENAME,
CookieValues.viewCookie_maxLength, temInt))
    CookieValues.viewCookie_maxLength = temInt;

those lines for all values, and methods for all the different type.

Is there a better way like this:

public class Value<T> {

   String valueName;
   final T defaultValue;
   T value;
   Method method;
    ...
}

but I don't know if this is the best way and also I'm a little bit worried, because I don't want to slow down the server, due to all the Wrapppers. This problem is hard to search so I'm sorry if this has already been asked somewhere.

This might answer your question: What is the best way to convert a java object to xml with open source apis

Basically you have to use a Marshaller (something that converts an object to text and back), and there are tons of those for Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM