I created this simple java configuration enum and utility class to easily retrieve and set config values.
Config.java
public enum Config {
AGE(10),
HELLO_WORLD("Hello World");
Object value;
Config(Object value) {
this.value = value;
}
Object getValue() {
return value;
}
public static Object get(Config key) {
if (ConfigUtility.get(key) != null) return ConfigUtility.get(key);
else return key.getValue();
}
public static int getInt(Config key) {
return (int) get(key);
}
public static String getString(Config key) {
return (String) get(key);
}
void set(Object value) {
ConfigUtility.set(this, value);
}
}
class ConfigUtility {
private static final HashMap<Config, Object> overrides = new HashMap<>();
public static Object get(Config key) {
return overrides.getOrDefault(key, null);
}
public static void set(Config key, Object value) {
overrides.put(key, value);
}
}
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(Config.getString(Config.HELLO_WORLD));
System.out.println("I am " + Config.getInt(Config.AGE) + " years old!");
System.out.println();
Config.AGE.set(30);
System.out.println("You are " + Config.getInt(Config.AGE) + " years old.");
}
}