Class AssocArray

java.lang.Object
org.metricshub.jawk.jrt.AssocArray
All Implemented Interfaces:
Comparator<Object>, Map<Object,Object>

public class AssocArray extends Object implements Comparator<Object>, Map<Object,Object>
An AWK associative array.

The implementation requires the ability to choose, at runtime, whether the keys are to be maintained in sorted order or not. Therefore, the implementation contains a reference to a Map (either TreeMap or HashMap, depending on whether to maintain keys in sorted order or not) and delegates calls to it accordingly.

Author:
Danny Daglas
  • Field Details

    • MT_HASH

      public static final int MT_HASH
      The parameter to useMapType to convert this associative array to a HashMap.
      See Also:
    • MT_LINKED

      public static final int MT_LINKED
      The parameter to useMapType to convert this associative array to a LinkedHashMap.
      See Also:
    • MT_TREE

      public static final int MT_TREE
      The parameter to useMapType to convert this associative array to a TreeMap.
      See Also:
  • Constructor Details

    • AssocArray

      public AssocArray(boolean sortedArrayKeys)

      Constructor for AssocArray.

      Parameters:
      sortedArrayKeys - Whether keys must be kept sorted
  • Method Details

    • useMapType

      public void useMapType(int mapType)
      Convert the map which backs this associative array into one of HashMap, LinkedHashMap, or TreeMap.
      Parameters:
      mapType - Can be one of MT_HASH, MT_LINKED, or MT_TREE.
    • mapString

      public String mapString()
      Provide a string representation of the delegated map object.
      Returns:
      string representing the map/array
    • isIn

      public boolean isIn(Object key)

      isIn.

      Parameters:
      key - Key to be checked
      Returns:
      whether a particular key is contained within the associative array. Unlike get(), which adds a blank (null) reference to the associative array if the element is not found, isIn will not. It exists to support the IN keyword.
    • get

      public Object get(Object key)

      get.

      Specified by:
      get in interface Map<Object,Object>
      Parameters:
      key - Key to retrieve in the array
      Returns:
      the value of an associative array element given a particular key. If the key does not exist, a null value (blank string) is inserted into the array with this key, and the null value is returned.
    • put

      public Object put(Object key, Object value)
      Added to support insertion of primitive key types.
      Specified by:
      put in interface Map<Object,Object>
      Parameters:
      key - Key of the entry to put in the array
      value - Value of the key
      Returns:
      the previous value of the specified key, or null if key didn't exist
    • put

      public Object put(long key, Object value)
      Added to support insertion of primitive key types.
      Parameters:
      key - Index of the entry to put in the array
      value - Value of the key
      Returns:
      the previous value of the specified key, or null if key didn't exist
    • keySet

      public Set<Object> keySet()

      keySet.

      Specified by:
      keySet in interface Map<Object,Object>
      Returns:
      the set of keys
    • clear

      public void clear()
      Clear the array
      Specified by:
      clear in interface Map<Object,Object>
    • remove

      public Object remove(Object key)
      Delete the specified entry
      Specified by:
      remove in interface Map<Object,Object>
      Parameters:
      key - Key of the entry to remove from the array
      Returns:
      the value of the entry before it was removed
    • toString

      public String toString()
      Do nothing. Should not be called in this state.
      Overrides:
      toString in class Object
    • compare

      public int compare(Object o1, Object o2)
      Comparator implementation used by the TreeMap when keys are to be maintained in sorted order.
      Specified by:
      compare in interface Comparator<Object>
    • getMapVersion

      public String getMapVersion()

      getMapVersion.

      Returns:
      the specification version of this class
    • size

      public int size()
      Specified by:
      size in interface Map<Object,Object>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<Object,Object>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<Object,Object>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<Object,Object>
    • putAll

      public void putAll(Map<? extends Object,? extends Object> m)
      Specified by:
      putAll in interface Map<Object,Object>
    • values

      public Collection<Object> values()
      Specified by:
      values in interface Map<Object,Object>
    • entrySet

      public Set<Map.Entry<Object,Object>> entrySet()
      Specified by:
      entrySet in interface Map<Object,Object>