org.bson.util
Class ClassMap<T>
java.lang.Object
org.bson.util.ClassMap<T>
- All Implemented Interfaces:
- java.util.Map<java.lang.Class,T>
public class ClassMap<T>
- extends java.lang.Object
- implements java.util.Map<java.lang.Class,T>
Maps Class objects to values. A ClassMap is different from a regular Map
in that get(c) does not only look to see if 'c' is a key in the
Map, but also walks the up superclass and interface graph of 'c'
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 m = new ClassMap();
m.put(Animal.class, "Animal");
m.put(Fox.class, "Fox");
m.Fox.class) --> "Fox"
m.get(Dog.class) --> "Animal"
(assuming Dog.class < Animal.class)
Nested classes/interfaces inherited from interface java.util.Map |
java.util.Map.Entry<K,V> |
Method Summary |
protected boolean |
cacheContainsKey(java.lang.Object key)
cacheContainsKey |
void |
clear()
clear |
boolean |
containsKey(java.lang.Object key)
containsKey |
boolean |
containsValue(java.lang.Object object)
containsValue |
java.util.Set<java.util.Map.Entry<java.lang.Class,T>> |
entrySet()
entrySet |
boolean |
equals(java.lang.Object object)
equals |
T |
get(java.lang.Object key)
get |
static java.util.List<java.lang.Class> |
getAncestry(java.lang.Class c)
getAncestry
Walks superclass and interface graph, superclasses first, then
interfaces, to compute an ancestry list. |
protected java.util.Map<java.lang.Class,T> |
getCache()
cache |
protected java.util.Map<java.lang.Class,T> |
getInternalMap()
internalMap |
int |
hashCode()
hashCode |
protected void |
initCache()
initCache |
boolean |
isEmpty()
isEmpty |
java.util.Set<java.lang.Class> |
keySet()
keySet |
T |
put(java.lang.Class key,
T value)
put |
void |
putAll(java.util.Map map)
putAll |
T |
remove(java.lang.Object object)
remove |
int |
size()
size |
java.lang.String |
toString()
toString |
java.util.Collection<T> |
values()
values |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
ClassMap
public ClassMap()
getInternalMap
protected java.util.Map<java.lang.Class,T> getInternalMap()
- internalMap
getCache
protected java.util.Map<java.lang.Class,T> getCache()
- cache
size
public int size()
- size
- Specified by:
size
in interface java.util.Map<java.lang.Class,T>
isEmpty
public boolean isEmpty()
- isEmpty
- Specified by:
isEmpty
in interface java.util.Map<java.lang.Class,T>
containsKey
public boolean containsKey(java.lang.Object key)
- containsKey
- Specified by:
containsKey
in interface java.util.Map<java.lang.Class,T>
cacheContainsKey
protected boolean cacheContainsKey(java.lang.Object key)
- cacheContainsKey
containsValue
public boolean containsValue(java.lang.Object object)
- containsValue
- Specified by:
containsValue
in interface java.util.Map<java.lang.Class,T>
get
public T get(java.lang.Object key)
- get
- Specified by:
get
in interface java.util.Map<java.lang.Class,T>
put
public T put(java.lang.Class key,
T value)
- put
- Specified by:
put
in interface java.util.Map<java.lang.Class,T>
remove
public T remove(java.lang.Object object)
- remove
- Specified by:
remove
in interface java.util.Map<java.lang.Class,T>
putAll
public void putAll(java.util.Map map)
- putAll
- Specified by:
putAll
in interface java.util.Map<java.lang.Class,T>
clear
public void clear()
- clear
- Specified by:
clear
in interface java.util.Map<java.lang.Class,T>
keySet
public java.util.Set<java.lang.Class> keySet()
- keySet
- Specified by:
keySet
in interface java.util.Map<java.lang.Class,T>
values
public java.util.Collection<T> values()
- values
- Specified by:
values
in interface java.util.Map<java.lang.Class,T>
entrySet
public java.util.Set<java.util.Map.Entry<java.lang.Class,T>> entrySet()
- entrySet
- Specified by:
entrySet
in interface java.util.Map<java.lang.Class,T>
equals
public boolean equals(java.lang.Object object)
- equals
- Specified by:
equals
in interface java.util.Map<java.lang.Class,T>
- Overrides:
equals
in class java.lang.Object
hashCode
public int hashCode()
- hashCode
- Specified by:
hashCode
in interface java.util.Map<java.lang.Class,T>
- Overrides:
hashCode
in class java.lang.Object
initCache
protected void initCache()
- initCache
toString
public java.lang.String toString()
- toString
- Overrides:
toString
in class java.lang.Object
getAncestry
public static java.util.List<java.lang.Class> getAncestry(java.lang.Class c)
- getAncestry
Walks superclass and interface graph, superclasses first, then
interfaces, to compute an ancestry list. Supertypes are visited
left to right. Duplicates are removed such that no Class will
appear in the list before one of its subtypes.
Does not need to be synchronized, races are harmless
as the Class graph does not change at runtime.