public class ObjectCache extends java.lang.Object implements ObjectCacheManager
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.Modifier and Type | Field and Description |
---|---|
static int |
OLDEST |
static int |
RANDOM |
static int |
YOUNGEST |
Constructor and Description |
---|
ObjectCache(int policy) |
Modifier and Type | Method and 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) |
public static final int OLDEST
public static final int YOUNGEST
public static final int RANDOM
public ObjectCache(int policy) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public void setPolicy(int type) throws java.lang.IllegalArgumentException
ObjectCacheManager
ObjectCache.OLDEST
, ObjectCache.YOUNGEST
, ObjectCache.RANDOM
.setPolicy
in interface ObjectCacheManager
type
- The policy.java.lang.IllegalArgumentException
public int getPolicy()
public java.util.Iterator iterator()
public java.util.List keys()
public java.util.List values()
public java.util.List keysForFilteredValues(GeneralFilter f) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
GeneralFilter
AND are applied to the values NOT the keys.f
- The filter to use.java.lang.IllegalAccessException
- Thrown by the 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 GeneralFilter.accept(Object)
method if (as a result of accessing a field) an exception
is thrown by a value object.FilterException
- Thrown by the 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.public java.util.List values(GeneralFilter f) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
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.java.lang.IllegalAccessException
- Thrown by the 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 GeneralFilter.accept(Object)
method if (as a result of accessing a field) an exception
is thrown by a value object.FilterException
- Thrown by the 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.public java.util.List keys(GeneralFilter f) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
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.java.lang.IllegalAccessException
- Thrown by the 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 GeneralFilter.accept(Object)
method if (as a result of accessing a field) an exception
is thrown by a key object.FilterException
- Thrown by the 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.public void valuesToList(java.util.List list)
public void keysToList(java.util.List list)
public void toMap(java.util.Map map)
ObjectCacheManager
toMap
in interface ObjectCacheManager
map
- The Map that should be populated with the key/values in the cache.public void putAll(java.util.Map map)
ObjectCacheManager
putAll
in interface ObjectCacheManager
map
- The Map to get key/values from.public boolean containsKey(java.lang.Object key)
public void merge(ObjectCache cache)
ObjectCacheManager
merge
in interface ObjectCacheManager
cache
- The cache to merge.public ObjectCache cacheSliceTo(java.util.Date to)
public ObjectCache cacheSliceFrom(java.util.Date from)
public ObjectCache cacheSlice(java.util.Date from, java.util.Date to)
public java.util.Map sliceFrom(java.util.Date from)
public java.util.Map sliceTo(java.util.Date to)
public java.util.Map slice(java.util.Date from, java.util.Date to)
public java.util.Date getLastAccessTime(java.lang.Object key)
public boolean isEmpty()
ObjectCacheManager
isEmpty
in interface ObjectCacheManager
true
if the cache is empty, false
if it has entries.public int capacity()
ObjectCacheManager
capacity
in interface ObjectCacheManager
public int getMaxSize()
public void setMaxSize(int size)
ObjectCacheManager
setMaxSize
in interface ObjectCacheManager
size
- The maximum size.public java.lang.Object get(java.lang.Object key)
public java.lang.Object firstValue()
public java.lang.Object lastValue()
public java.lang.Object firstKey()
public int size()
public void remove(java.lang.Object key)
public void resize(int size)
ObjectCacheManager
resize
in interface ObjectCacheManager
size
- The new size.protected void resize()
public void put(java.lang.Object key, java.lang.Object value)
public void flush()
flush
in interface ObjectCacheManager