Class AnyValueMap

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

public class AnyValueMap extends HashMap<String,Object> implements Cloneable
Cross-language implementation of dynamic object map (dictionary) what can hold values of any type. The stored values can be converted to different types using variety of accessor methods.

### Example ###

 
 AnyValueMap value1 = new AnyValueMap(new HashMap<String, Object>{
 { put("key1", 1);
 	 put("key2", "123.456"); 
   put("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

    • AnyValueMap

      public AnyValueMap()
    • AnyValueMap

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

    • get

      public Object 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.
    • append

      public void append(Map<?,?> map)
      Appends new elements to this map.
      Parameters:
      map - a map with elements to be added.
    • 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.
    • 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(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 flot or returns default value if conversion is not possible.
      Parameters:
      key - a key of element to get.
      defaultValue - the default value
      Returns:
      flot 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,Object>
      Returns:
      a string representation of the object.
    • clone

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

      public static AnyValueMap fromValue(Object value)
      Converts specified value into AnyValueMap.
      Parameters:
      value - value to be converted
      Returns:
      a newly created AnyValueMap.
      See Also:
    • fromTuples

      public static AnyValueMap fromTuples(Object... tuples)
      Creates a new AnyValueMap 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 AnyValueArray.
      See Also:
    • fromTuplesArray

      public static AnyValueMap fromTuplesArray(Object[] tuples)
      Creates a new AnyValueMap 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 AnyValueArray.
    • fromMaps

      public static AnyValueMap 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.