1 package org.apache.oro.util; 2 3 59 60 import java.util.*; 61 62 81 public final class CacheFIFO2 extends GenericCache { 82 private int __current = 0; 83 private boolean[] __tryAgain; 84 85 90 public CacheFIFO2(int capacity) { 91 super(capacity); 92 93 __tryAgain = new boolean[_cache.length]; 94 } 95 96 97 103 public CacheFIFO2(){ 104 this(GenericCache.DEFAULT_CAPACITY); 105 } 106 107 108 public synchronized Object getElement(Object key) { 109 Object obj; 110 111 obj = _table.get(key); 112 113 if(obj != null) { 114 GenericCacheEntry entry; 115 116 entry = (GenericCacheEntry)obj; 117 118 __tryAgain[entry._index] = true; 119 return entry._value; 120 } 121 122 return null; 123 } 124 125 126 134 public final synchronized void addElement(Object key, Object value) { 135 int index; 136 Object obj; 137 138 obj = _table.get(key); 139 140 if(obj != null) { 141 GenericCacheEntry entry; 142 143 entry = (GenericCacheEntry)obj; 146 entry._value = value; 147 entry._key = key; 148 149 __tryAgain[entry._index] = true; 151 152 return; 153 } 154 155 if(!isFull()) { 157 index = _numEntries; 158 ++_numEntries; 159 } else { 160 index = __current; 162 163 while(__tryAgain[index]) { 164 __tryAgain[index] = false; 165 if(++index >= __tryAgain.length) 166 index = 0; 167 } 168 169 __current = index + 1; 170 if(__current >= _cache.length) 171 __current = 0; 172 173 _table.remove(_cache[index]._key); 174 } 175 176 _cache[index]._value = value; 177 _cache[index]._key = key; 178 _table.put(key, _cache[index]); 179 } 180 181 } 182 183 | Popular Tags |