Package org.pipservices3.commons.data
Class StringValueMap
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,
String>
- Direct Known Subclasses:
ConfigParams
,FilterParams
Cross-language implementation of a map (dictionary) where all keys and values are strings.
The stored values can be converted to different types using variety of accessor methods.
The string map is highly versatile. It can be converted into many formats, stored and sent over the wire.
This class is widely used in Pip.Services as a basis for variety of classes, such as
ConfigParams
,
ConnectionParams,
CredentialParams and others.
### Example ###
StringValueMap value1 = StringValueMap.fromString("key1=1;key2=123.456;key3=2018-01-01");
value1.getAsBoolean("key1"); // Result: true
value1.getAsInteger("key2"); // Result: 123
value1.getAsFloat("key2"); // Result: 123.456
value1.getAsDateTime("key3"); // Result: new Date(2018,0,1)
-
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
ConstructorDescriptionStringValueMap
(Map<?, ?> map) Creates a new instance of the map and assigns its value. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Appends new elements to this map.clone()
Creates a binary clone of this object.static StringValueMap
Creates a new AnyValueMap by merging two or more maps.static StringValueMap
fromString
(String line) Parses semicolon-separated key-value pairs and returns them as a StringValueMap.static StringValueMap
fromTuples
(Object... tuples) Creates a new StringValueMap from a list of key-value pairs called tuples.static StringValueMap
fromTuplesArray
(Object[] tuples) Creates a new StringValueMap from a list of key-value pairs called tuples.Gets a map element specified by its key.getAsArray
(String key) Converts map element into an AnyValueArray or returns empty AnyValueArray if conversion is not possible.getAsArrayWithDefault
(String key, AnyValueArray defaultValue) Converts map element into an AnyValueArray or returns default value if conversion is not possible.boolean
getAsBoolean
(String key) Converts map element into a boolean or returns false if conversion is not possible.boolean
getAsBooleanWithDefault
(String key, boolean defaultValue) Converts map element into a boolean or returns default value if conversion is not possible.getAsDateTime
(String key) Converts map element into a Date or returns the current date if conversion is not possible.getAsDateTimeWithDefault
(String key, ZonedDateTime defaultValue) Converts map element into a Date or returns default value if conversion is not possible.double
getAsDouble
(String key) Converts map element into a double or returns 0 if conversion is not possible.double
getAsDoubleWithDefault
(String key, double defaultValue) Converts map element into a double or returns default value if conversion is not possible.getAsDuration
(String key) getAsDurationWithDefault
(String key, Duration defaultValue) <T extends Enum<T>>
T<T extends Enum<T>>
TgetAsEnumWithDefault
(Class<T> type, String key, T defaultValue) float
getAsFloat
(String key) Converts map element into a float or returns 0 if conversion is not possible.float
getAsFloatWithDefault
(String key, float defaultValue) Converts map element into a float or returns default value if conversion is not possible.int
getAsInteger
(String key) Converts map element into an integer or returns 0 if conversion is not possible.int
getAsIntegerWithDefault
(String key, int defaultValue) Converts map element into an integer or returns default value if conversion is not possible.long
Converts map element into a long or returns 0 if conversion is not possible.long
getAsLongWithDefault
(String key, long defaultValue) Converts map element into a long or returns default value if conversion is not possible.Converts map element into an AnyValueMap or returns empty AnyValueMap if conversion is not possible.getAsMapWithDefault
(String key, AnyValueMap defaultValue) Converts map element into an AnyValueMap or returns default value if conversion is not possible.getAsNullableArray
(String key) Converts map element into an AnyValueArray or returns null if conversion is not possible.Converts map element into a boolean or returns null if conversion is not possible.Converts map element into a Date or returns null if conversion is not possible.Converts map element into a double or returns null if conversion is not possible.<T extends Enum<T>>
TgetAsNullableEnum
(Class<T> type, String key) getAsNullableFloat
(String key) Converts map element into a float or returns null if conversion is not possible.Converts map element into an integer or returns null if conversion is not possible.getAsNullableLong
(String key) Converts map element into a long or returns null if conversion is not possible.getAsNullableMap
(String key) Converts map element into an AnyValueMap or returns null if conversion is not possible.Converts map element into a string or returns null if conversion is not possible.<T> T
getAsNullableType
(Class<T> type, String key) Converts map element into a value defined by specied typecode.Gets the value stored in this map element without any conversionsgetAsObject
(String key) Gets the value stored in map element without any conversions.getAsString
(String key) Converts map element into a string or returns "" if conversion is not possible.getAsStringWithDefault
(String key, String defaultValue) Converts map element into a string or returns default value if conversion is not possible.<T> T
Converts map element into a value defined by specied typecode.<T> T
getAsTypeWithDefault
(Class<T> type, String key, T defaultValue) Converts map element into a value defined by specied typecode.getAsValue
(String key) Converts map element into an AnyValue or returns an empty AnyValue if conversion is not possible.getKeys()
Gets keys of all elements stored in this map.int
length()
Gets a number of elements stored in this map.void
Puts a new value into map element specified by its key.void
setAsObject
(Object value) Sets a new value for this array elementvoid
setAsObject
(String key, Object value) Sets a new value to map element specified by its index.toString()
Gets a string representation of the object.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
-
StringValueMap
public StringValueMap() -
StringValueMap
Creates a new instance of the map and assigns its value.- Parameters:
map
- (optional) values to initialize this map.
-
-
Method Details
-
get
Gets a map element specified by its key.- Parameters:
key
- a key of the element to get.- Returns:
- the value of the map element.
-
getKeys
Gets keys of all elements stored in this map.- Returns:
- a list with all map keys.
-
put
Puts a new value into map element specified by its key.- Parameters:
key
- a key of the element to put.value
- a new value for map element.
-
append
Appends new elements to this map.- Parameters:
map
- a map with elements to be added.
-
length
public int length()Gets a number of elements stored in this map.- Returns:
- the number of elements in this map.
-
getAsObject
Gets the value stored in this map element without any conversions- Returns:
- the value of the map element.
-
setAsObject
Sets a new value for this array element- Parameters:
value
- the new object value.
-
getAsObject
Gets the value stored in map element without any conversions. When element key is not defined it returns the entire map value.- Parameters:
key
- (optional) a key of the element to get- Returns:
- the element value or value of the map when index is not defined.
-
setAsObject
Sets a new value to map element specified by its index. When the index is not defined, it resets the entire map value. This method has double purpose because method overrides are not supported in JavaScript.- Parameters:
key
- (optional) a key of the element to setvalue
- a new element or map value.
-
getAsNullableString
Converts map element into a string or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- string value of the element or null if conversion is not supported.
- See Also:
-
getAsString
Converts map element into a string or returns "" if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- string value of the element or "" if conversion is not supported.
- See Also:
-
getAsStringWithDefault
Converts map element into a string or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- string value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableBoolean
Converts map element into a boolean or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- boolean value of the element or null if conversion is not supported.
- See Also:
-
getAsBoolean
Converts map element into a boolean or returns false if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- boolean value of the element or false if conversion is not supported.
- See Also:
-
getAsBooleanWithDefault
Converts map element into a boolean or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- boolean value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableInteger
Converts map element into an integer or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- integer value of the element or null if conversion is not supported.
- See Also:
-
getAsInteger
Converts map element into an integer or returns 0 if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- integer value of the element or 0 if conversion is not supported.
- See Also:
-
getAsIntegerWithDefault
Converts map element into an integer or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- integer value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableLong
Converts map element into a long or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- long value of the element or null if conversion is not supported.
- See Also:
-
getAsLong
Converts map element into a long or returns 0 if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- long value of the element or 0 if conversion is not supported.
- See Also:
-
getAsLongWithDefault
Converts map element into a long or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- long value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableFloat
Converts map element into a float or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- float value of the element or null if conversion is not supported.
- See Also:
-
getAsFloat
Converts map element into a float or returns 0 if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- float value of the element or 0 if conversion is not supported.
- See Also:
-
getAsFloatWithDefault
Converts map element into a float or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- float value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableDouble
Converts map element into a double or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- double value of the element or null if conversion is not supported.
- See Also:
-
getAsDouble
Converts map element into a double or returns 0 if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- double value of the element or 0 if conversion is not supported.
- See Also:
-
getAsDoubleWithDefault
Converts map element into a double or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- double value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableDateTime
Converts map element into a Date or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- ZonedDateTime value of the element or null if conversion is not supported.
- See Also:
-
getAsDateTime
Converts map element into a Date or returns the current date if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- ZonedDateTime value of the element or the current date if conversion is not supported.
- See Also:
-
getAsDateTimeWithDefault
Converts map element into a Date or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- ZonedDateTime value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableDuration
-
getAsDuration
-
getAsDurationWithDefault
-
getAsNullableEnum
-
getAsEnum
-
getAsEnumWithDefault
-
getAsNullableType
Converts map element into a value defined by specied typecode. If conversion is not possible it returns null.- Parameters:
type
- the Class type that defined the type of the resultkey
- a key of element to get.- Returns:
- element value defined by the typecode or null if conversion is not supported.
- See Also:
-
getAsType
Converts map element into a value defined by specied typecode. If conversion is not possible it returns default value for the specified type.- Parameters:
type
- the Class type that defined the type of the resultkey
- a key of element to get.- Returns:
- element value defined by the typecode or default if conversion is not supported.
- See Also:
-
getAsTypeWithDefault
Converts map element into a value defined by specied typecode. If conversion is not possible it returns default value.- Parameters:
type
- the Class type that defined the type of the resultkey
- a key of element to get.defaultValue
- the default value- Returns:
- element value defined by the typecode or default value if conversion is not supported.
- See Also:
-
getAsValue
Converts map element into an AnyValue or returns an empty AnyValue if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- AnyValue value of the element or empty AnyValue if conversion is not supported.
- See Also:
-
getAsNullableArray
Converts map element into an AnyValueArray or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- AnyValueArray value of the element or null if conversion is not supported.
- See Also:
-
getAsArray
Converts map element into an AnyValueArray or returns empty AnyValueArray if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- AnyValueArray value of the element or empty AnyValueArray if conversion is not supported.
- See Also:
-
getAsArrayWithDefault
Converts map element into an AnyValueArray or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- AnyValueArray value of the element or default value if conversion is not supported.
- See Also:
-
getAsNullableMap
Converts map element into an AnyValueMap or returns null if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- AnyValueMap value of the element or null if conversion is not supported.
- See Also:
-
getAsMap
Converts map element into an AnyValueMap or returns empty AnyValueMap if conversion is not possible.- Parameters:
key
- a key of element to get.- Returns:
- AnyValueMap value of the element or empty AnyValueMap if conversion is not supported.
- See Also:
-
getAsMapWithDefault
Converts map element into an AnyValueMap or returns default value if conversion is not possible.- Parameters:
key
- a key of element to get.defaultValue
- the default value- Returns:
- AnyValueMap value of the element or default value if conversion is not supported.
- See Also:
-
toString
Gets a string representation of the object. The result is a semicolon-separated list of key-value pairs as "key1=value1;key2=value2;key=value3"- Overrides:
toString
in classAbstractMap<String,
String> - Returns:
- a string representation of the object.
-
clone
Creates a binary clone of this object. -
fromTuples
Creates a new StringValueMap from a list of key-value pairs called tuples.- Parameters:
tuples
- a list of values where odd elements are keys and the following even elements are values- Returns:
- a newly created StringValueMap.
- See Also:
-
fromTuplesArray
Creates a new StringValueMap from a list of key-value pairs called tuples. The method is similar to [[fromTuples]] but tuples are passed as array instead of parameters.- Parameters:
tuples
- a list of values where odd elements are keys and the following even elements are values- Returns:
- a newly created StringValueMap.
-
fromString
Parses semicolon-separated key-value pairs and returns them as a StringValueMap.- Parameters:
line
- semicolon-separated key-value list to initialize StringValueMap.- Returns:
- a newly created StringValueMap.
-
fromMaps
Creates a new AnyValueMap by merging two or more maps. Maps defined later in the list override values from previously defined maps.- Parameters:
maps
- an array of maps to be merged- Returns:
- a newly created AnyValueMap.
-