public class ParameterDatabase extends Properties implements Serializable
This extension of the Properties class allows you to set, get, and delete Parameters in a hierarchical tree-like database. The database consists of a list of Parameters, plus an array of "parent databases" which it falls back on when it can't find the Parameter you're looking for. Parents may also have arrays of parents, and so on..
The parameters are loaded from a Java property-list file, which is basically a collection of parameter=value pairs, one per line. Empty lines and lines beginning with # are ignored. These parameters and their values are case-sensitive , and whitespace is trimmed I believe.
An optional set of parameters, "parent. n ", where n are consecutive integers starting at 0, define the filenames of the database's parents.
An optional set of parameters, "print-params", specifies whether or not parameters should be printed as they are used (through one of the get(...) methods). If print-params is unset, or set to false or FALSE, nothing is printed. If set to non-false, then the parameters are printed prepended with a "P:" when their values are requested, "E:" when their existence is tested. Prior to the "P:" or "E:" you may see a "!" (meaning that the parameter isn't in the database), or a "<" (meaning that the parameter was a default parameter which was never looked up because the primary parameter contained the value).
When you create a ParameterDatabase using new ParameterDatabase(), it is created thus:
| DATABASE: | database |
| FROM: | (empty) |
When you create a ParameterDatabase using new ParameterDatabase( file ), it is created by loading the database file, and its parent file tree, thus:
| DATABASE: | database | -> | parent0 | +-> | parent0 | +-> | parent0 | +-> | .... |
| FROM: | (empty) | (file) | | | (parent.0) | | | (parent.0) | .... | ||
| | | +-> | parent1 | +-> | .... | |||||
| | | | | (parent.1) | |||||||
| | | .... | ||||||||
| | | |||||||||
| +-> | parent1 | +-> | .... | ||||||
| | | (parent.1) | ||||||||
| .... |
When you create a ParameterDatabase using new ParameterDatabase( file,argv ), the preferred way, it is created thus:
| DATABASE: | database | -> | parent0 | +-> | parent0 | +-> | parent0 | +-> | parent0 | +-> | .... |
| FROM: | (empty) | (argv) | (file) | | | (parent.0) | | | (parent.0) | .... | |||
| | | +-> | parent1 | +-> | .... | |||||||
| | | | | (parent.1) | |||||||||
| | | .... | ||||||||||
| | | |||||||||||
| +-> | parent1 | +-> | .... | ||||||||
| | | (parent.1) | ||||||||||
| .... |
...that is, the actual top database is empty, and stores parameters added programmatically; its parent is a database formed from arguments passed in on the command line; its parent is the parameter database which actually loads from foo. This allows you to programmatically add parameters which override those in foo, then delete them, thus bringing foo's parameters back in view.
Once a parameter database is loaded, you query it with the get methods. The database, then its parents, are searched until a match is found for your parameter. The search rules are thus: (1) the root database is searched first. (2) If a database being searched doesn't contain the data, it searches its parents recursively, starting with parent 0, then moving up, until all searches are exhausted or something was found. (3) No database is searched twice.
The various get methods all take two parameters. The first parameter is fetched and retrieved first. If that fails, the second one (known as the default parameter) is fetched and retrieved. You can pass in null for the default parameter if you don't have one.
You can test a parameter for existence with the exists methods.
You can set a parameter (in the topmost database only with the set command. The remove command removes a parameter from the topmost database only. The removeDeeply command removes that parameter from every database.
The values stored in a parameter database must not contain "#", "=", non-ascii values, or whitespace.
Note for JDK 1.1 . Finally recovering from stupendous idiocy, JDK 1.2 included parseDouble() and parseFloat() commands; now you can READ A FLOAT FROM A STRING without having to create a Float object first! Anyway, you will need to modify the getFloat() method below if you're running on JDK 1.1, but understand that large numbers of calls to the method may be inefficient. Sample JDK 1.1 code is given with those methods, but is commented out.
| Modifier and Type | Field and Description |
|---|---|
(package private) Hashtable |
accessed |
static String |
C_CLASS |
static String |
C_HERE |
(package private) boolean |
checked |
(package private) File |
directory |
(package private) Hashtable |
gotten |
(package private) String |
label |
(package private) Vector |
parents |
static String |
PRINT_PARAMS |
int |
printState |
static int |
PS_NONE |
static int |
PS_PRINT_PARAMS |
static int |
PS_UNKNOWN |
(package private) Class |
relativeClass |
(package private) String |
relativePath |
static String |
UNKNOWN_VALUE |
defaults| Constructor and Description |
|---|
ParameterDatabase()
Creates an empty parameter database.
|
ParameterDatabase(Dictionary map)
Creates a new parameter database from the given Dictionary.
|
ParameterDatabase(File file)
Creates a new parameter database tree from a given database file and its
parent files.
|
ParameterDatabase(File file,
String[] args)
Creates a new parameter database from a given database file and argv
list.
|
ParameterDatabase(InputStream stream)
Creates a new parameter database loaded from the given stream.
|
ParameterDatabase(String pathNameRelativeToClassFile,
Class cls)
Creates a new parameter database loaded from a parameter file located relative to a class file,
wherever the class file may be (such as in a jar).
|
ParameterDatabase(String pathNameRelativeToClassFile,
Class cls,
String[] args)
Creates a new parameter database from a given database file and argv
list.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
_addNodeForParameter(DefaultTreeModel model,
DefaultMutableTreeNode root,
String key) |
(package private) void |
_buildTreeModel(DefaultTreeModel model,
DefaultMutableTreeNode root) |
(package private) File |
_directoryFor(Parameter parameter)
Private helper function
|
(package private) boolean |
_exists(Parameter parameter) |
(package private) String |
_get(String parameter)
Private helper function
|
(package private) ParameterDatabase |
_getLocation(String parameter)
Private helper function
|
(package private) Set |
_getShadowedValues(Parameter parameter,
Set vals) |
(package private) void |
_list(PrintWriter p,
boolean listShadowed,
String prefix,
Hashtable gather)
Private helper function.
|
(package private) void |
_removeDeeply(Parameter parameter)
Private helper function
|
void |
addParent(ParameterDatabase database) |
TreeModel |
buildTreeModel()
Builds a TreeModel from the available property keys.
|
(package private) static String |
concatenatedJarPath(URL original,
String path) |
(package private) static URL |
concatenatedJarResource(URL original,
String path) |
(package private) static URL |
defaultResourceURL(Class cls) |
(package private) File |
directoryFor(Parameter parameter)
Searches down through databases to find the directory for the database
which holds a given parameter.
|
boolean |
exists(Parameter parameter)
Deprecated.
use exists(Parameter, null)
|
boolean |
exists(Parameter parameter,
Parameter defaultParameter)
Returns true if either parameter or defaultParameter exists in the
database
|
(package private) String |
get(Parameter parameter) |
(package private) boolean |
getBoolean(Parameter parameter,
boolean defaultValue)
Searches down through databases to find a given parameter; If the
parameter does not exist, defaultValue is returned.
|
boolean |
getBoolean(Parameter parameter,
Parameter defaultParameter,
boolean defaultValue)
Searches down through databases to find a given parameter; If the
parameter does not exist, defaultValue is returned.
|
Class |
getClassForParameter(Parameter parameter,
Parameter defaultParameter,
Class mustCastTosuperclass)
Searches down through databases to find a given parameter.
|
(package private) double |
getDouble(Parameter parameter) |
(package private) double |
getDouble(Parameter parameter,
double minValue)
Searches down through databases to find a given parameter, whose value
must be a double >= minValue.
|
(package private) double |
getDouble(Parameter parameter,
double minValue,
double maxValue)
Searches down through databases to find a given parameter, whose value
must be a double >= minValue and <= maxValue.
|
double |
getDouble(Parameter parameter,
Parameter defaultParameter) |
double |
getDouble(Parameter parameter,
Parameter defaultParameter,
double minValue)
Searches down through databases to find a given parameter, whose value
must be a double >= minValue.
|
double |
getDouble(Parameter parameter,
Parameter defaultParameter,
double minValue,
double maxValue)
Deprecated.
use getDoubleWithMax instead
|
(package private) double |
getDoubleWithDefault(Parameter parameter,
double defaultValue)
Searches down through databases to find a given parameter, which must be
a float.
|
double |
getDoubleWithDefault(Parameter parameter,
Parameter defaultParameter,
double defaultValue)
Searches down through databases to find a given parameter, which must be
a float.
|
double |
getDoubleWithMax(Parameter parameter,
Parameter defaultParameter,
double minValue,
double maxValue)
Searches down through databases to find a given parameter, whose value
must be a double >= minValue and <= maxValue.
|
(package private) File |
getFile(Parameter parameter)
Searches down through the databases to find a given parameter, whose
value must be an absolute or relative path name.
|
File |
getFile(Parameter parameter,
Parameter defaultParameter)
Searches down through the databases to find a given parameter, whose
value must be an absolute or relative path name.
|
(package private) float |
getFloat(Parameter parameter) |
(package private) float |
getFloat(Parameter parameter,
double minValue)
Searches down through databases to find a given parameter, whose value
must be a float >= minValue.
|
(package private) float |
getFloat(Parameter parameter,
double minValue,
double maxValue)
Searches down through databases to find a given parameter, whose value
must be a float >= minValue and <= maxValue.
|
float |
getFloat(Parameter parameter,
Parameter defaultParameter) |
float |
getFloat(Parameter parameter,
Parameter defaultParameter,
double minValue)
Searches down through databases to find a given parameter, whose value
must be a float >= minValue.
|
float |
getFloat(Parameter parameter,
Parameter defaultParameter,
double minValue,
double maxValue)
Deprecated.
Use getFloatWithMax instead
|
(package private) float |
getFloatWithDefault(Parameter parameter,
double defaultValue)
Searches down through databases to find a given parameter, which must be
a float.
|
float |
getFloatWithDefault(Parameter parameter,
Parameter defaultParameter,
double defaultValue)
Searches down through databases to find a given parameter, which must be
a float.
|
float |
getFloatWithMax(Parameter parameter,
Parameter defaultParameter,
double minValue,
double maxValue)
Searches down through databases to find a given parameter, whose value
must be a float >= minValue and <= maxValue.
|
Object |
getInstanceForParameter(Parameter parameter,
Parameter defaultParameter,
Class mustCastTosuperclass)
Searches down through databases to find a given parameter, whose value
must be a full Class name, and the class must be a descendent of but not
equal to mustCastTosuperclass .
|
Object |
getInstanceForParameterEq(Parameter parameter,
Parameter defaultParameter,
Class mustCastTosuperclass)
Searches down through databases to find a given parameter, whose value
must be a full Class name, and the class must be a descendent, or equal
to, mustCastTosuperclass .
|
(package private) int |
getInt(Parameter parameter)
Searches down through databases to find a given parameter, whose value
must be an integer.
|
(package private) int |
getInt(Parameter parameter,
int minValue)
Searches down through databases to find a given parameter, whose value
must be an integer >= minValue.
|
int |
getInt(Parameter parameter,
Parameter defaultParameter)
Searches down through databases to find a given parameter, whose value
must be an integer.
|
int |
getInt(Parameter parameter,
Parameter defaultParameter,
int minValue)
Searches down through databases to find a given parameter, whose value
must be an integer >= minValue.
|
(package private) int |
getIntWithDefault(Parameter parameter,
int defaultValue)
Searches down through databases to find a given parameter, which must be
an integer.
|
int |
getIntWithDefault(Parameter parameter,
Parameter defaultParameter,
int defaultValue)
Searches down through databases to find a given parameter, which must be
an integer.
|
(package private) int |
getIntWithMax(Parameter parameter,
int minValue,
int maxValue)
Searches down through databases to find a given parameter, whose value
must be an integer >= minValue and <= maxValue.
|
int |
getIntWithMax(Parameter parameter,
Parameter defaultParameter,
int minValue,
int maxValue)
Searches down through databases to find a given parameter, whose value
must be an integer >= minValue and <= maxValue.
|
String |
getLabel()
Returns a String describing the location of the ParameterDatabase holding
this parameter, or "" if there is none.
|
ParameterDatabase |
getLocation(Parameter parameter) |
ParameterDatabase |
getLocation(String parameter) |
(package private) long |
getLong(Parameter parameter)
Searches down through databases to find a given parameter, whose value
must be a long.
|
(package private) long |
getLong(Parameter parameter,
long minValue)
Searches down through databases to find a given parameter, whose value
must be a long >= minValue.
|
(package private) long |
getLong(Parameter parameter,
long minValue,
long maxValue)
Deprecated.
|
long |
getLong(Parameter parameter,
Parameter defaultParameter)
Searches down through databases to find a given parameter, whose value
must be a long.
|
long |
getLong(Parameter parameter,
Parameter defaultParameter,
long minValue)
Searches down through databases to find a given parameter, whose value
must be a long >= minValue.
|
long |
getLong(Parameter parameter,
Parameter defaultParameter,
long minValue,
long maxValue)
Deprecated.
|
(package private) long |
getLongWithDefault(Parameter parameter,
long defaultValue)
Searches down through databases to find a given parameter, which must be
a long.
|
long |
getLongWithDefault(Parameter parameter,
Parameter defaultParameter,
long defaultValue)
Searches down through databases to find a given parameter, which must be
a long.
|
(package private) long |
getLongWithMax(Parameter parameter,
long minValue,
long maxValue)
Use getLongWithMax(...) instead.
|
long |
getLongWithMax(Parameter parameter,
Parameter defaultParameter,
long minValue,
long maxValue)
Searches down through databases to find a given parameter, whose value
must be a long >= minValue and = < maxValue.
|
(package private) InputStream |
getResource(Parameter parameter) |
InputStream |
getResource(Parameter parameter,
Parameter defaultParameter)
Searches down through the databases to find a given parameter, whose
value must be an absolute or relative path name.
|
Set |
getShadowedValues(Parameter parameter) |
(package private) String |
getString(Parameter parameter)
Searches down through databases to find a given parameter.
|
String |
getString(Parameter parameter,
Parameter defaultParameter)
Searches down through databases to find a given parameter.
|
String |
getStringWithDefault(Parameter parameter,
Parameter defaultParameter,
String defaultValue)
Searches down through databases to find a given parameter.
|
(package private) String |
getStringWithDefault(Parameter parameter,
String defaultValue)
Searches down through databases to find a given parameter.
|
(package private) int |
indexOfFirstWhitespace(String s) |
(package private) static boolean |
isJarFile(URL url) |
void |
list(PrintWriter p)
Prints out all the parameters in the database, but not shadowed
parameters.
|
void |
list(PrintWriter p,
boolean listShadowed)
Prints out all the parameters in the database.
|
void |
listAccessed(PrintWriter p)
Prints out all the parameters marked as accessed ("gotten" by some
getFoo(...) method), plus their values.
|
void |
listGotten(PrintWriter p)
Prints out all the parameters marked as used, plus their values.
|
void |
listNotAccessed(PrintWriter p)
Prints out all the parameters NOT marked as used, plus their values.
|
void |
listNotGotten(PrintWriter p)
Prints out all the parameters NOT marked as used, plus their values.
|
static void |
main(String[] args)
Test the ParameterDatabase
|
(package private) int |
parseInt(String string)
Parses an integer from a string, either in decimal or (if starting with
an x) in hex
|
(package private) long |
parseLong(String string)
Parses a long from a string, either in decimal or (if starting with an x)
in hex
|
(package private) void |
printGotten(Parameter parameter,
Parameter defaultParameter,
boolean exists) |
void |
remove(Parameter parameter)
Removes a parameter from the topmost database.
|
void |
removeDeeply(Parameter parameter)
Deprecated.
You shouldn't modify parent databases
|
void |
set(Parameter parameter,
String value)
Sets a parameter in the topmost database to a given value, trimmed of
whitespace.
|
(package private) static String |
simplifyPath(String pathname) |
String |
toString() |
(package private) void |
uncheck()
Clears the checked flag
|
getProperty, getProperty, list, load, load, loadFromXML, propertyNames, save, setProperty, store, store, storeToXML, storeToXML, stringPropertyNamespublic static final String C_HERE
public static final String C_CLASS
public static final String UNKNOWN_VALUE
public static final String PRINT_PARAMS
public static final int PS_UNKNOWN
public static final int PS_NONE
public static final int PS_PRINT_PARAMS
public int printState
String label
Vector parents
File directory
boolean checked
Hashtable gotten
Hashtable accessed
Class relativeClass
String relativePath
public ParameterDatabase()
public ParameterDatabase(Dictionary map) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic ParameterDatabase(String pathNameRelativeToClassFile, Class cls, String[] args) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic ParameterDatabase(String pathNameRelativeToClassFile, Class cls) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic ParameterDatabase(InputStream stream) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic ParameterDatabase(File file) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic ParameterDatabase(File file, String[] args) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic Object getInstanceForParameter(Parameter parameter, Parameter defaultParameter, Class mustCastTosuperclass) throws ParamClassLoadException
ParamClassLoadExceptionpublic Object getInstanceForParameterEq(Parameter parameter, Parameter defaultParameter, Class mustCastTosuperclass) throws ParamClassLoadException
ParamClassLoadExceptionpublic Class getClassForParameter(Parameter parameter, Parameter defaultParameter, Class mustCastTosuperclass) throws ParamClassLoadException
ParamClassLoadExceptionpublic boolean getBoolean(Parameter parameter, Parameter defaultParameter, boolean defaultValue)
boolean getBoolean(Parameter parameter, boolean defaultValue)
int parseInt(String string) throws NumberFormatException
NumberFormatExceptionlong parseLong(String string) throws NumberFormatException
NumberFormatExceptionint getInt(Parameter parameter) throws NumberFormatException
NumberFormatExceptionpublic int getInt(Parameter parameter, Parameter defaultParameter) throws NumberFormatException
NumberFormatExceptionpublic int getInt(Parameter parameter, Parameter defaultParameter, int minValue)
int getInt(Parameter parameter, int minValue)
public int getIntWithDefault(Parameter parameter, Parameter defaultParameter, int defaultValue)
int getIntWithDefault(Parameter parameter, int defaultValue)
public int getIntWithMax(Parameter parameter, Parameter defaultParameter, int minValue, int maxValue)
int getIntWithMax(Parameter parameter, int minValue, int maxValue)
float getFloat(Parameter parameter) throws NumberFormatException
NumberFormatExceptionpublic float getFloat(Parameter parameter, Parameter defaultParameter) throws NumberFormatException
NumberFormatExceptionpublic float getFloat(Parameter parameter, Parameter defaultParameter, double minValue)
float getFloat(Parameter parameter, double minValue)
public float getFloatWithDefault(Parameter parameter, Parameter defaultParameter, double defaultValue)
float getFloatWithDefault(Parameter parameter, double defaultValue)
public float getFloatWithMax(Parameter parameter, Parameter defaultParameter, double minValue, double maxValue)
public float getFloat(Parameter parameter, Parameter defaultParameter, double minValue, double maxValue)
float getFloat(Parameter parameter, double minValue, double maxValue)
double getDouble(Parameter parameter) throws NumberFormatException
NumberFormatExceptionpublic double getDouble(Parameter parameter, Parameter defaultParameter) throws NumberFormatException
NumberFormatExceptionpublic double getDouble(Parameter parameter, Parameter defaultParameter, double minValue)
double getDouble(Parameter parameter, double minValue)
public double getDoubleWithMax(Parameter parameter, Parameter defaultParameter, double minValue, double maxValue)
public double getDouble(Parameter parameter, Parameter defaultParameter, double minValue, double maxValue)
double getDouble(Parameter parameter, double minValue, double maxValue)
public double getDoubleWithDefault(Parameter parameter, Parameter defaultParameter, double defaultValue)
double getDoubleWithDefault(Parameter parameter, double defaultValue)
long getLong(Parameter parameter) throws NumberFormatException
NumberFormatExceptionpublic long getLong(Parameter parameter, Parameter defaultParameter) throws NumberFormatException
NumberFormatExceptionpublic long getLong(Parameter parameter, Parameter defaultParameter, long minValue)
long getLong(Parameter parameter, long minValue)
public long getLongWithDefault(Parameter parameter, Parameter defaultParameter, long defaultValue)
long getLongWithDefault(Parameter parameter, long defaultValue)
public long getLongWithMax(Parameter parameter, Parameter defaultParameter, long minValue, long maxValue)
long getLongWithMax(Parameter parameter, long minValue, long maxValue)
public long getLong(Parameter parameter, Parameter defaultParameter, long minValue, long maxValue)
long getLong(Parameter parameter, long minValue, long maxValue)
public File getFile(Parameter parameter, Parameter defaultParameter)
File getFile(Parameter parameter)
public InputStream getResource(Parameter parameter, Parameter defaultParameter)
int indexOfFirstWhitespace(String s)
InputStream getResource(Parameter parameter)
public String getString(Parameter parameter, Parameter defaultParameter)
String getString(Parameter parameter)
public String getStringWithDefault(Parameter parameter, Parameter defaultParameter, String defaultValue)
String getStringWithDefault(Parameter parameter, String defaultValue)
void uncheck()
public void set(Parameter parameter, String value)
public void listGotten(PrintWriter p)
public void listNotGotten(PrintWriter p)
public void listNotAccessed(PrintWriter p)
public void listAccessed(PrintWriter p)
public boolean exists(Parameter parameter)
boolean _exists(Parameter parameter)
public boolean exists(Parameter parameter, Parameter defaultParameter)
public ParameterDatabase getLocation(Parameter parameter)
public ParameterDatabase getLocation(String parameter)
ParameterDatabase _getLocation(String parameter)
File directoryFor(Parameter parameter)
public String getLabel()
public void remove(Parameter parameter)
public void removeDeeply(Parameter parameter)
void _removeDeeply(Parameter parameter)
public void addParent(ParameterDatabase database)
static boolean isJarFile(URL url)
public void list(PrintWriter p)
list in class Propertiespublic void list(PrintWriter p, boolean listShadowed)
void _list(PrintWriter p, boolean listShadowed, String prefix, Hashtable gather)
public TreeModel buildTreeModel()
void _buildTreeModel(DefaultTreeModel model, DefaultMutableTreeNode root)
void _addNodeForParameter(DefaultTreeModel model, DefaultMutableTreeNode root, String key)
model - root - e - public static void main(String[] args) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionCopyright © 2014 Evolutionary Computation Laboratory at George Mason University. All rights reserved.