Class AliasHashMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Serializable,Cloneable,Map<K,List<V>>
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
FieldsModifier and TypeFieldDescriptionInternal map storing alias → canonical key relationships. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(Object key) Returnstrueif this map contains a mapping for the specified key or any of its aliases.static <K,V> void fillAliasToKeyMap(AliasHashMap<K, V> map, K alias, K canonicalKey, BuiltinFunction fn) Fills the alias-to-key mapping for a givenAliasHashMap.Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key or its aliases.final voidAssociates the specified value with the specified canonical key, and registers any number of alias keys that map to the same value.voidCopies all mappings from the specified map into this map.static Collector<BuiltinFunction,AliasHashMap<String, BuiltinFunction>, AliasHashMap<String, BuiltinFunction>> Returns aCollectorthat groupsBuiltinFunctioninstances by their canonical function name, storing the results in anAliasHashMapwhere: The key is the canonical function name as returned bygetFunctionInfo().name(). The value is aListofBuiltinFunctioninstances sharing that name. Each function's declared aliases (getFunctionInfo().aliases()) are registered in the resulting map, allowing lookup by alias as well.Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, put, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
Field Details
-
aliasToKeysMap
Internal map storing alias → canonical key relationships.
-
-
Constructor Details
-
AliasHashMap
public AliasHashMap()
-
-
Method Details
-
toAliasGroupedByName
public static Collector<BuiltinFunction,AliasHashMap<String, toAliasGroupedByName()BuiltinFunction>, AliasHashMap<String, BuiltinFunction>> Returns aCollectorthat groupsBuiltinFunctioninstances by their canonical function name, storing the results in anAliasHashMapwhere:- The key is the canonical function name as returned by
getFunctionInfo().name(). - The value is a
ListofBuiltinFunctioninstances 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
AliasHashMapsupports 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:
- The key is the canonical function name as returned by
-
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 givenAliasHashMap.This method registers a mapping from a given alias to its canonical key. If the alias already exists in the map, a
NaftahBugErrorwill 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- theAliasHashMapto updatealias- the alias key to registercanonicalKey- the canonical key associated with the aliasfn- theBuiltinFunctionused for error reporting
-
put
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
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 ofAliasHashMap.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, aNaftahBugErrorwill be thrown to prevent alias overriding. -
get
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key or its aliases.Checks canonical keys first, then aliases.
-
containsKey
Returnstrueif this map contains a mapping for the specified key or any of its aliases.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classHashMap<K,List<V>> - Parameters:
key- The key (or alias) whose presence in this map is to be tested.- Returns:
trueif the map contains the key or any of its aliases.
-