Class AliasHashMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,List<V>>
org.daiitech.naftah.builtin.utils.AliasHashMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Serializable, Cloneable, Map<K,List<V>>

public class AliasHashMap<K,V> extends HashMap<K,List<V>>
A specialized HashMap that supports aliasing of keys.

This map allows associating multiple alias keys with a single canonical key, enabling any alias to retrieve the value associated with the canonical key.

Example usage:


 AliasHashMap<String, String> map = new AliasHashMap<>();
 map.put("NA", "Naftah", "NFTH", "Naftah");

 map.get("NA");      // returns "Naftah"
 map.get("NFTH");    // returns "Naftah"
 map.get("Naftah");  // returns "Naftah"
 
Author:
Chakib Daii
See Also:
  • Field Details

    • aliasToKeysMap

      private final Map<K,Set<K>> aliasToKeysMap
      Internal map storing alias → canonical key relationships.
  • Constructor Details

    • AliasHashMap

      public AliasHashMap()
  • Method Details

    • toAliasGroupedByName

      Returns a Collector that groups BuiltinFunction instances by their canonical function name, storing the results in an AliasHashMap where:
      • The key is the canonical function name as returned by getFunctionInfo().name().
      • The value is a List of BuiltinFunction instances sharing that name.
      • Each function's declared aliases (getFunctionInfo().aliases()) are registered in the resulting map, allowing lookup by alias as well.

      This collector can be used to efficiently index built-in functions by their primary name and aliases, allowing flexible name-based lookup in environments such as the Naftah runtime.

      The resulting AliasHashMap supports retrieval via both canonical names and aliases.

      Example usage:

      
       AliasHashMap<String, List<BuiltinFunction>> functionsByName =
           getBuiltinMethods(Builtin.class)
               .stream()
               .collect(toAliasGroupedByName());
       
      Returns:
      a collector that groups BuiltinFunctions by canonical name and registers their aliases
      See Also:
    • fillAliasToKeyMap

      public static <K, V> void fillAliasToKeyMap(AliasHashMap<K,V> map, K alias, K canonicalKey, BuiltinFunction fn)
      Fills the alias-to-key mapping for a given AliasHashMap.

      This method registers a mapping from a given alias to its canonical key. If the alias already exists in the map, a NaftahBugError will be thrown to prevent alias overriding.

      Function aliases are intended to be immutable and globally unique. Attempting to override or redefine an existing alias is considered a bug in the builtin function provider.

      Type Parameters:
      K - the type of keys (aliases and canonical keys)
      V - the type of mapped values
      Parameters:
      map - the AliasHashMap to update
      alias - the alias key to register
      canonicalKey - the canonical key associated with the alias
      fn - the BuiltinFunction used for error reporting
    • put

      @SafeVarargs public final void put(K canonicalKey, V value, K... aliases)
      Associates the specified value with the specified canonical key, and registers any number of alias keys that map to the same value.

      If the canonical key already exists in the map, it will be overwritten. Aliases will always overwrite previous alias mappings.

      Parameters:
      canonicalKey - The main key to associate with the value.
      value - The value to be associated with the canonical key.
      aliases - Optional alias keys that should also retrieve the same value.
    • putAll

      public void putAll(Map<? extends K,? extends List<V>> map)
      Copies all mappings from the specified map into this map.

      This method behaves like HashMap.putAll(Map) for regular entries, but also preserves alias relationships when the source map is an instance of AliasHashMap.

      When copying from another AliasHashMap, all alias-to-canonical-key mappings from the source are merged into this map. If any alias already exists in this map, a NaftahBugError will be thrown to prevent alias overriding.

      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class HashMap<K,List<V>>
      Parameters:
      map - The map whose mappings are to be copied into this map.
      Throws:
      NaftahBugError - if an alias conflict occurs during merging.
      See Also:
    • get

      public List<V> get(Object key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key or its aliases.

      Checks canonical keys first, then aliases.

      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class HashMap<K,List<V>>
      Parameters:
      key - The key (or alias) whose associated value is to be returned.
      Returns:
      a list of associated values, or null if none exists.
    • containsKey

      public boolean containsKey(Object key)
      Returns true if this map contains a mapping for the specified key or any of its aliases.
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class HashMap<K,List<V>>
      Parameters:
      key - The key (or alias) whose presence in this map is to be tested.
      Returns:
      true if the map contains the key or any of its aliases.