public class ConfigUtils extends Object
The configuration map is constructed with a String
key and an
Object
value. The value is read from the configuration map and
converted to the proper type.
If an error occurs reading the desired value from the configuration map,the error is reflected in the log and a default value (provided) will be returned.
Modifier and Type | Method and Description |
---|---|
static boolean |
readBoolean(Map<String,Object> cfg,
String key,
boolean defaultValue)
Read a boolean value from the configuration map.
|
static int |
readInt(Map<String,Object> cfg,
String key,
Validator<Integer> validator,
int defaultValue)
Read an integer value from the configuration map.
|
static long |
readLong(Map<String,Object> cfg,
String key,
Validator<Long> validator,
long defaultValue)
Read a long value from the configuration map.
|
static int |
readPort(Map<String,Object> cfg,
String key,
Validator<Integer> validator,
int defaultValue)
Read a port number from the configuration map.
|
static String |
readString(Map<String,Object> cfg,
String key,
Validator<String> validator,
String defaultValue)
Read a string value from the configuration.
|
public static int readInt(Map<String,Object> cfg, String key, Validator<Integer> validator, int defaultValue)
The property must be represented in the configuration map as either an Integer, an array of integers or as a String, otherwise the given default value is returned.
If the property is not found in the configuration map, based on the supplied key, the given default value is returned.
If the property is represented as an array of integers the given default value is returned.
If the property is represented as a String, the string is converted to an integer. In the case of an empty string or one that cannot be converted to an integer, the given default value is returned.
If the supplied validator is not null, the integer read from the configuration map is passed to the validator for additional validation. If the validator does not validate the integer, the default value is returned.
cfg
- the configuration mapkey
- the key to locate the property in the mapvalidator
- the validator for additional validationdefaultValue
- the default value to usepublic static long readLong(Map<String,Object> cfg, String key, Validator<Long> validator, long defaultValue)
The property must be represented in the configuration map as either a Long, an array of longs or as a String, otherwise the given default value is returned.
If the property is not found in the configuration map, based on the supplied key, the given default value is returned.
If the property is represented as an array of longs the given default value is returned.
If the property is represented as a String, the string is converted to a long. In the case of an empty string or one that cannot be converted to a long, the given default value is returned.
If the property found in the configuration map is less than zero the given default value is returned.
If the supplied validator is not null, the long read from the configuration map is passed to the validator for additional validation. If the validator does not validate the long, the default value is returned.
cfg
- the configuration mapkey
- the key to locate the property in the mapvalidator
- the validator for additional validationdefaultValue
- the default value to usepublic static int readPort(Map<String,Object> cfg, String key, Validator<Integer> validator, int defaultValue)
The property must be represented in the configuration map as either an Integer, an array of integers or as a String, otherwise the given default value is returned.
If the property is not found in the configuration map, based on the supplied key, the given default value is returned.
If the property is represented as an array of integers the first entry in the integer array is returned.
If the property is represented as a String, the string is converted to an integer. In the case of an empty string or one that cannot be converted to an integer, the given default value is returned.
If the property found in the configuration map will not fit in an unsigned 16-bit field the given default value is returned.
If the supplied validator is not null, the long read from the configuration map is passed to the validator for additional validation. If the validator does not validate the long, the default value is returned.
cfg
- the configuration mapkey
- the key to locate the property in the mapvalidator
- the validator for additional validationdefaultValue
- the default value to usepublic static boolean readBoolean(Map<String,Object> cfg, String key, boolean defaultValue)
The property must be represented in the configuration map as either a Boolean or as a String, otherwise the given default value is returned.
If the property is not found in the configuration map, based on the supplied key, the given default value is returned.
If the property is represented as a String, the string is converted to an boolean. In the case of an empty string the given default value is returned.
cfg
- the configuration mapkey
- the key to locate the property in the mapdefaultValue
- the default value to usepublic static String readString(Map<String,Object> cfg, String key, Validator<String> validator, String defaultValue)
The property must be represented in the configuration map as either a String or as an array of Strings, otherwise the given default value is returned.
If the property is not found in the configuration map, based on the supplied key, the given default value is returned.
If the property is represented as an array of Strings the first entry in the String array is returned.
If the property is an empty String the given default value is returned.
If the supplied validator is not null, the long read from the configuration map is passed to the validator for additional validation. If the validator does not validate the long, the default value is returned.
cfg
- the configuration mapkey
- the key to locate the property in the mapvalidator
- the validator for additional validationdefaultValue
- the default value to useCopyright © 2015. All Rights Reserved.