Fixed a couple of bugs in the Configuration categories pull request.

Added helper functions to Property for standard int/boolean usage.
This commit is contained in:
LexManos 2012-03-25 23:26:01 -07:00
parent dec539f6f9
commit 06eb9de126
2 changed files with 92 additions and 9 deletions

View File

@ -58,15 +58,17 @@ public class Configuration
{
this.file = file;
categories.put(CATEGORY_GENERAL, generalProperties);
categories.put(CATEGORY_BLOCK, generalProperties);
categories.put(CATEGORY_BLOCK, blockProperties);
categories.put(CATEGORY_ITEM, itemProperties);
}
/**
* Helper for depreciated functions
*/
private String getCategoryFromIndex(int kind){
switch(kind){
private String getCategoryFromIndex(int kind)
{
switch(kind)
{
case 1:
return CATEGORY_BLOCK;
case 2:
@ -131,7 +133,8 @@ public class Configuration
}
@Deprecated
public Property getOrCreateIntProperty(String key, int kind, int defaultValue){
public Property getOrCreateIntProperty(String key, int kind, int defaultValue)
{
return getOrCreateIntProperty(key, getCategoryFromIndex(kind), defaultValue);
}
@ -151,7 +154,8 @@ public class Configuration
}
@Deprecated
public Property getOrCreateBooleanProperty(String key, int kind, boolean defaultValue){
public Property getOrCreateBooleanProperty(String key, int kind, boolean defaultValue)
{
return getOrCreateBooleanProperty(key, getCategoryFromIndex(kind), defaultValue);
}
@ -170,7 +174,8 @@ public class Configuration
}
@Deprecated
public Property getOrCreateProperty(String key, int kind, String defaultValue){
public Property getOrCreateProperty(String key, int kind, String defaultValue)
{
return getOrCreateProperty(key, getCategoryFromIndex(kind), defaultValue);
}
@ -179,7 +184,8 @@ public class Configuration
category = category.toLowerCase();
Map<String, Property> source = categories.get(category);
if(source == null){
if(source == null)
{
source = new TreeMap<String, Property>();
categories.put(category, source);
}
@ -296,7 +302,6 @@ public class Configuration
}
}
}
}
}
catch (IOException e)
@ -328,7 +333,8 @@ public class Configuration
buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n");
buffer.write("\r\n");
for(Map.Entry<String, Map<String, Property>> category : categories.entrySet()){
for(Map.Entry<String, Map<String, Property>> category : categories.entrySet())
{
buffer.write("####################\r\n");
buffer.write("# " + category.getKey() + " \r\n");
buffer.write("####################\r\n\r\n");

View File

@ -10,4 +10,81 @@ public class Property
public String name;
public String value;
public String comment;
/**
* Returns the value in this property as a integer,
* if the value is not a valid integer, it will return -1.
*
* @return The value
*/
public int getInt()
{
return getInt(-1);
}
/**
* Returns the value in this property as a integer,
* if the value is not a valid integer, it will return the
* provided default.
*
* @param _default The default to provide if the current value is not a valid integer
* @return The value
*/
public int getInt(int _default)
{
try
{
return Integer.parseInt(value);
}
catch (NumberFormatException e)
{
return _default;
}
}
/**
* Checks if the current value stored in this property can be converted to an integer.
* @return True if the vslue can be converted to an integer
*/
public boolean isIntValue()
{
try
{
Integer.parseInt(value);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}
/**
* Returns the value in this property as a boolean,
* if the value is not a valid boolean, it will return the
* provided default.
*
* @param _default The default to provide
* @return The value as a boolean, or the default
*/
public boolean getBoolean(boolean _default)
{
if (isBooleanValue())
{
return Boolean.parseBoolean(value);
}
else
{
return _default;
}
}
/**
* Checks if the current value held by this property is a valid boolean value.
* @return True if it is a boolean value
*/
public boolean isBooleanValue()
{
return ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase()));
}
}