public class Settings
extends java.lang.Object
init(String)). If
Settings isn't initialized, only settings in DEF_SETTINGS_FILE
are read. Normally, after initialization, settings in the given file can
override any settings defined in the default settings file and/or define
new settings.
All settings are key-value pairs. For parsing details see
Properties.getProperty(String). Value can be a single
value or comma separated list of values. With CSV values, CSV methods
must be used (e.g. getCsvInts(String, int)). Setting value should
not start and end with a bracket since those are reserved for run-specific
values (see setRunIndex(int)). In file paths directory separator
should always be forward slash ("/").
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEF_SETTINGS_FILE
file name of the default settings file ("default_settings.txt")
|
static java.lang.String |
FILL_DELIMITER
delimiter for requested values in strings ("%%")
|
protected static java.util.Properties |
props
properties object where the setting files are read into
|
static java.lang.String |
SETTING_OUTPUT_S
Setting to define the file name where all read settings are written
("Settings.output".
|
| Constructor and Description |
|---|
Settings()
Create a setting object without namespace.
|
Settings(java.lang.String namespace)
Creates a setting object with a namespace.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
addSettings(java.lang.String propFile)
Reads another settings file and adds the key-value pairs to the current
settings overriding any values that already existed with the same keys.
|
void |
assertValidRange(int[] range,
java.lang.String sname)
Checks that the given integer array contains a valid range.
|
boolean |
contains(java.lang.String name)
Returns true if a setting with defined name (in the current namespace
or secondary namespace if such is set) exists and has some value
(not just white space)
|
java.lang.Object |
createIntializedObject(java.lang.String className)
Creates (and dynamically loads the class of) an object that
initializes itself using the settings in this Settings object
(given as the only parameter to the constructor).
|
java.lang.Object |
createObject(java.lang.String className)
Creates (and dynamically loads the class of) an object using the
constructor without any parameters.
|
void |
ensurePositiveValue(double value,
java.lang.String settingName)
Makes sure that the given settings value is positive
|
boolean |
getBoolean(java.lang.String name)
Returns a boolean-valued setting
|
boolean |
getBoolean(java.lang.String name,
boolean defaultValue)
Returns the given boolean setting if it exists, or defaultValue if the
setting does not exist
|
ArithmeticCondition |
getCondition(java.lang.String name)
Returns an ArithmeticCondition setting.
|
double[] |
getCsvDoubles(java.lang.String name)
Returns an array of CSV setting double values.
|
double[] |
getCsvDoubles(java.lang.String name,
int expectedCount)
Returns an array of CSV setting double values containing expected
amount of values.
|
int[] |
getCsvInts(java.lang.String name)
Returns an array of CSV setting integer values
|
int[] |
getCsvInts(java.lang.String name,
int expectedCount)
Returns an array of CSV setting integer values
|
util.Range[] |
getCsvRanges(java.lang.String name)
Returns comma-separated ranges (e.g., "3-5, 17-20, 15")
|
java.lang.String[] |
getCsvSetting(java.lang.String name)
Returns a CSV setting.
|
java.lang.String[] |
getCsvSetting(java.lang.String name,
int expectedCount)
Returns a CSV setting containing expected amount of values.
|
double |
getDouble(java.lang.String name)
Returns a double-valued setting
|
double |
getDouble(java.lang.String name,
double defaultValue)
Returns a double-valued setting, or the default value if the given
setting does not exist
|
java.lang.String |
getFullPropertyName(java.lang.String setting)
Returns full (namespace prefixed) property name for a setting.
|
int |
getInt(java.lang.String name)
Returns an integer-valued setting
|
int |
getInt(java.lang.String name,
int defaultValue)
Returns an integer-valued setting, or the default value if the
setting does not exist
|
java.lang.String |
getNameSpace()
Returns the namespace of the settings object
|
java.lang.String |
getSecondaryNameSpace()
Returns the secondary namespace of the settings object
|
java.lang.String |
getSetting(java.lang.String name)
Returns a String-valued setting.
|
java.lang.String |
getSetting(java.lang.String name,
java.lang.String defaultValue)
Returns the given setting if it exists, or defaultValue if the setting
does not exist
|
static void |
init(java.lang.String propFile)
Initializes the settings all Settings objects will use.
|
void |
restoreNameSpace()
Restores the namespace that was in use before a call to setNameSpace
|
void |
restoreSecondaryNamespace()
Restores the secondary namespace that was in use before a call to
setSecondaryNameSpace
|
void |
restoreSubNameSpace()
Reverts the change made with
setSubNameSpace(String), i.e.,
restores both the primary and secondary namespace. |
void |
setNameSpace(java.lang.String namespace)
Sets the namespace to something else than the current namespace.
|
static void |
setRunIndex(int index)
Sets the run index for the settings (only has effect on settings with
run array).
|
void |
setSecondaryNamespace(java.lang.String namespace)
Sets a secondary namespace where a setting is searched from if it
isn't found from the primary namespace.
|
void |
setSubNameSpace(java.lang.String namespace)
Appends the given namespace to the the current namespace,
for both the primary and secondary namespace .
|
java.lang.String |
toString()
Returns a String representation of the stored settings
|
java.lang.String |
valueFillString(java.lang.String input)
Fills a String formatted in a special way with values from Settings.
|
protected static java.util.Properties props
public static final java.lang.String DEF_SETTINGS_FILE
public static final java.lang.String SETTING_OUTPUT_S
public static final java.lang.String FILL_DELIMITER
valueFillString(String),
Constant Field Valuespublic Settings(java.lang.String namespace)
namespace - Namespace to usepublic Settings()
public static void setRunIndex(int index)
[settingFor1stRun ; settingFor2ndRun ; SettingFor3rdRun]
runIndex % arrayLength).
To disable whole run-index-thing, set index to value smaller than
zero (e.g. -1). When disabled, run-arrays are returned as normal values,
including the brackets.index - The run index to use for subsequent settings calls, or
-1 to disable run indexingpublic void assertValidRange(int[] range,
java.lang.String sname)
throws SettingsError
first_value <= second_value.range - The range arraysname - Name of the setting (for error messages)SettingsError - If the given array didn't qualify as a rangepublic void ensurePositiveValue(double value,
java.lang.String settingName)
value - Value to checksettingName - Name of the setting (for error's message)SettingsError - if the value was not positivepublic void setNameSpace(java.lang.String namespace)
restoreNameSpace()namespace - The new namespacepublic void setSubNameSpace(java.lang.String namespace)
restoreNameSpace() and
restoreSecondaryNamespace().namespace - The new namespace to appendpublic java.lang.String getFullPropertyName(java.lang.String setting)
setting - The name of the settingpublic java.lang.String getNameSpace()
public java.lang.String getSecondaryNameSpace()
public void setSecondaryNamespace(java.lang.String namespace)
restoreSecondaryNamespace()namespace - The new secondary namespace or null if secondary
namespace is not used (default behavior)public void restoreNameSpace()
setNameSpace(String)public void restoreSecondaryNamespace()
setSecondaryNamespace(String)public void restoreSubNameSpace()
setSubNameSpace(String), i.e.,
restores both the primary and secondary namespace.public static void init(java.lang.String propFile)
throws SettingsError
DEF_SETTINGS_FILE, if exists, is always read.propFile - Path to the property file where additional settings
are read from or null if no additional settings files are needed.SettingsError - If loading the settings file(s) didn't succeedpublic static void addSettings(java.lang.String propFile)
throws SettingsError
propFile - Path to the property fileSettingsError - If loading the settings file didn't succeedinit(String)public boolean contains(java.lang.String name)
name - Name of the setting to checkpublic java.lang.String getSetting(java.lang.String name)
name - Name of the setting to getSettingsError - if the setting is not found from either one of
the namespacespublic java.lang.String getSetting(java.lang.String name,
java.lang.String defaultValue)
name - The name of the settingdefaultValue - The value to return if the given setting didn't existpublic double getDouble(java.lang.String name)
name - Name of the setting to getpublic double getDouble(java.lang.String name,
double defaultValue)
name - Name of the setting to getdefaultValue - The value to return if the setting doesn't existpublic java.lang.String[] getCsvSetting(java.lang.String name)
name - Name of the settingSettingsError - if something went wrong with readingpublic java.lang.String[] getCsvSetting(java.lang.String name,
int expectedCount)
name - Name of the settingexpectedCount - how many values are expectedSettingsError - if something went wrong with reading or didn't
read the expected amount of values.public double[] getCsvDoubles(java.lang.String name,
int expectedCount)
name - Name of the settingexpectedCount - how many values are expectedgetCsvSetting(String, int)public double[] getCsvDoubles(java.lang.String name)
name - Name of the settinggetCsvSetting(String)public int[] getCsvInts(java.lang.String name,
int expectedCount)
name - Name of the settingexpectedCount - how many values are expectedgetCsvSetting(String, int)public int[] getCsvInts(java.lang.String name)
name - Name of the settinggetCsvSetting(String, int)public util.Range[] getCsvRanges(java.lang.String name)
name - Name of the settingSettingsError - if something went wrong with readingpublic int getInt(java.lang.String name)
name - Name of the setting to getpublic int getInt(java.lang.String name,
int defaultValue)
name - Name of the setting to getdefaultValue - The value to return if the setting didn't existpublic boolean getBoolean(java.lang.String name)
name - Name of the setting to getSettingsError - if the value wasn't any recognized valuegetSetting(String)public boolean getBoolean(java.lang.String name,
boolean defaultValue)
name - The name of the settingdefaultValue - The value to return if the given setting didn't existpublic ArithmeticCondition getCondition(java.lang.String name)
ArithmeticConditionname - Name of the setting to getSettingsError - if the value wasn't a valid arithmetic expressionpublic java.lang.Object createIntializedObject(java.lang.String className)
className - Name of the class of the objectSettingsError - if object couldn't be createdpublic java.lang.Object createObject(java.lang.String className)
className - Name of the class of the objectSettingsError - if object couldn't be createdpublic java.lang.String valueFillString(java.lang.String input)
FILL_DELIMITER). Values for those settings
are retrieved and filled in the place of place holders.input - The input string that may contain value requestsSettingsError - if such settings were not foundpublic java.lang.String toString()
toString in class java.lang.Object