Class HierarchyClassMap<V>
- Type Parameters:
V- the generic type of the mapped values
Map which keys are instances of Class and values are instances of generic type
V.
This classes' value comes from its get(Object) and containsKey(Object) methods being redefined to support
hierarchical lookups. For example, consider types Dog and RabidDog, such that RabidDog extends
Dog. If the map contains both keys, then the behaviour is the same as in a standard map. However, if
get(Object) is invoked the key Dog but only RabidDog is mapped, this implementation will still find a
value for Dog, since it will recursively traverse across all entries looking for keys which are assignable from that
key. When no value is found, then the search is retried using the key's superclass. Recursion ends when the Object
class is reached or when no superclass is available (if you searched for Object in the first place) A consistent behaviour will
occur when using the containsKey(Object) method.
This also works when the key is an interface, with the difference that no recursion is performed in that case.
Interfaces
If an interface key is used and no explicit mapping is defined, no special behaviour is used.Ordering considerations
- Exact matches are always privileged. Meaning that if the
Dogkey has a specific value mapped, it will be preferred over aRabidDogkey - This implementation works by wrapping an existing map (look at the constructors Javadocs). When performing searches, the iteration order depends on the rules of the underlying match. When multiple entries could match a specific query, no guarantees are offered regarding which value will be returned on each invokation
Performance considerations
When there's an explicit mapping for a given key, the performance is the same as in the backing map. When a deep search occurs, then the performance drops to O(n^n) (worst case).Other methods
Other thanget(Object) and containsKey(Object), no other method has been overridden.
Behaviour will be that of the backing map.- Since:
- 1.0
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance which behaves like aHashMapHierarchyClassMap(Map<Class<?>, V> delegate) Creates a new instance which wraps the givendelegate, inheriting its rules. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()computeIfAbsent(Class<?> key, Function<? super Class<?>, ? extends V> mappingFunction) computeIfPresent(Class<?> key, BiFunction<? super Class<?>, ? super V, ? extends V> remappingFunction) booleancontainsKey(Object key) Determines if there's a matching mapping by the rules described on the class javadoc.booleancontainsValue(Object value) entrySet()booleanvoidforEach(BiConsumer<? super Class<?>, ? super V> action) Fetches a value associated to the givenkeyfollowing the rules described on the class javadocgetOrDefault(Object key, V defaultValue) inthashCode()booleanisEmpty()keySet()voidputIfAbsent(Class<?> key, V value) booleanbooleanvoidreplaceAll(BiFunction<? super Class<?>, ? super V, ? extends V> function) intsize()values()
-
Constructor Details
-
Method Details
-
get
Fetches a value associated to the givenkeyfollowing the rules described on the class javadoc -
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
Determines if there's a matching mapping by the rules described on the class javadoc.- Specified by:
containsKeyin interfaceMap<Class<?>,V> - Parameters:
key- the considered key- Returns:
- whether there's a mapping for this key or not
-
containsValue
- Specified by:
containsValuein interfaceMap<Class<?>,V>
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
equals
-
hashCode
public int hashCode() -
getOrDefault
- Specified by:
getOrDefaultin interfaceMap<Class<?>,V>
-
forEach
-
replaceAll
- Specified by:
replaceAllin interfaceMap<Class<?>,V>
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceMap<Class<?>,V>
-
remove
-
replace
-
replace
-
computeIfAbsent
- Specified by:
computeIfAbsentin interfaceMap<Class<?>,V>
-
computeIfPresent
public V computeIfPresent(Class<?> key, BiFunction<? super Class<?>, ? super V, ? extends V> remappingFunction) - Specified by:
computeIfPresentin interfaceMap<Class<?>,V>
-
compute
-
merge
-