Class HierarchyClassMap<V>

java.lang.Object
org.mule.runtime.extension.api.util.HierarchyClassMap<V>
Type Parameters:
V - the generic type of the mapped values
All Implemented Interfaces:
Map<Class<?>,V>

public class HierarchyClassMap<V> extends Object implements Map<Class<?>,V>
A utility implementation of 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 Dog key has a specific value mapped, it will be preferred over a RabidDog key
  • 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 than get(Object) and containsKey(Object), no other method has been overridden. Behaviour will be that of the backing map.
Since:
1.0
  • Constructor Details

    • HierarchyClassMap

      public HierarchyClassMap()
      Creates a new instance which behaves like a HashMap
    • HierarchyClassMap

      public HierarchyClassMap(Map<Class<?>,V> delegate)
      Creates a new instance which wraps the given delegate, inheriting its rules.
      Parameters:
      delegate - a backing Map with predictable iteration order
  • Method Details