//*********************************************************************************
// class tig.prefs.TigProperties
//*********************************************************************************
package tig;
import tig.Strings;
import java.util.Properties;
import java.lang.reflect.Field;
/******************************************************************
Superclass for Preferences classes.
The getXxxProperty() method behave like method getProperty(String key), inherited from Properties class : if the property can't be correctly retrieved, an exception is thrown.
@author Thierry Graff
@history sep 23 2001 : Creation
@history feb 04 2002 : Added default mechanism.
Renamed TigProperties from TigPrefs.
@todo add a method : public abstract TigProperties getDefault() ???
@todo add a method : public abstract String getPath() ???
*****************************************************************/
public class TigProperties extends Properties{
//=================================================================================
// PUBLIC STATIC METHODS
//=================================================================================
//******************************************************************
/** Convenient method to get a static final int field of a class, using reflection.
For example, if you have this piece of code in a class :
package mypackage;
public class MyClass{
public static final int MY_CONSTANT = 8;
}
The call : TigBundle.getIntConstant("My_CONSTANT", "mypackage.MyClass"); will then return 8.
@throws RuntimeException If the property can't be correctly retrieved.
*/
public static int getIntConstant(String constantName, String className){
try{
Class theClass = Class.forName(className);
Field field = theClass.getDeclaredField(constantName);
return field.getInt(null);
}
catch(Exception e){
throw new RuntimeException(e.toString());
}
}// end getIntConstant
//=================================================================================
// PUBLIC METHODS
//=================================================================================
//************************** Integers ****************************************
/** Returns a property of type int.
@throws NumberFormatException If the property can't be retrieved, or can't be converted to an integer.
*/
public int getIntProperty(String key){
return Integer.parseInt(this.getProperty(key));
}// end getIntProperty
/** Returns a property of type int.
If the property can't be retrieved, or can't be converted to an integer,
the default value is returned.
*/
public int getIntProperty(String key, int defaultValue){
try{
int i = Integer.parseInt(this.getProperty(key));
return i;
}
catch(Exception e){
return defaultValue;
}
}// end getIntProperty
/** Sets a property of type int. */
public void setIntProperty(String key, int intValue){
this.setProperty(key, Integer.toString(intValue));
}// end setIntProperty
//************************** Doubles ****************************************
/** Returns a property of type double.
If the property can't be retrieved, or if it can't be converted to a double,
null is returned.
*/
public double getDoubleProperty(String key){
return Double.parseDouble(this.getProperty(key));
}// end getDoubleProperty
/** Returns a property of type double.
If the property can't be retrieved, or can't be converted to a double,
the default value is returned.
*/
public double getDoubleProperty(String key, double defaultValue){
try{
double d = Double.parseDouble(this.getProperty(key));
return d;
}
catch (Exception e){
return defaultValue;
}
}// end getDoubleProperty
/** Sets a property of type double. */
public void setDoubleProperty(String key, double doubleValue){
this.setProperty(key, Double.toString(doubleValue));
}// end setDoubleProperty
//************************** Booleans ****************************************
/** Returns a property of type boolean.
Returns true if the property exists and equals (ignoring case) to "true".
Returns false if the property exists and equals (ignoring case) to "false".
A RuntimeException is thrown otherwise.
*/
public boolean getBooleanProperty(String key){
String value = this.getProperty(key);
if(value == null) throw new RuntimeException("Parameter 'key' can't be converted to a boolean");
else if(value.equalsIgnoreCase("true")) return true;
else if(value.equalsIgnoreCase("false")) return false;
else throw new RuntimeException("Parameter 'key' can't be converted to a boolean");
}// end getBooleanProperty
/** Returns a property of type boolean.
Returns true if the property exists and equals (ignoring case) to "true".
Returns false if the property exists and equals (ignoring case) to "false".
'defaultValue' is returned otherwise.
*/
public boolean getBooleanProperty(String key, boolean defaultValue){
String value = this.getProperty(key);
if(value == null) return defaultValue;
else if(value.equalsIgnoreCase("true")) return true;
else if(value.equalsIgnoreCase("false")) return false;
else return defaultValue;
}// end getBooleanProperty
/** Sets a property of type boolean. */
public void setBooleanProperty(String key, boolean booleanValue){
String strValue;
if (booleanValue) strValue = "true";
else strValue = "false";
this.setProperty(key, strValue);
}// end getBooleanProperty
//************************** Integer arrays ****************************************
/** Returns a property of type int[].
The value corresponding to key is a Sting, and must contain a list of
substrings which can be parsed to integers, and separated by comas.
For example, "12, 23, 34" is a correct value.
@throws RuntimeException If the property corresponding to 'key' can't be parsed that way.
*/
public int[] getIntArrayProperty(String key){
String[] values = Strings.stringToStringArray(this.getProperty(key), ',');
int[] res = new int[values.length];
for (int i = 0; i < values.length; i++){
try{
res[i] = Integer.parseInt(values[i]);
}
catch(Exception e){
throw new RuntimeException("'" + values[i] + "' can't be converted to an integer");
}
}
return res;
}// end getIntArrayProperty
/** Returns a property of type int[].
The value corresponding to key is a Sting, and must contain a list of
substrings which can be parsed to integers, and separated by comas.
For example, "12, 23, 34" is a correct value.
If the property corresponding to 'key' can't be parsed that way, 'defaultValue' is returned.
*/
public int[] getIntArrayProperty(String key, int[] defaultValue){
try{
String[] values = Strings.stringToStringArray(this.getProperty(key), ',');
int[] res = new int[values.length];
for (int i = 0; i < values.length; i++){
res[i] = Integer.parseInt(values[i]);
}
return res;
}
catch(Exception e){
return defaultValue;
}
}// end getIntArrayProperty
/** Sets a property of type int[].
The int[] is transformed to a String.
Ex : if 'intArray' contains 1, 4, 7 and 5, it will be stored as "1, 4, 7, 5".
int[] stored this way can be retrived with {@link #getIntArrayProperty(String)}
*/
public void setIntArrayProperty(String key, int[] intArray){
this.setProperty(key, Strings.intArrayToString(intArray));
}// end setIntArrayProperty
}// end class TigProperties
Ads help maintain this website.