00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_LRUCACHE_H_
00019 #define _DECAF_UTIL_LRUCACHE_H_
00020
00021 #include <decaf/util/Config.h>
00022
00023 #include <decaf/util/LinkedHashMap.h>
00024 #include <decaf/lang/exceptions/IllegalArgumentException.h>
00025
00026 namespace decaf {
00027 namespace util {
00028
00042 template<typename K, typename V, typename HASHCODE = HashCode<K> >
00043 class LRUCache : public LinkedHashMap<K, V, HASHCODE> {
00044 protected:
00045
00046 int maxCacheSize;
00047
00048 public:
00049
00050
00054 LRUCache() : LinkedHashMap<K, V, HASHCODE>(0, 0.75f, true), maxCacheSize(10000) {}
00055
00062 LRUCache(int maximumCacheSize) :
00063 LinkedHashMap<K, V, HASHCODE>(0, 0.75f, true), maxCacheSize(maximumCacheSize) {
00064
00065 if (maximumCacheSize <= 0) {
00066 throw decaf::lang::exceptions::IllegalArgumentException(
00067 __FILE__, __LINE__, "Cache size must be greater than zero.");
00068 }
00069 }
00070
00087 LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, bool accessOrder) :
00088 LinkedHashMap<K, V, HASHCODE>(initialCapacity, loadFactor, accessOrder), maxCacheSize(maximumCacheSize) {
00089
00090 if (maximumCacheSize <= 0) {
00091 throw decaf::lang::exceptions::IllegalArgumentException(
00092 __FILE__, __LINE__, "Cache size must be greater than zero.");
00093 }
00094 }
00095
00096 virtual ~LRUCache() {}
00097
00103 int getMaxCacheSize() const {
00104 return maxCacheSize;
00105 }
00106
00115 void setMaxCacheSize(int size) {
00116 if (size <= 0) {
00117 throw decaf::lang::exceptions::IllegalArgumentException(
00118 __FILE__, __LINE__, "Cache size must be greater than zero.");
00119 }
00120
00121 this->maxCacheSize = size;
00122 }
00123
00124 protected:
00125
00126 virtual bool removeEldestEntry(const MapEntry<K, V>& eldest DECAF_UNUSED) {
00127 if (this->size() > maxCacheSize) {
00128 return true;
00129 }
00130 return false;
00131 }
00132
00133 };
00134
00135 }}
00136
00137 #endif