Class Cache

  • All Implemented Interfaces:
    Map

    public class Cache
    extends Dictionary
    implements Map
    IMPORTANT NOTE: The cache has be implemented by modifing the existing java.util.Hashtable code. Its has added functionality of a built in replacement strategy which in this case is based on Least Recently Used concept. The synchronization functionality that exists in a Hashtable has been removed to improve performance.

    The class Cache provides the functionality to cache objects based on their usage. The maximum size of the cache can be set using the constructor. If the maximum size is not set the default cache size for Cache will be obtained from the config file ??? file, defined using the key ???. The object that needs to be cached can be supplied to the instance of this class using the put method. The object can be obtained by invoking the get method on the instance. Each object that is cached is tracked based on its usage. If a new object needs to added to the cache and the maximum size limit of the cache is reached, then the least recently used object is replaced. This class implements a Cache, which maps keys to values. Any non-null object can be used as a key or as a value.

    To successfully store and retrieve objects from a Cache, the objects used as keys must implement the hashCode method and the equals method.

    An instance of Cache has three parameters that affect its performance: initial capacity, maximum size and load factor. The maximum capacity is the maximum number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case a "hash collision", a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the Cache exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method.

    Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Cache operations, including get and put).

    The capacity controls a tradeoff between wasted space and the need for rehash operations, which are time-consuming. No rehash operations will ever occur if the capacity is greater than the maximum number of entries the Cache will contain divided by its load factor. However, setting the capacity too high can waste space.

    If many entries are to be made into a Cache, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

    This class has been retrofitted to implement Map, so that it becomes a part of Java's collection framework. The Iterators returned by the iterator and listIterator methods of the Collections returned by all of Cache's "collection view methods" are fail-fast: if the Cache is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Cache's keys and values methods are not fail-fast.

    Since:
    JDK1.0
    See Also:
    Object.equals(java.lang.Object), Object.hashCode(), Collection, Map
    • Constructor Summary

      Constructors 
      Constructor Description
      Cache()  
      Cache​(String name, int initCapacity, int maxSize)
      Constructs a new, empty Cache with the specified capacity and default load factor, which is 0.75.
      Cache​(String name, int initCapacity, int maxSize, float loadFactor)
      Constructs a new, empty Cache with the specified capacity and the specified load factor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void adjustEntry​(Object key)
      This method adjusts the table by removing the entry corresponding to key from the table.
      String audit()  
      void clear()
      Clears this Cache so that it contains no keys.
      boolean contains​(Object value)
      Tests if some key maps into the specified value in this Cache.
      boolean containsKey​(Object key)
      Tests if the specified object is a key in this Cache.
      boolean containsValue​(Object value)
      Returns true if this Cache maps one or more keys to this value.
      Enumeration elements()
      Returns an enumeration of the values in this Cache.
      Set entrySet()
      Returns a Set view of the entries contained in this Cache.
      boolean equals​(Object o)
      Compares the specified Object with this Map for equality, as per the definition in the Map interface.
      Object get​(Object key)
      Returns the value to which the specified key is mapped in this Cache.
      String getName()
      Returns name.
      int hashCode()
      Returns the hash code value for this Map as per the definition in the Map interface.
      boolean isEmpty()
      Tests if this Cache maps no keys to values.
      Enumeration keys()
      Returns an enumeration of the keys in this Cache.
      Set keySet()
      Returns a Set view of the keys contained in this Cache.
      Object put​(Object key, Object value)
      Maps the specified key to the specified value in this Cache.
      void putAll​(Map t)
      Copies all of the mappings from the specified Map to this Hashtable These mappings will replace any mappings that this Hashtable had for any of the keys currently in the specified Map.
      protected void rehash()
      Increases the capacity of and internally reorganizes this Cache, in order to accommodate and access its entries more efficiently.
      Object remove​(Object key)
      Removes the key (and its corresponding value) from this Cache.
      int size()
      Returns the number of keys in this Cache.
      String toString()
      Returns a string representation of this Cache object in the form of a set of entries, enclosed in braces and separated by the ASCII characters "" (comma and space).
      Collection values()
      Returns a Collection view of the values contained in this Cache.
    • Constructor Detail

      • Cache

        public Cache​(String name,
                     int initCapacity,
                     int maxSize,
                     float loadFactor)
        Constructs a new, empty Cache with the specified capacity and the specified load factor.
        Parameters:
        name - Name of cache.
        initCapacity - the capacity of the Cache.
        maxSize - the maximum size of the cache.
        loadFactor - the load factor of the Cache.
        Throws:
        IllegalArgumentException - if the capacity is less than zero, or if the load factor is nonpositive.
      • Cache

        public Cache​(String name,
                     int initCapacity,
                     int maxSize)
        Constructs a new, empty Cache with the specified capacity and default load factor, which is 0.75.
        Parameters:
        name - Name of cache.
        initCapacity - the initial capacity of the Cache.
        maxSize - the maximum size of the cache.
        Throws:
        IllegalArgumentException - if the capacity is less than zero.
      • Cache

        public Cache()
    • Method Detail

      • getName

        public String getName()
        Returns name.
        Returns:
        name.
      • size

        public int size()
        Returns the number of keys in this Cache.
        Specified by:
        size in interface Map
        Specified by:
        size in class Dictionary
        Returns:
        the number of keys in this Cache.
      • isEmpty

        public boolean isEmpty()
        Tests if this Cache maps no keys to values.
        Specified by:
        isEmpty in interface Map
        Specified by:
        isEmpty in class Dictionary
        Returns:
        true if this Cache maps no keys to values; false otherwise.
      • elements

        public Enumeration elements()
        Returns an enumeration of the values in this Cache. Use the Enumeration methods on the returned object to fetch the elements sequentially.
        Specified by:
        elements in class Dictionary
        Returns:
        an enumeration of the values in this Cache.
        See Also:
        Enumeration, keys(), values(), Map
      • contains

        public boolean contains​(Object value)
        Tests if some key maps into the specified value in this Cache. This operation is more expensive than the containsKey method.

        Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the PolicyCollections framework).

        Parameters:
        value - a value to search for.
        Returns:
        true if and only if some key maps to the value argument in this Cache as determined by the equals method; false otherwise.
        Throws:
        NullPointerException - if the value is null.
        See Also:
        containsKey(Object), containsValue(Object), Map
      • containsValue

        public boolean containsValue​(Object value)
        Returns true if this Cache maps one or more keys to this value.

        Note that this method is identical in functionality to contains (which predates the Map interface).

        Specified by:
        containsValue in interface Map
        Parameters:
        value - value whose presence in this Cache is to be tested.
        Since:
        JDK1.2
        See Also:
        Map
      • containsKey

        public boolean containsKey​(Object key)
        Tests if the specified object is a key in this Cache.
        Specified by:
        containsKey in interface Map
        Parameters:
        key - possible key.
        Returns:
        true if and only if the specified object is a key in this Cache, as determined by the equals method; false otherwise.
        See Also:
        contains(Object)
      • get

        public Object get​(Object key)
        Returns the value to which the specified key is mapped in this Cache.
        Specified by:
        get in interface Map
        Specified by:
        get in class Dictionary
        Parameters:
        key - a key in the Cache.
        Returns:
        the value to which the key is mapped in this Cache; null if the key is not mapped to any value in this Cache.
        See Also:
        put(Object, Object)
      • rehash

        protected void rehash()
        Increases the capacity of and internally reorganizes this Cache, in order to accommodate and access its entries more efficiently. This method is called automatically when the number of keys in the Cache exceeds this Cache's capacity and load factor. It ensures the new capacity does not exceed the maximum size.
      • put

        public Object put​(Object key,
                          Object value)
        Maps the specified key to the specified value in this Cache. Neither the key nor the value can be null. If the cache is full to its capacity, then the least recently used entry in the cache will be replaced.

        The value can be retrieved by calling the get method with a key that is equal to the original key.

        Specified by:
        put in interface Map
        Specified by:
        put in class Dictionary
        Parameters:
        key - the Cache key.
        value - the value.
        Returns:
        the previous value of the specified key in this Cache, or null if it did not have one.
        Throws:
        NullPointerException - if the key or value is null.
        See Also:
        Object.equals(Object), get(Object)
      • adjustEntry

        protected void adjustEntry​(Object key)
        This method adjusts the table by removing the entry corresponding to key from the table.
      • remove

        public Object remove​(Object key)
        Removes the key (and its corresponding value) from this Cache. This method does nothing if the key is not in the Cache.
        Specified by:
        remove in interface Map
        Specified by:
        remove in class Dictionary
        Parameters:
        key - the key that needs to be removed.
        Returns:
        the value to which the key had been mapped in this Cache, or null if the key did not have a mapping.
      • putAll

        public void putAll​(Map t)
        Copies all of the mappings from the specified Map to this Hashtable These mappings will replace any mappings that this Hashtable had for any of the keys currently in the specified Map.
        Specified by:
        putAll in interface Map
        Since:
        JDK1.2
      • clear

        public void clear()
        Clears this Cache so that it contains no keys.
        Specified by:
        clear in interface Map
      • toString

        public String toString()
        Returns a string representation of this Cache object in the form of a set of entries, enclosed in braces and separated by the ASCII characters "" (comma and space). Each entry is rendered as the key, an equals sign =, and the associated element, where the toString method is used to convert the key and element to strings.

        Overrides to toString method of Object.

        Overrides:
        toString in class Object
        Returns:
        a string representation of this Cache.
      • audit

        public String audit()
      • keySet

        public Set keySet()
        Returns a Set view of the keys contained in this Cache. The Set is backed by the Cache, so changes to the Cache are reflected in the Set, and vice-versa. The Set supports element removal (which removes the corresponding entry from the Cache), but not element addition.
        Specified by:
        keySet in interface Map
        Since:
        JDK1.2
      • entrySet

        public Set entrySet()
        Returns a Set view of the entries contained in this Cache. Each element in this collection is a Map.Entry. The Set is backed by the Cache, so changes to the Cache are reflected in the Set, and vice-versa. The Set supports element removal (which removes the corresponding entry from the Cache), but not element addition.
        Specified by:
        entrySet in interface Map
        Since:
        JDK1.2
        See Also:
        Map.Entry
      • values

        public Collection values()
        Returns a Collection view of the values contained in this Cache. The Collection is backed by the Cache, so changes to the Cache are reflected in the Collection, and vice-versa. The Collection supports element removal (which removes the corresponding entry from the Cache), but not element addition.
        Specified by:
        values in interface Map
        Since:
        JDK1.2
      • equals

        public boolean equals​(Object o)
        Compares the specified Object with this Map for equality, as per the definition in the Map interface.
        Specified by:
        equals in interface Map
        Overrides:
        equals in class Object
        Returns:
        true if the specified Object is equal to this Map.
        Since:
        JDK1.2
        See Also:
        Map.equals(Object)
      • hashCode

        public int hashCode()
        Returns the hash code value for this Map as per the definition in the Map interface.
        Specified by:
        hashCode in interface Map
        Overrides:
        hashCode in class Object
        Since:
        JDK1.2
        See Also:
        Map.hashCode()