Package edu.wisc.game.util
Class ParseConfig
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,Object>
public final class ParseConfig extends Hashtable<String,Object>
This class is used to obtain configuration parameters, from a configuration
file or from Java System Properties. If this is an applet,
SecurityException is caught safely.
- Author:
- Qin Shi, Vladimir Menkov //@date 1999-2018
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description ParseConfig()
Creates an empty hashtable.ParseConfig(Reader in)
Creates a hashtable that contains the parsed data obtained from an open reader (which may, for example, be associated with an open file), and then closes the reader.ParseConfig(String aFname)
Creates a hashtable that contains the parsed contents of the specified configuration file. -
Method Summary
Modifier and Type Method Description boolean
containsKey(String aName)
String[]
enrichFromArgv(String[] argv)
Scans the argv array, identifying all elements of the form X=Y.boolean
getBoolean(String name, boolean defVal)
double
getDouble(String name, double defVal)
<T extends Enum<T>>
TgetEnum(Class<T> retType, String name, T defVal)
int
getInt(String name, int defVal)
long
getLong(String name, long defVal)
boolean
getOption(String aName, boolean aDefault)
Gets the requested integer value from the hash table or from the Java system property aName.int
getOption(String aName, int aDefault)
Gets the requested integer value from the hash table or from the Java system property aName.String
getOption(String aName, String aDefault)
Gets the requested value from the hash table or from the Java system property aName.Date
getOptionDate(String aName, String aDefault)
Gets a date parameter, in the format YYYY-MM-DDdouble
getOptionDouble(String aName, double aDefault)
Gets the requested double value from the hash table or from the Java system property aName.<T extends Enum<T>>
TgetOptionEnum(Class<T> retType, String aName, T defVal)
Returns the value of the specified parameter if it can be interpreted as a value of the specified enumerated type.<T extends Enum<T>>
T[]getOptionEnumArray(Class<T> retType, String aName, T[] defVal)
Reads a comma-separated array of enums.long
getOptionLong(String aName, long aDefault)
String
getParameter(String aName)
Gets the requested value from the hash table.String
getString(String name, String defVal)
static void
main(String[] argv)
Purely for testing.Methods inherited from class java.util.Hashtable
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, size, toString, values
-
Constructor Details
-
ParseConfig
public ParseConfig()Creates an empty hashtable. That can be used simply as a convenient interface for accessing Java system options. -
ParseConfig
Creates a hashtable that contains the parsed contents of the specified configuration file.- Parameters:
aFname
- Configuration file name.- Throws:
FileNotFoundException
IOException
-
ParseConfig
Creates a hashtable that contains the parsed data obtained from an open reader (which may, for example, be associated with an open file), and then closes the reader.The configuration file syntax:
- Lines (or "tails" of lines) beginning with a '#' are comments, and are ignored
- Blank lines are ignored
- A line of the form
name value
orname = value
assigns a value to the named variable. The equal sign is optional. There can be a semicolon at the end of the line, but it's optional. The value may be a number (with no quotes), or a string surrounded by double quotes. If the string consists only of alphanumeric characters, with possible '/' and ':' chars, then quotes are optional too.
A ParseConfig structure is created by reading a specified configuration file. The values of parameters stored in the table can be accessed by using accessor methods, such as getOption or getOptionDouble.
This method throws various exceptions, so that the caller method could produce a meaningful error report.
- Parameters:
in
- A Reader (a file reader, etc.)- Throws:
IOException
-
-
Method Details
-
getOption
Gets the requested value from the hash table or from the Java system property aName. The Java system property, if given, overrides the value from the hash table. -
getOptionDouble
Gets the requested double value from the hash table or from the Java system property aName. -
getOption
Gets the requested integer value from the hash table or from the Java system property aName. -
getOptionLong
-
getOption
Gets the requested integer value from the hash table or from the Java system property aName. -
getOptionDate
Gets a date parameter, in the format YYYY-MM-DD- Parameters:
aDefault
- Default date, in format 'YYYY-MM-DD'- Throws:
ParseException
-
getOptionEnum
Returns the value of the specified parameter if it can be interpreted as a value of the specified enumerated type. If no parameter with the specified value has been supplied, or if its value cannot be interpreted as a value of the desired type, the supplied default value is returned- Parameters:
defVal
- The default value to be returned. May be null.
-
getOptionEnumArray
Reads a comma-separated array of enums. E.g. -Dcolors=RED,GREEN,BLUE -
containsKey
-
getParameter
Gets the requested value from the hash table. If the value is not found, IOException is thrown.- Throws:
IOException
-
getLong
-
getInt
-
getDouble
-
getString
-
getBoolean
-
getEnum
-
enrichFromArgv
Scans the argv array, identifying all elements of the form X=Y. For each such element, adds the (key,value) pair (X,Y) to this ParseConfig table.- Returns:
- An array that contains all other elements from argv (those not of the form X=Y).
-
main
Purely for testing.- Throws:
FileNotFoundException
IOException
-