Class AssocArray

java.lang.Object
org.sentrysoftware.jawk.jrt.AssocArray
All Implemented Interfaces:
Comparator<Object>

public class AssocArray extends Object implements Comparator<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. It exists to support the _DUMP keyword.
      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.

      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.
      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.

      Returns:
      the set of keys
    • clear

      public void clear()
      Clear the array
    • remove

      public Object remove(Object key)
      Delete the specified entry
      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