Package com.gentlyweb.utils
Class ObjectCache
- java.lang.Object
-
- com.gentlyweb.utils.ObjectCache
-
- All Implemented Interfaces:
ObjectCacheManager
public class ObjectCache extends 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 theObjectCacheManagerinterface they only have to provide their own functionality whilst client classes can rely on the interface. All operations defined withObjectCacheManagerare supported here.
-
-
Constructor Summary
Constructors Constructor Description ObjectCache(int policy)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ObjectCachecacheSlice(Date from, Date to)ObjectCachecacheSliceFrom(Date from)ObjectCachecacheSliceTo(Date to)intcapacity()Return the current capacity of the cache, it should basically be (max size - current size).booleancontainsKey(Object key)ObjectfirstKey()ObjectfirstValue()voidflush()Clear our data structures.Objectget(Object key)DategetLastAccessTime(Object key)intgetMaxSize()intgetPolicy()booleanisEmpty()Return whether the cache is empty or not.Iteratoriterator()Listkeys()Return a List of all the keys in the cache.Listkeys(GeneralFilter f)Return a List of all the keys in the cache that match the conditions imposed by theGeneralFilterpassed in.ListkeysForFilteredValues(GeneralFilter f)Return a List of keys in the cache that match the conditions imposed by theGeneralFilterAND are applied to the values NOT the keys.voidkeysToList(List list)ObjectlastValue()voidmerge(ObjectCache cache)Merge the current cache with another.voidput(Object key, Object value)voidputAll(Map map)Add all the entries in the Map to cache.voidremove(Object key)protected voidresize()voidresize(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.voidsetMaxSize(int size)Set the maximum size of the cache.voidsetPolicy(int type)Set the policy for managing the cache, should be one of:ObjectCache.OLDEST,ObjectCache.YOUNGEST,ObjectCache.RANDOM.intsize()Mapslice(Date from, Date to)MapsliceFrom(Date from)MapsliceTo(Date to)voidtoMap(Map map)Get all the entries in the cache as a Map of key to value.Listvalues()Return a List of all the values in the cache.Listvalues(GeneralFilter f)Return a List of all the values in the cache that match the conditions imposed by theGeneralFilterpassed in.voidvaluesToList(List list)
-
-
-
Field Detail
-
OLDEST
public static final int OLDEST
- See Also:
- Constant Field Values
-
YOUNGEST
public static final int YOUNGEST
- See Also:
- Constant Field Values
-
RANDOM
public static final int RANDOM
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ObjectCache
public ObjectCache(int policy) throws IllegalArgumentException- Throws:
IllegalArgumentException
-
-
Method Detail
-
setPolicy
public void setPolicy(int type) throws IllegalArgumentExceptionDescription copied from interface:ObjectCacheManagerSet the policy for managing the cache, should be one of:ObjectCache.OLDEST,ObjectCache.YOUNGEST,ObjectCache.RANDOM.- Specified by:
setPolicyin interfaceObjectCacheManager- Parameters:
type- The policy.- Throws:
IllegalArgumentException
-
getPolicy
public int getPolicy()
-
iterator
public Iterator iterator()
-
keys
public 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 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 List keysForFilteredValues(GeneralFilter f) throws IllegalAccessException, InvocationTargetException, FilterException
Return a List of keys in the cache that match the conditions imposed by theGeneralFilterAND 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:
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.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 List values(GeneralFilter f) throws IllegalAccessException, InvocationTargetException, FilterException
Return a List of all the values in the cache that match the conditions imposed by theGeneralFilterpassed 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:
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.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 List keys(GeneralFilter f) throws IllegalAccessException, InvocationTargetException, FilterException
Return a List of all the keys in the cache that match the conditions imposed by theGeneralFilterpassed 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:
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.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(List list)
-
keysToList
public void keysToList(List list)
-
toMap
public void toMap(Map map)
Description copied from interface:ObjectCacheManagerGet all the entries in the cache as a Map of key to value.- Specified by:
toMapin interfaceObjectCacheManager- Parameters:
map- The Map that should be populated with the key/values in the cache.
-
putAll
public void putAll(Map map)
Description copied from interface:ObjectCacheManagerAdd all the entries in the Map to cache.- Specified by:
putAllin interfaceObjectCacheManager- Parameters:
map- The Map to get key/values from.
-
containsKey
public boolean containsKey(Object key)
-
merge
public void merge(ObjectCache cache)
Description copied from interface:ObjectCacheManagerMerge the current cache with another.- Specified by:
mergein interfaceObjectCacheManager- Parameters:
cache- The cache to merge.
-
cacheSliceTo
public ObjectCache cacheSliceTo(Date to)
-
cacheSliceFrom
public ObjectCache cacheSliceFrom(Date from)
-
cacheSlice
public ObjectCache cacheSlice(Date from, Date to)
-
isEmpty
public boolean isEmpty()
Description copied from interface:ObjectCacheManagerReturn whether the cache is empty or not.- Specified by:
isEmptyin interfaceObjectCacheManager- Returns:
trueif the cache is empty,falseif it has entries.
-
capacity
public int capacity()
Description copied from interface:ObjectCacheManagerReturn the current capacity of the cache, it should basically be (max size - current size).- Specified by:
capacityin interfaceObjectCacheManager- 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:ObjectCacheManagerSet the maximum size of the cache.- Specified by:
setMaxSizein interfaceObjectCacheManager- Parameters:
size- The maximum size.
-
firstValue
public Object firstValue()
-
lastValue
public Object lastValue()
-
firstKey
public Object firstKey()
-
size
public int size()
-
remove
public void remove(Object key)
-
resize
public void resize(int size)
Description copied from interface:ObjectCacheManagerResize 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:
resizein interfaceObjectCacheManager- Parameters:
size- The new size.
-
resize
protected void resize()
-
flush
public void flush()
Clear our data structures.- Specified by:
flushin interfaceObjectCacheManager
-
-