Package org.pipservices3.commons.config
Class ConfigParams
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<String,String>
org.pipservices3.commons.data.StringValueMap
org.pipservices3.commons.config.ConfigParams
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,
String>
Contains a key-value map with configuration parameters.
All values stored as strings and can be serialized as JSON or string forms.
When retrieved the values can be automatically converted on read using
getAsXXX()
methods.
The keys are case-sensitive, so it is recommended to use consistent C-style as: "my_param"
Configuration parameters can be broken into sections and subsections using dot notation as:
"section1.subsection1.param1"
. Using getSection()
method all parameters from specified section
can be extracted from a ConfigMap.
The ConfigParams supports serialization from/to plain strings as:
"key1=123;key2=ABC;key3=2016-09-16T00:00:00.00Z"
ConfigParams are used to pass configurations to IConfigurable
objects.
They also serve as a basis for more concrete configurations such as ConnectionParams
or CredentialParams.
### Example ###
ConfigParams config = ConfigParams.fromTuples(
"section1.key1", "AAA",
"section1.key2", 123,
"section2.key1", true
);
config.getAsString("section1.key1"); // Result: AAA
config.getAsInteger("section1.key1"); // Result: 0
ConfigParams section1 = config.getSection("section1");
section1.toString(); // Result: key1=AAA;key2=123
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorDescriptionConfigParams
(Map<?, ?> values) Creates a new ConfigParams and fills it with values. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSection
(String section, ConfigParams sectionParams) Adds parameters into this ConfigParams under specified section.static ConfigParams
fromString
(String line) Creates a new ConfigParams object filled with key-value pairs serialized as a string.static ConfigParams
fromTuples
(Object... tuples) Creates a new ConfigParams object filled with provided key-value pairs called tuples.static ConfigParams
Creates a new ConfigParams object filled with key-value pairs from specified object.getSection
(String section) Gets parameters from specific section stored in this ConfigMap.Gets a list with all 1st level section names.static ConfigParams
mergeConfigs
(ConfigParams... configs) Merges two or more ConfigParams into one.override
(ConfigParams configParams) Overrides parameters with new values from specified ConfigParams and returns a new ConfigParams object.setDefaults
(ConfigParams defaultConfigParams) Set default values from specified ConfigParams and returns a new ConfigParams object.Methods inherited from class org.pipservices3.commons.data.StringValueMap
append, clone, fromMaps, fromTuplesArray, get, getAsArray, getAsArrayWithDefault, getAsBoolean, getAsBooleanWithDefault, getAsDateTime, getAsDateTimeWithDefault, getAsDouble, getAsDoubleWithDefault, getAsDuration, getAsDurationWithDefault, getAsEnum, getAsEnumWithDefault, getAsFloat, getAsFloatWithDefault, getAsInteger, getAsIntegerWithDefault, getAsLong, getAsLongWithDefault, getAsMap, getAsMapWithDefault, getAsNullableArray, getAsNullableBoolean, getAsNullableDateTime, getAsNullableDouble, getAsNullableDuration, getAsNullableEnum, getAsNullableFloat, getAsNullableInteger, getAsNullableLong, getAsNullableMap, getAsNullableString, getAsNullableType, getAsObject, getAsObject, getAsString, getAsStringWithDefault, getAsType, getAsTypeWithDefault, getAsValue, getKeys, length, put, setAsObject, setAsObject, toString
Methods inherited from class java.util.HashMap
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
Methods inherited from class java.util.AbstractMap
equals, hashCode
-
Constructor Details
-
ConfigParams
public ConfigParams() -
ConfigParams
Creates a new ConfigParams and fills it with values.- Parameters:
values
- (optional) an object to be converted into key-value pairs to initialize this config map.- See Also:
-
-
Method Details
-
getSectionNames
Gets a list with all 1st level section names.- Returns:
- a list of section names stored in this ConfigMap.
-
getSection
Gets parameters from specific section stored in this ConfigMap. The section name is removed from parameter keys.- Parameters:
section
- name of the section to retrieve configuration parameters from.- Returns:
- all configuration parameters that belong to the section named 'section'.
-
addSection
Adds parameters into this ConfigParams under specified section. Keys for the new parameters are appended with section dot prefix.- Parameters:
section
- name of the section where add new parameterssectionParams
- new parameters to be added.
-
override
Overrides parameters with new values from specified ConfigParams and returns a new ConfigParams object.- Parameters:
configParams
- ConfigMap with parameters to override the current values.- Returns:
- a new ConfigParams object.
- See Also:
-
setDefaults
Set default values from specified ConfigParams and returns a new ConfigParams object.- Parameters:
defaultConfigParams
- ConfigMap with default parameter values.- Returns:
- a new ConfigParams object.
- See Also:
-
fromValue
Creates a new ConfigParams object filled with key-value pairs from specified object.- Parameters:
value
- an object with key-value pairs used to initialize a new ConfigParams.- Returns:
- a new ConfigParams object.
- See Also:
-
fromTuples
Creates a new ConfigParams object filled with provided key-value pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs.- Parameters:
tuples
- the tuples to fill a new ConfigParams object.- Returns:
- a new ConfigParams object.
- See Also:
-
fromString
Creates a new ConfigParams object filled with key-value pairs serialized as a string.- Parameters:
line
- a string with serialized key-value pairs as "key1=value1;key2=value2;..." Example: "Key1=123;Key2=ABC;Key3=2016-09-16T00:00:00.00Z"- Returns:
- a new ConfigParams object.
- See Also:
-
mergeConfigs
Merges two or more ConfigParams into one. The following ConfigParams override previously defined parameters.- Parameters:
configs
- a list of ConfigParams objects to be merged.- Returns:
- a new ConfigParams object.
- See Also:
-