I have recently started working on a server software in Java and wanted to add a configuration option to the program. I have gone through a few revisions of this class because I never liked what I came up with. I finally made something that I'm somewhat happy about and it works better than past versions. I'm just curious if anything can be done better or if this class should be rewritten again in a different?
FYI: some methods are not done yet. I'm not worried about those methods, I'm asking about what is there so far.
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
public class Config
{
public static final String SERVER_NAME = "serverName";
public static final String MOTD = "motd";
public static final String SERVER_PORT = "serverPort";
private static final File file = new File("config.yaml");
private Map<String, Object> config = new HashMap<String, Object>();
private DumperOptions yamlOptions = new DumperOptions();
private Yaml yaml;
private void setDefaults()
{
config.clear();
config.put(SERVER_NAME, "Server");
config.put(MOTD, "Welcome to the server!");
config.put(SERVER_PORT, 21020);
}
public void generate()
{
setDefaults();
save();
}
public void load()
{
//To-do
}
public void save()
{
//To-do
}
public Object getObject(String key)
{
return config.get(key);
}
public String getString(String key)
{
return (String) getObject(key);
}
public int getInt(String key)
{
return (int) getObject(key);
}
public double getDouble(String key)
{
return (double) getObject(key);
}
}
Example of usage:
Config.getInt(Config.SERVER_PORT);