Class ClassLookupCache
- java.lang.Object
-
- org.onemind.commons.java.datastructure.LookupCache
-
- org.onemind.commons.java.lang.reflect.ClassLookupCache
-
public class ClassLookupCache extends LookupCache
A class lookup cache can lookup non-fully-qualified name classes for a given set of packages and will cache the lookup for later use. For example,
ClassLookupCache cache = new ClassLookupCache(); cache.addPackage("*"); //default package cache.addPackage("java.io.*"); Class c = cache.getClass("File"); //c = java.io.File c = cache.getClass("File1"); //c = null
NOTE: 1. The cache is static for all instances of the lookup cache. 2. The packages is instance specific 3. It will cache only positive and negative response of fully qualified name thus lookup of non-fully-qualified has some performance hit, but for the sake of correctness- Version:
- $Id: ClassLookupCache.java,v 1.6 2006/10/29 17:02:38 thlee Exp $ $Name: $
- Author:
- TiongHiang Lee (thlee@onemindsoft.org)
-
-
Constructor Summary
Constructors Constructor Description ClassLookupCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addImport(java.lang.String importName)
Add a new package.protected void
clearNegCache()
Clear all the negative cachejava.lang.Class
getClass(java.lang.String className)
Get the class given by the fully-qualified or non-fully qualified java class namejava.util.Set
getPackages()
Get all the import packages in this lookup cache.protected boolean
isInCache(java.lang.Object o)
Test if the key is in cacheprotected boolean
isInNegCache(java.lang.Object o)
Get whether the object is in negative cachejava.lang.Object
produce(java.lang.Object key)
Produce the class given the key Produce the object given the key.protected void
setDoNegativeCache(boolean b)
Turn on/off the negative cache-
Methods inherited from class org.onemind.commons.java.datastructure.LookupCache
lookup
-
-
-
-
Method Detail
-
addImport
public void addImport(java.lang.String importName)
Add a new package.- Parameters:
packageName
- the package name
-
getClass
public java.lang.Class getClass(java.lang.String className)
Get the class given by the fully-qualified or non-fully qualified java class name- Parameters:
className
- the class name- Returns:
- the class or null
-
produce
public java.lang.Object produce(java.lang.Object key)
Produce the class given the key Produce the object given the key. This is assumed to be an expensive operation and it will be called by the lookup method. The result will be cached by the lookup method and negative result also will be cached to if the doNegCache is turned on.- Specified by:
produce
in classLookupCache
- Parameters:
key
- the key- Returns:
- the result or null if no result
-
getPackages
public java.util.Set getPackages()
Get all the import packages in this lookup cache.- Returns:
- the packages
-
clearNegCache
protected void clearNegCache()
Clear all the negative cache- Overrides:
clearNegCache
in classLookupCache
-
isInCache
protected boolean isInCache(java.lang.Object o)
Test if the key is in cache- Overrides:
isInCache
in classLookupCache
- Parameters:
o
- the object- Returns:
- true if is in cache
-
isInNegCache
protected boolean isInNegCache(java.lang.Object o)
Get whether the object is in negative cache- Overrides:
isInNegCache
in classLookupCache
- Parameters:
o
- the object- Returns:
- true if is in negative cache
-
setDoNegativeCache
protected void setDoNegativeCache(boolean b)
Turn on/off the negative cache- Overrides:
setDoNegativeCache
in classLookupCache
- Parameters:
b
- true to turn on the neg cache
-
-