FileDocCategorySizeDatePackage
ResourceCacheLRUImpl.javaAPI DocphoneME MR2 API (J2ME)5399Wed May 02 18:00:46 BST 2007com.sun.j2me.global

ResourceCacheLRUImpl

public class ResourceCacheLRUImpl extends Object implements ResourceCache
{@link ResourceCache} implementation. Implements LRU algorithm. Cache is created with parameters max cache capacity [Kb] and max allowed resource size [kB]. Cache cannot grow over max cache capacity and if resource is bigger then max allowed resource size it isn't stored in cache.

Fields Summary
private static final String
classname
Class name.
private static Vector
cache
Storage for resources.
private static int
maxCacheCapacity
Maximum cache capacity in KB.
private static int
maxAllowedResourceSize
Maximum size of resource in KB.
private static int
cacheSize
Current cache size in bytes.
Constructors Summary
public ResourceCacheLRUImpl(int capacityKB, int maxResourceSize)
Constructor creates cache initialized with capacity and max resource size.

param
capacityKB max cache capacity in KB
param
maxResourceSize maximum size of resource in KB



                                           
         
        this.maxCacheCapacity = capacityKB * 1024;
        this.maxAllowedResourceSize = maxResourceSize * 1024;
    
Methods Summary
public synchronized booleanaddResource(Resource o)
Add resource into cache. Size of resource is checked against maxResourceSize and cache capacity. Only if resource doesn't exceed maxResourceSize it's stored in cache. If adding resource would exceeed max cache size, resources are removed from cache until the resource could have be en stored.

param
o The feature to be added to the Resource attribute
return
true if resource was stored in cache.

        if (o.getSize() > maxAllowedResourceSize) {
            return false;
        }
        while (cacheSize + o.getSize() > maxCacheCapacity) {
            cacheSize -= ((Resource) cache.elementAt(0)).getSize();
            cache.removeElementAt(0);
            // remove last used element
        }
        // while
        cache.addElement(o);
        cacheSize += o.getSize();
        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": " +
                           "\nCache: " + this +
                           "\nResource added to cache\n" +
                           "\nCache size = " + cacheSize);
        }
        return true;
    
public intgetHashCodeForResource(java.lang.String base, java.lang.String locale, int id)
Calculates hash code for the resource.

param
base the base name
param
locale the locale
param
id the resource id
return
hash code for resource

        int hashCode = new String(locale + 
                                  base + 
                                  Integer.toString(id)).hashCode();
        return hashCode;
    
public synchronized Resourcelookup(int hashCode)
Resource is looked up in cache. If resource was found it is returned. Otherwise null is returned.

param
hashCode Description of the Parameter
return
resource if it was found or null if resource wasn't found.

        Resource o = null;
        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": " +
                           "\nCache: " + this +
                           "\nLooking up resource with key=" + hashCode);
        }
        for (int i = cache.size() - 1; i >= 0; i--) {
            o = (Resource) cache.elementAt(i);
            if (cache.elementAt(i).hashCode() == hashCode) {
                // move element to front
                cache.removeElementAt(i);
                cache.addElement(o);
                return o;
            }
        }
        // for
        return null;