1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
| 1
16 17 package android.support.v4.util; 18 19 import java.util.LinkedHashMap; 20 import java.util.Map; 21 22
29 public class LruCache<K, V> { 30 private final LinkedHashMap<K, V> map; 31 32 33 private int size; 34 private int maxSize; 35 36 private int putCount; 37 private int createCount; 38 private int evictionCount; 39 private int hitCount; 40 private int missCount; 41 42
47 public LruCache(int maxSize) { 48 if (maxSize <= 0) { 49 throw new IllegalArgumentException("maxSize <= 0"); 50 } 51 this.maxSize = maxSize; 52 53 this.map = new LinkedHashMap<K, V>(0, 0.75f, true); 54 } 55 56
64 public final V get(K key) { 65 if (key == null) { 66 throw new NullPointerException("key == null"); 67 } 68 69 V mapValue; 70 synchronized (this) { 71 mapValue = map.get(key); 72 if (mapValue != null) { 73 74 hitCount++; 75 return mapValue; 76 } 77 missCount++; 78 } 79 80
89 V createdValue = create(key); 90 if (createdValue == null) { 91 return null; 92 } 93 94 synchronized (this) { 95 createCount++; 96 97 mapValue = map.put(key, createdValue); 98 if (mapValue != null) { 99 100 101 map.put(key, mapValue); 102 } else { 103 104 size += safeSizeOf(key, createdValue); 105 } 106 } 107 108 if (mapValue != null) { 109 entryRemoved(false, key, createdValue, mapValue); 110 return mapValue; 111 } else { 112 113 trimToSize(maxSize); 114 return createdValue; 115 } 116 } 117 118
124 public final V put(K key, V value) { 125 if (key == null || value == null) { 126 throw new NullPointerException("key == null || value == null"); 127 } 128 129 V previous; 130 synchronized (this) { 131 putCount++; 132 size += safeSizeOf(key, value); 133 previous = map.put(key, value); 134 if (previous != null) { 135 136 size -= safeSizeOf(key, previous); 137 } 138 } 139 140 if (previous != null) { 141 entryRemoved(false, key, previous, value); 142 } 143 144 trimToSize(maxSize); 145 return previous; 146 } 147 148
153 private void trimToSize(int maxSize) { 154 while (true) { 155 K key; 156 V value; 157 synchronized (this) { 158 if (size < 0 || (map.isEmpty() && size != 0)) { 159 throw new IllegalStateException(getClass().getName() 160 + ".sizeOf() is reporting inconsistent results!"); 161 } 162 163 if (size <= maxSize || map.isEmpty()) { 164 break; 165 } 166 167 Map.Entry<K, V> toEvict = map.entrySet().iterator().next(); 168 key = toEvict.getKey(); 169 value = toEvict.getValue(); 170 map.remove(key); 171 size -= safeSizeOf(key, value); 172 evictionCount++; 173 } 174 175 entryRemoved(true, key, value, null); 176 } 177 } 178 179
185 public final V remove(K key) { 186 if (key == null) { 187 throw new NullPointerException("key == null"); 188 } 189 190 V previous; 191 synchronized (this) { 192 previous = map.remove(key); 193 if (previous != null) { 194 size -= safeSizeOf(key, previous); 195 } 196 } 197 198 if (previous != null) { 199 entryRemoved(false, key, previous, null); 200 } 201 202 return previous; 203 } 204 205
220 protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {} 221 222
237 protected V create(K key) { 238 return null; 239 } 240 241 private int safeSizeOf(K key, V value) { 242 int result = sizeOf(key, value); 243 if (result < 0) { 244 throw new IllegalStateException("Negative size: " + key + "=" + value); 245 } 246 return result; 247 } 248 249
266 protected int sizeOf(K key, V value) { 267 return 1; 268 } 269 270
274 public final void evictAll() { 275 trimToSize(-1); 276 } 277 278
283 public synchronized final int size() { 284 return size; 285 } 286 287
292 public synchronized final int maxSize() { 293 return maxSize; 294 } 295 296
299 public synchronized final int hitCount() { 300 return hitCount; 301 } 302 303
307 public synchronized final int missCount() { 308 return missCount; 309 } 310 311
314 public synchronized final int createCount() { 315 return createCount; 316 } 317 318
321 public synchronized final int putCount() { 322 return putCount; 323 } 324 325
328 public synchronized final int evictionCount() { 329 return evictionCount; 330 } 331 332
336 public synchronized final Map<K, V> snapshot() { 337 return new LinkedHashMap<K, V>(map); 338 } 339 340 @Override public synchronized final String toString() { 341 int accesses = hitCount + missCount; 342 int hitPercent = accesses != 0 ? (100 * hitCount / accesses) : 0; 343 return String.format("LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]", 344 maxSize, hitCount, missCount, hitPercent); 345 } 346 }
|