Class ObjectCache

  • All Implemented Interfaces:
    ObjectCacheManager

    public class ObjectCache
    extends java.lang.Object
    implements ObjectCacheManager
    The ObjectCache is it's own manager which means that classes that extend this one can do so quickly and without having to implement the ObjectCacheManager interface they only have to provide their own functionality whilst client classes can rely on the interface. All operations defined with ObjectCacheManager are supported here.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int OLDEST  
      static int RANDOM  
      static int YOUNGEST  
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectCache​(int policy)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ObjectCache cacheSlice​(java.util.Date from, java.util.Date to)  
      ObjectCache cacheSliceFrom​(java.util.Date from)  
      ObjectCache cacheSliceTo​(java.util.Date to)  
      int capacity()
      Return the current capacity of the cache, it should basically be (max size - current size).
      boolean containsKey​(java.lang.Object key)  
      java.lang.Object firstKey()  
      java.lang.Object firstValue()  
      void flush()
      Clear our data structures.
      java.lang.Object get​(java.lang.Object key)  
      java.util.Date getLastAccessTime​(java.lang.Object key)  
      int getMaxSize()  
      int getPolicy()  
      boolean isEmpty()
      Return whether the cache is empty or not.
      java.util.Iterator iterator()  
      java.util.List keys()
      Return a List of all the keys in the cache.
      java.util.List keys​(GeneralFilter f)
      Return a List of all the keys in the cache that match the conditions imposed by the GeneralFilter passed in.
      java.util.List keysForFilteredValues​(GeneralFilter f)
      Return a List of keys in the cache that match the conditions imposed by the GeneralFilter AND are applied to the values NOT the keys.
      void keysToList​(java.util.List list)  
      java.lang.Object lastValue()  
      void merge​(ObjectCache cache)
      Merge the current cache with another.
      void put​(java.lang.Object key, java.lang.Object value)  
      void putAll​(java.util.Map map)
      Add all the entries in the Map to cache.
      void remove​(java.lang.Object key)  
      protected void resize()  
      void resize​(int size)
      Resize the cache to a particular size, if the size is actually bigger than the current size then this operation should not touch the cached objects, if the size is less then the cache should be reduced in size using the current policy until the size is reached.
      void setMaxSize​(int size)
      Set the maximum size of the cache.
      void setPolicy​(int type)
      Set the policy for managing the cache, should be one of: ObjectCache.OLDEST, ObjectCache.YOUNGEST, ObjectCache.RANDOM.
      int size()  
      java.util.Map slice​(java.util.Date from, java.util.Date to)  
      java.util.Map sliceFrom​(java.util.Date from)  
      java.util.Map sliceTo​(java.util.Date to)  
      void toMap​(java.util.Map map)
      Get all the entries in the cache as a Map of key to value.
      java.util.List values()
      Return a List of all the values in the cache.
      java.util.List values​(GeneralFilter f)
      Return a List of all the values in the cache that match the conditions imposed by the GeneralFilter passed in.
      void valuesToList​(java.util.List list)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ObjectCache

        public ObjectCache​(int policy)
                    throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
    • Method Detail

      • setPolicy

        public void setPolicy​(int type)
                       throws java.lang.IllegalArgumentException
        Description copied from interface: ObjectCacheManager
        Set the policy for managing the cache, should be one of: ObjectCache.OLDEST, ObjectCache.YOUNGEST, ObjectCache.RANDOM.
        Specified by:
        setPolicy in interface ObjectCacheManager
        Parameters:
        type - The policy.
        Throws:
        java.lang.IllegalArgumentException
      • getPolicy

        public int getPolicy()
      • iterator

        public java.util.Iterator iterator()
      • keys

        public java.util.List keys()
        Return a List of all the keys in the cache.
        Returns:
        The List (ArrayList) of all the keys in the cache.
      • values

        public java.util.List values()
        Return a List of all the values in the cache.
        Returns:
        The List (ArrayList) of all the values in the cache.
      • keysForFilteredValues

        public java.util.List keysForFilteredValues​(GeneralFilter f)
                                             throws java.lang.IllegalAccessException,
                                                    java.lang.reflect.InvocationTargetException,
                                                    FilterException
        Return a List of keys in the cache that match the conditions imposed by the GeneralFilter AND are applied to the values NOT the keys.
        Parameters:
        f - The filter to use.
        Returns:
        A List of the keys that map to the matched values.
        Throws:
        java.lang.IllegalAccessException - Thrown by the {@link GeneralFilter.accept(Object)} method if we can't get access to a value defined in the GeneralFilter for the value class.
        java.lang.reflect.InvocationTargetException - Thrown by the {@link GeneralFilter.accept(Object)} method if (as a result of accessing a field) an exception is thrown by a value object.
        FilterException - Thrown by the {@link GeneralFilter.accept(Object)} method if (as a result of accessing a field) the type of object returned by the field access is not of the expected type.
      • values

        public java.util.List values​(GeneralFilter f)
                              throws java.lang.IllegalAccessException,
                                     java.lang.reflect.InvocationTargetException,
                                     FilterException
        Return a List of all the values in the cache that match the conditions imposed by the GeneralFilter passed in. We first gain all the values in the cache and then pass them through the filter returning the values that match. Because a GeneralFilter can only filter on a single class type (but the values may not be of a single type) we ignore any values that are not of the type specified for the GeneralFilter.
        Returns:
        A List (ArrayList) of all the values in the cache.
        Throws:
        java.lang.IllegalAccessException - Thrown by the {@link GeneralFilter.accept(Object)} method if we can't get access to a value defined in the GeneralFilter for the value class.
        java.lang.reflect.InvocationTargetException - Thrown by the {@link GeneralFilter.accept(Object)} method if (as a result of accessing a field) an exception is thrown by a value object.
        FilterException - Thrown by the {@link GeneralFilter.accept(Object)} method if (as a result of accessing a field) the type of object returned by the field access is not of the expected type.
      • keys

        public java.util.List keys​(GeneralFilter f)
                            throws java.lang.IllegalAccessException,
                                   java.lang.reflect.InvocationTargetException,
                                   FilterException
        Return a List of all the keys in the cache that match the conditions imposed by the GeneralFilter passed in. We first gain all the keys in the cache and then pass them through the filter returning the keys that match. Because a GeneralFilter can only filter on a single class type (but the keys may not be of a single type) we ignore any keys that are not of the type specified for the GeneralFilter.
        Returns:
        A List (ArrayList) of all the keys in the cache.
        Throws:
        java.lang.IllegalAccessException - Thrown by the {@link GeneralFilter.accept(Object)} method if we can't get access to a value defined in the GeneralFilter for the key class.
        java.lang.reflect.InvocationTargetException - Thrown by the {@link GeneralFilter.accept(Object)} method if (as a result of accessing a field) an exception is thrown by a key object.
        FilterException - Thrown by the {@link GeneralFilter.accept(Object)} method if (as a result of accessing a field) the type of object returned by the field access is not of the expected type.
      • valuesToList

        public void valuesToList​(java.util.List list)
      • keysToList

        public void keysToList​(java.util.List list)
      • toMap

        public void toMap​(java.util.Map map)
        Description copied from interface: ObjectCacheManager
        Get all the entries in the cache as a Map of key to value.
        Specified by:
        toMap in interface ObjectCacheManager
        Parameters:
        map - The Map that should be populated with the key/values in the cache.
      • putAll

        public void putAll​(java.util.Map map)
        Description copied from interface: ObjectCacheManager
        Add all the entries in the Map to cache.
        Specified by:
        putAll in interface ObjectCacheManager
        Parameters:
        map - The Map to get key/values from.
      • containsKey

        public boolean containsKey​(java.lang.Object key)
      • cacheSliceTo

        public ObjectCache cacheSliceTo​(java.util.Date to)
      • cacheSliceFrom

        public ObjectCache cacheSliceFrom​(java.util.Date from)
      • cacheSlice

        public ObjectCache cacheSlice​(java.util.Date from,
                                      java.util.Date to)
      • sliceFrom

        public java.util.Map sliceFrom​(java.util.Date from)
      • sliceTo

        public java.util.Map sliceTo​(java.util.Date to)
      • slice

        public java.util.Map slice​(java.util.Date from,
                                   java.util.Date to)
      • getLastAccessTime

        public java.util.Date getLastAccessTime​(java.lang.Object key)
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: ObjectCacheManager
        Return whether the cache is empty or not.
        Specified by:
        isEmpty in interface ObjectCacheManager
        Returns:
        true if the cache is empty, false if it has entries.
      • capacity

        public int capacity()
        Description copied from interface: ObjectCacheManager
        Return the current capacity of the cache, it should basically be (max size - current size).
        Specified by:
        capacity in interface ObjectCacheManager
        Returns:
        The current number of items that can be added until the cache reaches it's maximum size.
      • getMaxSize

        public int getMaxSize()
      • setMaxSize

        public void setMaxSize​(int size)
        Description copied from interface: ObjectCacheManager
        Set the maximum size of the cache.
        Specified by:
        setMaxSize in interface ObjectCacheManager
        Parameters:
        size - The maximum size.
      • get

        public java.lang.Object get​(java.lang.Object key)
      • firstValue

        public java.lang.Object firstValue()
      • lastValue

        public java.lang.Object lastValue()
      • firstKey

        public java.lang.Object firstKey()
      • size

        public int size()
      • remove

        public void remove​(java.lang.Object key)
      • resize

        public void resize​(int size)
        Description copied from interface: ObjectCacheManager
        Resize the cache to a particular size, if the size is actually bigger than the current size then this operation should not touch the cached objects, if the size is less then the cache should be reduced in size using the current policy until the size is reached. Either way the maximum size should be set to this value.
        Specified by:
        resize in interface ObjectCacheManager
        Parameters:
        size - The new size.
      • resize

        protected void resize()
      • put

        public void put​(java.lang.Object key,
                        java.lang.Object value)