Interface AssocArray
- All Known Implementing Classes:
HashAssocArray,ListAssocArray,SortedAssocArray
This interface extends Map and provides AWK-specific behaviour:
automatic key normalization (null and uninitialized values map to ""),
numeric key coercion ("1" and 1L address the same slot), and
auto-creation of blank entries on first access.
Concrete implementations directly extend a JDK Map class to avoid
delegation overhead:
HashAssocArray— backed byHashMapListAssocArray— materialized from aListand backed byHashMapSortedAssocArray— backed byTreeMapwith AWK key ordering
Use the factory methods to create instances:
AssocArray hash = AssocArray.createHash(); AssocArray sorted = AssocArray.createSorted(); AssocArray list = AssocArray.createFromList(values, sortedArrayKeys); AssocArray aa = AssocArray.create(sortedArrayKeys);
- Author:
- Danny Daglas
-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final UninitializedObjectA blank (uninitialized) value shared across all AWK array accesses. -
Method Summary
Modifier and TypeMethodDescriptionstatic AssocArraycreate(boolean sortedArrayKeys) Creates a new associative array of the appropriate type.static AssocArraycreateFromList(List<?> values, boolean sortedArrayKeys) Creates a new associative array materialized from a JavaList.static AssocArrayCreates a new hash-based associative array (backed byHashMap).static AssocArrayCreates a new sorted associative array (backed byTreeMapwith AWK key ordering).default StringReturns the specification version of the underlying JDKMapclass that backs this implementation.default booleanReturns whether a particular key is contained within the associative array.default StringProvides a string representation of this associative array, recursively rendering nested arrays.static ObjectnormalizeKey(Object key) Converts a key to the canonical form expected by AWK:nullandUninitializedObjectmap to the empty string.static ObjectnormalizeValue(Object value, boolean sortedArrayKeys) Normalizes an externally supplied structured value before the AVM stores it.default ObjectStores a value using a primitivelongkey, bypassing string parsing.static LongAttempts to parse the key as aLong.Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Field Details
-
BLANK
A blank (uninitialized) value shared across all AWK array accesses.
-
-
Method Details
-
normalizeKey
Converts a key to the canonical form expected by AWK:nullandUninitializedObjectmap to the empty string.- Parameters:
key- the raw key- Returns:
- the normalized key, never
null
-
toLongKey
Attempts to parse the key as aLong.- Parameters:
key- the key to parse (must not benull)- Returns:
- the
Longvalue, ornullif the key cannot be parsed as a long integer
-
isIn
Returns whether a particular key is contained within the associative array.Unlike
Map.get(Object), which auto-creates a blank entry when the key is absent, this method does not modify the array. It exists to support the AWKINkeyword.- Parameters:
key- Key to be checked- Returns:
trueif the key (or its numeric equivalent) is present
-
mapString
Provides a string representation of this associative array, recursively rendering nested arrays.- Returns:
- a human-readable map string of the form
{key=value, ...}
-
put
Stores a value using a primitivelongkey, bypassing string parsing.This is a convenience overload for callers that already hold a
longkey. The default implementation boxes the key and delegates toMap.put(Object, Object).- Parameters:
key- the long keyvalue- the value to associate with the key- Returns:
- the previous value associated with the key, or
null
-
getMapVersion
Returns the specification version of the underlying JDKMapclass that backs this implementation.- Returns:
- the specification version string, or
nullif unavailable
-
createHash
Creates a new hash-based associative array (backed byHashMap).- Returns:
- a new
HashAssocArray
-
createSorted
Creates a new sorted associative array (backed byTreeMapwith AWK key ordering).- Returns:
- a new
SortedAssocArray
-
create
Creates a new associative array of the appropriate type.- Parameters:
sortedArrayKeys-trueto create a sorted (tree-backed) array,falsefor a hash-backed array- Returns:
- a new
AssocArrayinstance
-
createFromList
Creates a new associative array materialized from a JavaList.List elements are stored under zero-based
Longkeys. NestedListvalues are recursively materialized as associative arrays so JSON-like object trees can be traversed with AWK array syntax.- Parameters:
values- list values to expose as an AWK arraysortedArrayKeys-trueto create a sorted array,falsefor a hash-backed array- Returns:
- a new
AssocArraycontaining the list values
-
normalizeValue
Normalizes an externally supplied structured value before the AVM stores it.Listvalues are converted toAssocArrayinstances.Mapvalues are kept in place to preserve direct-map performance, but their nested list values are recursively converted.- Parameters:
value- value to normalizesortedArrayKeys-truewhen converted lists should use sorted array keys- Returns:
- the normalized value
-