Class Maps


  • public class Maps
    extends java.lang.Object
    Utilities to work with maps.

    Author:
    Javier Paniza
    • Constructor Summary

      Constructors 
      Constructor Description
      Maps()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map clone​(java.util.Map m)
      Create a clone.
      static java.lang.Object getKeyFromValue​(java.util.Map map, java.lang.Object value)  
      static java.lang.Object getKeyFromValue​(java.util.Map map, java.lang.Object value, java.lang.Object defaultValue)  
      static java.lang.Object getValueFromQualifiedName​(java.util.Map tree, java.lang.String qualifiedName)
      Obtain a value in a map with nested maps from a qualified name.
      static boolean isEmpty​(java.util.Map values)
      It's empty if is null, without elements, with null elements or elements with neutral value (empty strings, collections, nulls).
      static boolean isEmptyOrZero​(java.util.Map values)
      It's empty if is null, without elements, with null elements or elements with neutral value (empty strings, collections, nulls or zeroes).
      static java.util.Map plainToTree​(java.util.Map plainMap)
      Converts a plain map (without levels) in a tree map.
      static void putValueFromQualifiedName​(java.util.Map tree, java.lang.String qualifiedName, java.lang.Object value)
      Put a value in a map with nested maps from a qualified name.
      static java.util.Map qualifiedKeysCollectionToTreeMapWithNullValues​(java.util.Collection collection)
      Converts a collection with qualified keys in a tree map with null values.
      static java.util.Map recursiveClone​(java.util.Map origin)
      Does a recursive clone of map.
      static java.util.Map recursiveCloneWithCollections​(java.util.Map origin)
      Does a recursive clone of map, cloning collections too.
      static java.util.Map toMap​(java.lang.Object... values)
      Converts the objects sent into a map.
      static java.util.Map treeToPlain​(java.util.Map treeMap)
      Converts a tree map in a plain map (without levels).
      static java.util.Map treeToPlainIncludingCollections​(java.util.Map treeMap)  
      static java.util.Map treeToPlainIncludingCollections​(java.util.Map treeMap, int baseIndex)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Maps

        public Maps()
    • Method Detail

      • clone

        public static java.util.Map clone​(java.util.Map m)
        Create a clone.

        Tries to make a clone using the clone() method of the origin if this is Cloneable, otherwise do a manual clon of the map, without clone the elements (it's to say, it does not make a deep clone).

        Parameters:
        m - Original map to clone. Has to be Cloneable or has a default contructor and implementing putAll method.
        Returns:
        Can be null (if the original is null), and the same type of original.
        Throws:
        java.lang.IllegalArgumentException - If precondition is broken
      • getValueFromQualifiedName

        public static java.lang.Object getValueFromQualifiedName​(java.util.Map tree,
                                                                 java.lang.String qualifiedName)
        Obtain a value in a map with nested maps from a qualified name. For example: (((Map) mymap.get("a")).get("b")).get("c") is equal to: Maps.getValueFromQualifiedName(mymap, "a.b.c")
        Parameters:
        tree - Map with map in some values, hence in tree-form.
        qualifiedName - Name in form a.b.c.
      • putValueFromQualifiedName

        public static void putValueFromQualifiedName​(java.util.Map tree,
                                                     java.lang.String qualifiedName,
                                                     java.lang.Object value)
        Put a value in a map with nested maps from a qualified name. For example: (((Map) mymap.get("a")).get("b")).put("c", value) is equal to: Maps.putValueFromQualifiedName(mymap, "a.b.c", value)
        Parameters:
        tree - Map with map in some values, hence in tree-form.
        qualifiedName - Name in form a.b.c.
        value - Value to put
      • recursiveClone

        public static java.util.Map recursiveClone​(java.util.Map origin)
        Does a recursive clone of map.

        A recursive clone means that if some value is a map itself this method is applied to it.

        Parameters:
        origin - Cannot be null.
        Returns:
        instanceof origen.getClass()
      • recursiveCloneWithCollections

        public static java.util.Map recursiveCloneWithCollections​(java.util.Map origin)
        Does a recursive clone of map, cloning collections too.

        A recursive clone means that if some value is a map itself this method is applied to it. If some value is a collection the collection is duplicated.

        Parameters:
        origin - Cannot be null.
        Returns:
        instanceof origen.getClass() or HashMap() if not possible
        Since:
        5.9
      • isEmpty

        public static boolean isEmpty​(java.util.Map values)
        It's empty if is null, without elements, with null elements or elements with neutral value (empty strings, collections, nulls).

        Numeric values with value 0 not are considered empty.

        Parameters:
        values - Can be null.
      • isEmptyOrZero

        public static boolean isEmptyOrZero​(java.util.Map values)
        It's empty if is null, without elements, with null elements or elements with neutral value (empty strings, collections, nulls or zeroes).

        Numeric values with value 0 are considered empty.

        Parameters:
        values - Can be null.
      • plainToTree

        public static java.util.Map plainToTree​(java.util.Map plainMap)
        Converts a plain map (without levels) in a tree map.

        That is, convert:

         {invoice.year=2006, invoice.number=1, number=3}
         
        in
         {invoice={year=2006, number=1}, number=3}
         
        Parameters:
        plainMap - This argument is not changed. The keys must be strings. Mustn't be null
        Returns:
        A map with the data in tree format.
      • qualifiedKeysCollectionToTreeMapWithNullValues

        public static java.util.Map qualifiedKeysCollectionToTreeMapWithNullValues​(java.util.Collection collection)
        Converts a collection with qualified keys in a tree map with null values.

        That is, convert:

         [invoice.year, invoice.number, number]
         
        in
         {invoice={year=null, number=null}, number=null}
         
        Parameters:
        collection - This argument is not changed. The values must be strings. Mustn't be null
        Returns:
        A map with the data in tree format.
        Since:
        6.6.1
      • treeToPlain

        public static java.util.Map treeToPlain​(java.util.Map treeMap)
        Converts a tree map in a plain map (without levels).

        That is, convert:

         {invoice={year=2006, number=1}, number=3}
         
        in
         {invoice.year=2006, invoice.number=1, number=3}
         
        Parameters:
        treeMap - This argument is not changed. The keys must be strings. Mustn't be null
        Returns:
        A map with the data in plain format.
      • treeToPlainIncludingCollections

        public static java.util.Map treeToPlainIncludingCollections​(java.util.Map treeMap)
        Since:
        5.9
      • treeToPlainIncludingCollections

        public static java.util.Map treeToPlainIncludingCollections​(java.util.Map treeMap,
                                                                    int baseIndex)
        Since:
        5.9
      • toMap

        public static java.util.Map toMap​(java.lang.Object... values)
        Converts the objects sent into a map.

        Useful to initialize maps:

         Map scores = Maps.toMap("Manolo", 7, "Angela", 10);
         
        Parameters:
        Vararg - with key, value, key, value, etc
        Since:
        5.7
      • getKeyFromValue

        public static java.lang.Object getKeyFromValue​(java.util.Map map,
                                                       java.lang.Object value,
                                                       java.lang.Object defaultValue)
        Since:
        5.9
      • getKeyFromValue

        public static java.lang.Object getKeyFromValue​(java.util.Map map,
                                                       java.lang.Object value)
        Since:
        5.9