You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
5.3 KiB
274 lines
5.3 KiB
10 years ago
|
package mightypork.utils.config.propmgr;
|
||
10 years ago
|
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Map.Entry;
|
||
|
import java.util.TreeMap;
|
||
|
|
||
|
import mightypork.utils.Convert;
|
||
10 years ago
|
import mightypork.utils.config.propmgr.properties.BooleanProperty;
|
||
|
import mightypork.utils.config.propmgr.properties.DoubleProperty;
|
||
|
import mightypork.utils.config.propmgr.properties.IntegerProperty;
|
||
|
import mightypork.utils.config.propmgr.properties.StringProperty;
|
||
10 years ago
|
import mightypork.utils.logging.Log;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Property manager with advanced formatting and value checking.
|
||
|
*
|
||
|
* @author Ondřej Hruška (MightyPork)
|
||
|
*/
|
||
|
public class PropertyManager {
|
||
|
|
||
10 years ago
|
private final TreeMap<String, Property<?>> entries = new TreeMap<>();
|
||
|
private final TreeMap<String, String> renameTable = new TreeMap<>();
|
||
|
private PropertyStore props;
|
||
10 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Create property manager from file path and an initial comment.
|
||
|
*
|
||
10 years ago
|
* @param props a property store implementation backing this property
|
||
|
* manager
|
||
10 years ago
|
*/
|
||
10 years ago
|
public PropertyManager(PropertyStore props) {
|
||
|
this.props = props;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Load from file
|
||
10 years ago
|
*/
|
||
|
public void load()
|
||
|
{
|
||
10 years ago
|
props.load();
|
||
10 years ago
|
|
||
10 years ago
|
// rename keys (useful if keys change but value is to be kept)
|
||
10 years ago
|
for (final Entry<String, String> entry : renameTable.entrySet()) {
|
||
|
|
||
10 years ago
|
final String value = props.getProperty(entry.getKey());
|
||
10 years ago
|
|
||
10 years ago
|
if (value == null) continue;
|
||
10 years ago
|
|
||
10 years ago
|
final String oldKey = entry.getKey();
|
||
|
final String newKey = entry.getValue();
|
||
|
|
||
|
props.removeProperty(oldKey);
|
||
|
props.setProperty(newKey, value, entries.get(newKey).getComment());
|
||
10 years ago
|
}
|
||
|
|
||
|
for (final Property<?> entry : entries.values()) {
|
||
10 years ago
|
entry.fromString(props.getProperty(entry.getKey()));
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
public void save()
|
||
|
{
|
||
|
try {
|
||
|
final ArrayList<String> keyList = new ArrayList<>();
|
||
|
|
||
|
// validate entries one by one, replace with default when needed
|
||
|
for (final Property<?> entry : entries.values()) {
|
||
|
keyList.add(entry.getKey());
|
||
|
|
||
10 years ago
|
props.setProperty(entry.getKey(), entry.toString(), entry.getComment());
|
||
10 years ago
|
}
|
||
|
|
||
|
// removed unused props
|
||
10 years ago
|
for (final String key : props.keys()) {
|
||
|
if (!keyList.contains(key)) {
|
||
|
props.removeProperty(key);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
props.save();
|
||
10 years ago
|
} catch (final IOException ioe) {
|
||
|
ioe.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get a property entry (rarely used)
|
||
|
*
|
||
|
* @param k key
|
||
|
* @return the entry
|
||
|
*/
|
||
|
public Property<?> getProperty(String k)
|
||
|
{
|
||
|
try {
|
||
|
return entries.get(k);
|
||
|
} catch (final Exception e) {
|
||
|
Log.w(e);
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get boolean property
|
||
|
*
|
||
|
* @param k key
|
||
|
* @return the boolean found, or false
|
||
|
*/
|
||
|
public Boolean getBoolean(String k)
|
||
|
{
|
||
|
return Convert.toBoolean(getProperty(k).getValue());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get numeric property
|
||
|
*
|
||
|
* @param k key
|
||
|
* @return the int found, or null
|
||
|
*/
|
||
|
public Integer getInteger(String k)
|
||
|
{
|
||
|
return Convert.toInteger(getProperty(k).getValue());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get numeric property as double
|
||
|
*
|
||
|
* @param k key
|
||
|
* @return the double found, or null
|
||
|
*/
|
||
|
public Double getDouble(String k)
|
||
|
{
|
||
|
return Convert.toDouble(getProperty(k).getValue());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get string property
|
||
|
*
|
||
|
* @param k key
|
||
|
* @return the string found, or null
|
||
|
*/
|
||
|
public String getString(String k)
|
||
|
{
|
||
|
return Convert.toString(getProperty(k).getValue());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get arbitrary property. Make sure it's of the right type!
|
||
|
*
|
||
|
* @param k key
|
||
|
* @return the prioperty found
|
||
|
*/
|
||
|
@SuppressWarnings("unchecked")
|
||
|
public <T> T getValue(String k)
|
||
|
{
|
||
|
try {
|
||
|
return ((Property<T>) getProperty(k)).getValue();
|
||
|
} catch (final ClassCastException e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Add a boolean property
|
||
|
*
|
||
|
* @param k key
|
||
|
* @param d default value
|
||
|
* @param comment the in-file comment
|
||
|
*/
|
||
|
public void putBoolean(String k, boolean d, String comment)
|
||
|
{
|
||
|
putProperty(new BooleanProperty(k, d, comment));
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Add a numeric property (double)
|
||
|
*
|
||
|
* @param k key
|
||
|
* @param d default value
|
||
|
* @param comment the in-file comment
|
||
|
*/
|
||
|
public void putDouble(String k, double d, String comment)
|
||
|
{
|
||
|
putProperty(new DoubleProperty(k, d, comment));
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Add a numeric property
|
||
|
*
|
||
|
* @param k key
|
||
|
* @param d default value
|
||
|
* @param comment the in-file comment
|
||
|
*/
|
||
|
public void putInteger(String k, int d, String comment)
|
||
|
{
|
||
|
putProperty(new IntegerProperty(k, d, comment));
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Add a string property
|
||
|
*
|
||
|
* @param k key
|
||
|
* @param d default value
|
||
|
* @param comment the in-file comment
|
||
|
*/
|
||
|
public void putString(String k, String d, String comment)
|
||
|
{
|
||
|
putProperty(new StringProperty(k, d, comment));
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Add a generic property (can be used with custom property types)
|
||
10 years ago
|
*
|
||
10 years ago
|
* @param prop property to add
|
||
10 years ago
|
*/
|
||
|
public <T> void putProperty(Property<T> prop)
|
||
|
{
|
||
|
entries.put(prop.getKey(), prop);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Rename key before loading; value is preserved
|
||
|
*
|
||
|
* @param oldKey old key
|
||
|
* @param newKey new key
|
||
|
*/
|
||
|
public void renameKey(String oldKey, String newKey)
|
||
|
{
|
||
|
renameTable.put(oldKey, newKey);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Set value saved to certain key.
|
||
|
*
|
||
|
* @param key key
|
||
|
* @param value the saved value
|
||
|
*/
|
||
|
public void setValue(String key, Object value)
|
||
|
{
|
||
|
getProperty(key).setValue(value);
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Set heading comment of the property store.
|
||
|
*
|
||
|
* @param fileComment comment text (can be multi-line)
|
||
|
*/
|
||
10 years ago
|
public void setFileComment(String fileComment)
|
||
|
{
|
||
10 years ago
|
props.setComment(fileComment);
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|