Class StringValueMap

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<String,String>
org.pipservices3.commons.data.StringValueMap
All Implemented Interfaces:
Serializable, Cloneable, Map<String,String>
Direct Known Subclasses:
ConfigParams, FilterParams

public class StringValueMap extends HashMap<String,String> implements Cloneable
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)
 
 
See Also:
  • Constructor Details

    • StringValueMap

      public StringValueMap()
    • StringValueMap

      public StringValueMap(Map<?,?> map)
      Creates a new instance of the map and assigns its value.
      Parameters:
      map - (optional) values to initialize this map.
  • Method Details

    • get

      public String get(String key)
      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

      public List<String> getKeys()
      Gets keys of all elements stored in this map.
      Returns:
      a list with all map keys.
    • put

      public void put(String key, Object value)
      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

      public void append(Map<?,?> map)
      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

      public Object getAsObject()
      Gets the value stored in this map element without any conversions
      Returns:
      the value of the map element.
    • setAsObject

      public void setAsObject(Object value)
      Sets a new value for this array element
      Parameters:
      value - the new object value.
    • getAsObject

      public Object getAsObject(String key)
      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

      public void setAsObject(String key, Object value)
      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 set
      value - a new element or map value.
    • getAsNullableString

      public String getAsNullableString(String key)
      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

      public String getAsString(String key)
      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

      public String getAsStringWithDefault(String key, String defaultValue)
      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

      public Boolean getAsNullableBoolean(String key)
      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

      public boolean getAsBoolean(String key)
      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

      public boolean getAsBooleanWithDefault(String key, boolean defaultValue)
      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

      public Integer getAsNullableInteger(String key)
      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

      public int getAsInteger(String key)
      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

      public int getAsIntegerWithDefault(String key, int defaultValue)
      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

      public Long getAsNullableLong(String key)
      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

      public long getAsLong(String key)
      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

      public long getAsLongWithDefault(String key, long defaultValue)
      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

      public Float getAsNullableFloat(String key)
      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

      public float getAsFloat(String key)
      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

      public float getAsFloatWithDefault(String key, float defaultValue)
      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

      public Double getAsNullableDouble(String key)
      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

      public double getAsDouble(String key)
      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

      public double getAsDoubleWithDefault(String key, double defaultValue)
      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

      public ZonedDateTime getAsNullableDateTime(String key)
      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

      public ZonedDateTime getAsDateTime(String key)
      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

      public ZonedDateTime getAsDateTimeWithDefault(String key, ZonedDateTime defaultValue)
      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

      public Duration getAsNullableDuration(String key)
    • getAsDuration

      public Duration getAsDuration(String key)
    • getAsDurationWithDefault

      public Duration getAsDurationWithDefault(String key, Duration defaultValue)
    • getAsNullableEnum

      public <T extends Enum<T>> T getAsNullableEnum(Class<T> type, String key)
    • getAsEnum

      public <T extends Enum<T>> T getAsEnum(Class<T> type, String key)
    • getAsEnumWithDefault

      public <T extends Enum<T>> T getAsEnumWithDefault(Class<T> type, String key, T defaultValue)
    • getAsNullableType

      public <T> T getAsNullableType(Class<T> type, String key)
      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 result
      key - a key of element to get.
      Returns:
      element value defined by the typecode or null if conversion is not supported.
      See Also:
    • getAsType

      public <T> T getAsType(Class<T> type, String key)
      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 result
      key - a key of element to get.
      Returns:
      element value defined by the typecode or default if conversion is not supported.
      See Also:
    • getAsTypeWithDefault

      public <T> T getAsTypeWithDefault(Class<T> type, String key, T defaultValue)
      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 result
      key - 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

      public AnyValue getAsValue(String key)
      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

      public AnyValueArray getAsNullableArray(String key)
      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

      public AnyValueArray getAsArray(String key)
      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

      public AnyValueArray getAsArrayWithDefault(String key, AnyValueArray defaultValue)
      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

      public AnyValueMap getAsNullableMap(String key)
      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

      public AnyValueMap getAsMap(String key)
      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

      public AnyValueMap getAsMapWithDefault(String key, AnyValueMap defaultValue)
      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

      public String 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 class AbstractMap<String,String>
      Returns:
      a string representation of the object.
    • clone

      public StringValueMap clone()
      Creates a binary clone of this object.
      Overrides:
      clone in class HashMap<String,String>
      Returns:
      a clone of this object.
    • fromTuples

      public static StringValueMap fromTuples(Object... tuples)
      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

      public static StringValueMap fromTuplesArray(Object[] tuples)
      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

      public static StringValueMap fromString(String line)
      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

      public static StringValueMap fromMaps(Map<?,?>... maps)
      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.