Browse files

adding iterator hasnext() check, fixes issue #75

  • Loading branch information...
1 parent 37c7213 commit 098776060e6245a7553ac2db64881c22a4322b54 @ouchadam ouchadam committed Jan 15, 2013
Showing with 14 additions and 11 deletions.
  1. +14 −11 core/src/main/java/com/novoda/imageloader/core/cache/util/LruCache.java
View
25 core/src/main/java/com/novoda/imageloader/core/cache/util/LruCache.java
@@ -167,8 +167,8 @@ public final V put(K key, V value) {
*/
private void trimToSize(int maxSize) {
while (true) {
- K key;
- V value;
+ K key = null;
+ V value = null;
synchronized (this) {
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!");
@@ -178,16 +178,19 @@ private void trimToSize(int maxSize) {
break;
}
// Change
- Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
- if (toEvict == null) {
- break;
- }
- key = toEvict.getKey();
- value = toEvict.getValue();
- map.remove(key);
- size -= safeSizeOf(key, value);
- evictionCount++;
+ if (map.entrySet().iterator().hasNext()) {
+ Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
+ if (toEvict == null) {
+ break;
+ }
+
+ key = toEvict.getKey();
+ value = toEvict.getValue();
+ map.remove(key);
+ size -= safeSizeOf(key, value);
+ evictionCount++;
+ }
}
entryRemoved(true, key, value, null);

0 comments on commit 0987760

Please sign in to comment.