Package com.jidesoft.utils
Class CacheMap<T,K>
- java.lang.Object
-
- com.jidesoft.utils.CacheMap<T,K>
-
- Direct Known Subclasses:
CacheMap
public class CacheMap<T,K> extends java.lang.Object
CacheMap
is a two-levelHashMap
. It uses Class as the key and you can map the key to an object and a context as a pair. We use context because we want to register multiple objects with the same Class.register(Class, Object, Object)
is the method to register a new entry.getRegisteredObject(Class, Object)
will allow you to look up the object by specifying the Class and the context.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<RegistrationListener>
listenerList
List of listeners
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addRegistrationListener(RegistrationListener l)
Adds a listener to the list that's notified each time a change to the registration occurs.void
clear()
void
fireRegistrationChanged(RegistrationEvent e)
Forwards the given notification event to allRegistrationListeners
that registered themselves as listeners for this table model.protected com.jidesoft.utils.CacheMap.Cache<K,T>
getCache(java.lang.Class<?> clazz)
K[]
getKeys(java.lang.Class<?> clazz, K[] a)
Gets the secondary keys that are registered with the class in CacheMap.T
getMatchRegisteredObject(java.lang.Class<?> clazz, K context)
Gets the exact match registered object.T
getRegisteredObject(java.lang.Class<?> clazz, K context)
Gets registered object from CacheMap.RegistrationListener[]
getRegistrationListeners()
Returns an array of all the registration listeners registered on this registration.java.util.List<T>
getValues()
protected com.jidesoft.utils.CacheMap.Cache<K,T>
initCache(java.lang.Class<?> clazz)
void
register(java.lang.Class<?> clazz, T object, K context)
Registers an object with the specified clazz and object.void
remove(java.lang.Class<?> clazz)
Remove all registrations for the designated class.void
removeRegistrationListener(RegistrationListener l)
Removes a listener from the list that's notified each time a change to the registration occurs.void
unregister(java.lang.Class<?> clazz, K context)
Unregisters the object associated with the specified class and context.
-
-
-
Field Detail
-
listenerList
protected java.util.List<RegistrationListener> listenerList
List of listeners
-
-
Constructor Detail
-
CacheMap
public CacheMap(K defaultContext)
Constructs aCacheMap
.- Parameters:
defaultContext
- the default context.
-
-
Method Detail
-
getKeys
public K[] getKeys(java.lang.Class<?> clazz, K[] a)
Gets the secondary keys that are registered with the class in CacheMap.- Parameters:
clazz
- the classa
- the array to receive the keys.- Returns:
- the secondary keys.
-
register
public void register(java.lang.Class<?> clazz, T object, K context)
Registers an object with the specified clazz and object.- Parameters:
clazz
- the class which is used as the key.object
- the object, or the value of the mappingcontext
- the secondary key. It is used to register multiple objects to the same primary key (the clazz parameter in this case).
-
unregister
public void unregister(java.lang.Class<?> clazz, K context)
Unregisters the object associated with the specified class and context.- Parameters:
clazz
- the classcontext
- the context
-
getRegisteredObject
public T getRegisteredObject(java.lang.Class<?> clazz, K context)
Gets registered object from CacheMap. The algorithm used to look up is
1. First check for exact match with clazz and context.
2. If didn't find, look for interfaces that clazz implements using the exact context.
3. If still didn't find, look for super class of clazz using the exact context.
4. If still didn't find, using the exact clazz with default context.
5. If still didn't find, return null.
If found a match in step 1, 2, 3 or 4, it will return the registered object immediately.- Parameters:
clazz
- the class which is used as the primary key.context
- the context which is used as the secondary key. This parameter could be null in which case the default context is used.- Returns:
- registered object the object associated with the class and the context.
-
getMatchRegisteredObject
public T getMatchRegisteredObject(java.lang.Class<?> clazz, K context)
Gets the exact match registered object. Different fromgetRegisteredObject(Class, Object)
which will try different context and super classes and interfaces to find match. This method will do an exact match.- Parameters:
clazz
- the class which is used as the primary key.context
- the context which is used as the secondary key. This parameter could be null in which case the default context is used.- Returns:
- registered object the object associated with the class and the context.
-
getValues
public java.util.List<T> getValues()
-
remove
public void remove(java.lang.Class<?> clazz)
Remove all registrations for the designated class.- Parameters:
clazz
- the class
-
clear
public void clear()
-
addRegistrationListener
public void addRegistrationListener(RegistrationListener l)
Adds a listener to the list that's notified each time a change to the registration occurs.- Parameters:
l
- the RegistrationListener
-
removeRegistrationListener
public void removeRegistrationListener(RegistrationListener l)
Removes a listener from the list that's notified each time a change to the registration occurs.- Parameters:
l
- the RegistrationListener
-
getRegistrationListeners
public RegistrationListener[] getRegistrationListeners()
Returns an array of all the registration listeners registered on this registration.- Returns:
- all of this registration's
RegistrationListener
s or an empty array if no registration listeners are currently registered - See Also:
addRegistrationListener(com.jidesoft.utils.RegistrationListener)
,removeRegistrationListener(com.jidesoft.utils.RegistrationListener)
-
fireRegistrationChanged
public void fireRegistrationChanged(RegistrationEvent e)
Forwards the given notification event to allRegistrationListeners
that registered themselves as listeners for this table model.- Parameters:
e
- the event to be forwarded- See Also:
addRegistrationListener(com.jidesoft.utils.RegistrationListener)
,RegistrationEvent
-
-