Skip to content

Instantly share code, notes, and snippets.

@mcgivrer
Last active April 8, 2023 16:21
Show Gist options
  • Save mcgivrer/8d171a1d8d0c5ef5c3ff28b851fa0aa0 to your computer and use it in GitHub Desktop.
Save mcgivrer/8d171a1d8d0c5ef5c3ff28b851fa0aa0 to your computer and use it in GitHub Desktop.
Pure Java implementation for Configruation
/**
* {@link Configuration} loads a properties file and
* let user gather converted value to
* <ul>
* <li>Integer,</li>
* <li>Double,</li>
* <li>Boolean,</li>
* <li>String.</li>
* <li>{@link Dimension} </li>
* <li>{@link Rectangle2D}</li>
* </ul>
* from these properties.
* The user can also {@link Configuration#save()} values after changes.
*
* @author Frédéric Delorme
* @since 0.0.1
*/
public class Configuration {
private final Properties parameters = new Properties();
String filePath;
/**
* Initialize the {@link Configuration} with a file properties.
*
* @param file the path to the properties file to load into Configuration.
*/
public Configuration(String file) {
this.filePath = file;
try {
parameters.load(Game.class.getResourceAsStream(filePath));
System.out.printf("Configuration:The configuration file %s has been loaded%n", file);
} catch (IOException e) {
System.err.printf("Configuration:The configuration file %s can not been loaded: %s%n", file, e.getMessage());
}
}
/**
* Retrieve a value as Integer from {@link Configuration}.
*
* @param key name of the configuration key to be loaded
* @param defaultValue if no value exists in {@link Configuration}, instead use the defaultValue.
* @return the corresponding int value
*/
public int getInteger(String key, int defaultValue) {
if (parameters.containsKey(key)) {
return Integer.parseInt(parameters.getProperty(key));
}
return defaultValue;
}
/**
* Retrieve a value as Double from {@link Configuration}.
*
* @param key name of the configuration key to be loaded
* @param defaultValue if no value exists in {@link Configuration}, instead use the defaultValue.
* @return the corresponding double value
*/
public double getDouble(String key, double defaultValue) {
if (parameters.containsKey(key)) {
return Double.parseDouble(parameters.getProperty(key));
}
return defaultValue;
}
/**
* Retrieve a value as Boolean from {@link Configuration}.
*
* @param key name of the configuration key to be loaded
* @param defaultValue if no value exists in {@link Configuration}, instead use the defaultValue.
* @return the corresponding boolean value
*/
public boolean getBoolean(String key, boolean defaultValue) {
if (parameters.containsKey(key)) {
return Boolean.parseBoolean(parameters.getProperty(key));
}
return defaultValue;
}
/**
* Retrieve a value as String from {@link Configuration}.
*
* @param key name of the configuration key to be loaded
* @param defaultValue if no value exists in {@link Configuration}, instead use the defaultValue.
* @return the corresponding String value
*/
public String getString(String key, String defaultValue) {
if (parameters.containsKey(key)) {
return parameters.getProperty(key);
}
return defaultValue;
}
/**
* Retrieve a value as Dimension from {@link Configuration} from a string format "dim([width],[height])".
*
* @param key name of the configuration key to be loaded
* @param defaultValue if no value exists in {@link Configuration}, instead use the defaultValue.
* @return the corresponding Dimension value
*/
public Dimension getDimension(String key, Dimension defaultValue) {
String param = parameters.getProperty(key, "");
if (!param.equals("") && param.startsWith("dim(") && param.endsWith(")")) {
String[] k = param.substring("dim(".length(), param.length() - 1).split(",");
int width = Integer.valueOf(k[0]);
int height = Integer.valueOf(k[0]);
return new Dimension(width, height);
} else {
System.err.printf("Configuration: Dimension value not found for %s, use %s as default value.%n", key, defaultValue);
return defaultValue;
}
}
/**
* Retrieve a value as {@link Rectangle2D} from {@link Configuration} from a string format "r2d([width],[height])".
*
* @param key name of the configuration key to be loaded
* @param defaultValue if no value exists in {@link Configuration}, instead use the defaultValue.
* @return the corresponding {@link Rectangle2D} value
*/
public Rectangle2D getRectangle2D(String key, Rectangle2D defaultValue) {
String param = parameters.getProperty(key, "");
if (!param.equals("") && param.startsWith("r2d(") && param.endsWith(")")) {
String[] k = param.substring("r2d(".length(), param.length() - 1).split(",");
double width = Double.valueOf(k[0]);
double height = Double.valueOf(k[0]);
return new Rectangle2D.Double(0, 0, width, height);
} else {
System.err.printf("Configuration: Rectangle2D value not found for %s, use %s as default value.%n", key, defaultValue);
return defaultValue;
}
}
/**
* Parse common CLI arguments to retrieve values from.
*
* @param args a String array of value with "key=value" format.
*/
public void parseArguments(String[] args) {
for (String s : args) {
String[] p = s.split("=");
String key = p[0];
String value = p[1];
if (parameters.containsKey(s)) {
parameters.setProperty(key, value);
}
}
}
/**
* Save changes to the previously loaded properties file.
*/
public void save() {
try {
String rootPath = this.getClass().getResource("/").getPath();
OutputStream output = new FileOutputStream(rootPath + filePath);
parameters.store(output, "updated From CommandLine");
System.out.printf("Configuration:Configuration saved into the properties file: %s%n", filePath);
System.out.printf("Configuration:Content: %s%n", parameters);
} catch (IOException e) {
System.err.printf("Configuration:Unable to save configuration into properties file: %s%n", e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment