Last active
May 24, 2018 12:31
-
-
Save mcgivrer/5a15355c64e600428f10e3ea7416af1e to your computer and use it in GitHub Desktop.
A simple Java Configuration class to provide some easy configuration capabilities to any Java project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This `Configuration` class load properties from `res/configuration.properties` file. | |
* some provided helpers will retrieve: | |
* - String value `Configuration.get(String key, String default)` | |
* - int value `Configuration.getInteger(String key, inf default)` | |
* - float value `Configuration.getFloat(String key, float default)` | |
* | |
* @author McGivrer | |
* @see https://gist.github.com/McGivrer | |
* @see https://twitter.com/McGivrer | |
*/ | |
static class Configuration { | |
private static final String UNKNOWN_CONFIG_KEY = "UNKNOWN_CONFIG_KEY"; | |
public static Configuration instance = new Configuration(); | |
Properties props; | |
private Configuration() { | |
props = new Properties(); | |
reload(); | |
} | |
/** | |
* Load configuration from `/res/configuration.properties` file. | |
*/ | |
private void load() { | |
try { | |
props.load(Game.class.getResourceAsStream("/res/configuration.properties")); | |
} catch (IOException e) { | |
System.err.println("Unable to read configuration file"); | |
System.exit(-1); | |
} | |
} | |
/** | |
* retrieve a value from configuraiton.properties file. | |
* | |
* @param key | |
* key configuration to be retrieved. | |
* | |
* @return String value | |
*/ | |
private String getConfig(String key) { | |
if (props.containsKey(key)) { | |
return props.getProperty(key); | |
} else { | |
return UNKNOWN_CONFIG_KEY; | |
} | |
} | |
/** | |
* retrieve a <code>int</code> value for configuration <code>key</code>. | |
* | |
* @param key | |
* @return | |
*/ | |
public static int getInteger(String key, int defaultValue) { | |
String value = Configuration.instance.getConfig(key); | |
if (value.equals(UNKNOWN_CONFIG_KEY)) { | |
return defaultValue; | |
} | |
return Integer.parseInt(value); | |
} | |
/** | |
* retrieve a <code>float</code> value for configuration <code>key</code>. | |
* | |
* @param key | |
* @return | |
*/ | |
public static float getFloat(String key, float defaultValue) { | |
String value = Configuration.instance.getConfig(key); | |
if (value.equals(UNKNOWN_CONFIG_KEY)) { | |
return defaultValue; | |
} | |
return Float.parseFloat(value); | |
} | |
/** | |
* retrieve a <code>String</code> value for configuration <code>key</code>. | |
* | |
* @param key | |
* @return | |
*/ | |
public static String get(String key, String defaultValue) { | |
String value = Configuration.instance.getConfig(key); | |
if (value.equals(UNKNOWN_CONFIG_KEY)) { | |
return defaultValue; | |
} | |
return value; | |
} | |
/** | |
* request a configuration reload from file. | |
*/ | |
public static void reload() { | |
Configuration.instance.load(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment