T
- the type of the value in this mappublic class ClassMap<T>
extends java.lang.Object
Maps Class objects to values. A ClassMap is different from a regular Map in that get(clazz)
does not only look to see if
clazz
is a key in the Map, but also walks the up superclass and interface graph of clazz
to find matches. Derived matches
of this sort are then cached in the registry so that matches are faster on future gets.
This is a very useful class for Class based registries.
Example:
ClassMap<String> m = new ClassMap<String>();
m.put(Animal.class, "Animal");
m.put(Fox.class, "Fox");
m.get(Fox.class) --> "Fox"
m.get(Dog.class) --> "Animal"
(assuming Dog.class < Animal.class)Constructor and Description |
---|
ClassMap() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
As per
java.util.Map , removes all of the mappings from this map (optional operation). |
T |
get(java.lang.Object key)
Gets the value associated with either this Class or a superclass of this class.
|
static <T> java.util.List<java.lang.Class<?>> |
getAncestry(java.lang.Class<T> clazz)
Helper method that walks superclass and interface graph, superclasses first, then interfaces, to compute an ancestry list.
|
boolean |
isEmpty()
As per
java.util.Map , returns true if this map contains no key-value mappings. |
T |
put(java.lang.Class<?> key,
T value)
As per
java.util.Map , associates the specified value with the specified key in this map. |
T |
remove(java.lang.Object key)
As per
java.util.Map , removes the mapping for a key from this map if it is present |
int |
size()
As per
java.util.Map , returns the number of key-value mappings in this map. |
public static <T> java.util.List<java.lang.Class<?>> getAncestry(java.lang.Class<T> clazz)
T
- the type of the class modeled by this Class
object.clazz
- the class to get the ancestors forclazz
, starting with the class, and ending with java.lang.Object
.public T get(java.lang.Object key)
key
- a Class
to get the value forpublic T put(java.lang.Class<?> key, T value)
java.util.Map
, associates the specified value with the specified key in this map. If the map previously contained a
mapping for the key, the old value is replaced by the specified value.key
- a Class
keyvalue
- the value for this classkey
, or null if there was no mapping for key.Map.put(Object, Object)
public T remove(java.lang.Object key)
java.util.Map
, removes the mapping for a key from this map if it is presentkey
- a Class
keykey
, or null if there was no mapping for key.Map.remove(Object)
public void clear()
java.util.Map
, removes all of the mappings from this map (optional operation).Map.clear()
public int size()
java.util.Map
, returns the number of key-value mappings in this map. This will only return the number of keys
explicitly added to the map, not any cached hierarchy keys.Map.size()
public boolean isEmpty()
java.util.Map
, returns true
if this map contains no key-value mappings.Map.isEmpty()