Package com.gentlyweb.utils
Class ObjectCache
java.lang.Object
com.gentlyweb.utils.ObjectCache
- All Implemented Interfaces:
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncacheSlice
(Date from, Date to) cacheSliceFrom
(Date from) cacheSliceTo
(Date to) int
capacity()
Return the current capacity of the cache, it should basically be (max size - current size).boolean
containsKey
(Object key) firstKey()
void
flush()
Clear our data structures.getLastAccessTime
(Object key) int
int
boolean
isEmpty()
Return whether the cache is empty or not.iterator()
keys()
Return a List of all the keys in the cache.Return a List of all the keys in the cache that match the conditions imposed by theGeneralFilter
passed in.Return a List of keys in the cache that match the conditions imposed by theGeneralFilter
AND are applied to the values NOT the keys.void
keysToList
(List list) void
merge
(ObjectCache cache) Merge the current cache with another.void
void
Add all the entries in the Map to cache.void
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:invalid reference
ObjectCache.OLDEST
invalid reference
ObjectCache.YOUNGEST
invalid reference
ObjectCache.RANDOM
int
size()
void
Get all the entries in the cache as a Map of key to value.values()
Return a List of all the values in the cache.Return a List of all the values in the cache that match the conditions imposed by theGeneralFilter
passed in.void
valuesToList
(List list)
-
Field Details
-
OLDEST
public static final int OLDEST- See Also:
-
YOUNGEST
public static final int YOUNGEST- See Also:
-
RANDOM
public static final int RANDOM- See Also:
-
-
Constructor Details
-
ObjectCache
- Throws:
IllegalArgumentException
-
-
Method Details
-
setPolicy
Description copied from interface:ObjectCacheManager
Set the policy for managing the cache, should be one of:invalid reference
ObjectCache.OLDEST
invalid reference
ObjectCache.YOUNGEST
invalid reference
ObjectCache.RANDOM
- Specified by:
setPolicy
in interfaceObjectCacheManager
- Parameters:
type
- The policy.- Throws:
IllegalArgumentException
-
getPolicy
public int getPolicy() -
iterator
-
keys
Return a List of all the keys in the cache.- Returns:
- The List (ArrayList) of all the keys in the cache.
-
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 theGeneralFilter
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:
IllegalAccessException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
InvocationTargetException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
FilterException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
-
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 theGeneralFilter
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:
IllegalAccessException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
InvocationTargetException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
FilterException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
-
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 theGeneralFilter
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:
IllegalAccessException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
InvocationTargetException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
FilterException
- Thrown by theinvalid @link
{@link GeneralFilter.accept(Object)
-
valuesToList
-
keysToList
-
toMap
Description copied from interface:ObjectCacheManager
Get all the entries in the cache as a Map of key to value.- Specified by:
toMap
in interfaceObjectCacheManager
- Parameters:
map
- The Map that should be populated with the key/values in the cache.
-
putAll
Description copied from interface:ObjectCacheManager
Add all the entries in the Map to cache.- Specified by:
putAll
in interfaceObjectCacheManager
- Parameters:
map
- The Map to get key/values from.
-
containsKey
-
merge
Description copied from interface:ObjectCacheManager
Merge the current cache with another.- Specified by:
merge
in interfaceObjectCacheManager
- Parameters:
cache
- The cache to merge.
-
cacheSliceTo
-
cacheSliceFrom
-
cacheSlice
-
sliceFrom
-
sliceTo
-
slice
-
getLastAccessTime
-
isEmpty
public boolean isEmpty()Description copied from interface:ObjectCacheManager
Return whether the cache is empty or not.- Specified by:
isEmpty
in interfaceObjectCacheManager
- 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 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:ObjectCacheManager
Set the maximum size of the cache.- Specified by:
setMaxSize
in interfaceObjectCacheManager
- Parameters:
size
- The maximum size.
-
get
-
firstValue
-
lastValue
-
firstKey
-
size
public int size() -
remove
-
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 interfaceObjectCacheManager
- Parameters:
size
- The new size.
-
resize
protected void resize() -
put
-
flush
public void flush()Clear our data structures.- Specified by:
flush
in interfaceObjectCacheManager
-