patternjavaMinor
YAML Configuration file
Viewed 0 times
fileconfigurationyaml
Problem
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.
Example of usage:
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 config = new HashMap();
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);Solution
I think this is really not so good:
Example of usage:
That's too much implementation to expose as an API. This would be a lot more natural:
In your current code, I really don't see the point of the
Even if there is really a good reason for this approach, you should expose user-friendly API methods like:
and make
Finally, as @Simon Andre Forsberg said,
Example of usage:
Config.getInt(Config.SERVER_PORT);That's too much implementation to expose as an API. This would be a lot more natural:
serverConfig.getServerPort()In your current code, I really don't see the point of the
Map config field. Why not use native fields for the configuration? If there is other code that needs it this way, that should be part of the review.Even if there is really a good reason for this approach, you should expose user-friendly API methods like:
public int getServerPort() {
return getInt(SERVER_PORT);
}and make
getInt and the others private. No need to expose such implementation details.Finally, as @Simon Andre Forsberg said,
config.yaml should not be hardcoded. Actually I would make it a constructor parameter.private final File configFile;
public ServerConfig(String configFilePath) {
configFile = new File(configFilePath);
}Code Snippets
Config.getInt(Config.SERVER_PORT);serverConfig.getServerPort()public int getServerPort() {
return getInt(SERVER_PORT);
}private final File configFile;
public ServerConfig(String configFilePath) {
configFile = new File(configFilePath);
}Context
StackExchange Code Review Q#49382, answer score: 6
Revisions (0)
No revisions yet.