package com.webimageloader.loader;

import com.webimageloader.ImageLoader.Logger;
import com.webimageloader.util.Android;
import com.webimageloader.util.LruCache;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.util.Log;

public class MemoryCache {
    private static final String TAG = "MemoryLoader";

    public static class Entry {
        public final Bitmap bitmap;
        public final Metadata metadata;

        private Entry(Bitmap bitmap, Metadata metadata) {
            this.bitmap = bitmap;
            this.metadata = metadata;
        }
    }

    public static class DebugInfo {
        public final int hitCount;
        public final int missCount;
        public final int putCount;
        public final int evictionCount;
        public final int numImages;

        private DebugInfo(int hitCount, int missCount, int putCount, int evictionCount, int numImages) {
            this.hitCount = hitCount;
            this.missCount = missCount;
            this.putCount = putCount;
            this.evictionCount = evictionCount;
            this.numImages = numImages;
        }
    }

    private LruCache<String, Entry> cache;

    public MemoryCache(int maxSize) {
        cache = new BitmapCache(maxSize);
    }

    public int size() {
        return cache.size();
    }

    public int maxSize() {
        return cache.maxSize();
    }

    public void trimToSize(int maxSize) {
        cache.trimToSize(maxSize);
    }

    public void evictAll() {
        cache.evictAll();
    }

    public Entry get(LoaderRequest request) {
        Entry entry = cache.get(request.getCacheKey());
        if (entry != null) {
            if (Logger.VERBOSE) Log.v(TAG, "Loaded " + request + " from memory");
        }

        return entry;
    }

    public void put(LoaderRequest request, Bitmap b, Metadata metadata) {
        // Add the bitmap to the cache if we can fit at least six images of this size,
        // this way we avoid caching large images that will evict all other entries
        if (sizeOf(b) < cache.maxSize() / 6) {
            cache.put(request.getCacheKey(), new Entry(b, metadata));
        }
    }

    public DebugInfo getDebugInfo() {
        return new DebugInfo(cache.hitCount(), cache.missCount(), cache.putCount(), cache.evictionCount(), cache.snapshot().size());
    }

    @TargetApi(12)
    private static int sizeOf(Bitmap b) {
        if (Android.isAPI(12)) {
            return b.getByteCount();
        } else {
            return b.getRowBytes() * b.getHeight();
        }
    }

    private static class BitmapCache extends LruCache<String, Entry> {
        public BitmapCache(int maxSize) {
            super(maxSize);
        }

        @Override
        protected int sizeOf(String key, Entry value) {
            Bitmap b = value.bitmap;

            return MemoryCache.sizeOf(b);
        }
    }
}

@@ -252,16 +252,47 @@
 l.Log;%0A%0A
+import java.util.WeakHashMap;%0A%0A
 public c
@@ -1208,16 +1208,64 @@
 %3E cache;
+%0A    private WeakHashMap%3CString, Entry%3E expired;
 %0A%0A    pu
@@ -1330,32 +1330,84 @@
 Cache(maxSize);%0A
+        expired = new WeakHashMap%3CString, Entry%3E();%0A
     %7D%0A%0A    publi
@@ -1727,53 +1727,89 @@
     
-Entry entry = cache.get(request.getCacheKey()
+String cacheKey = request.getCacheKey();%0A        Entry entry = cache.get(cacheKey
 );%0A 
@@ -1919,32 +1919,280 @@
 ory%22);%0A        %7D
+ else %7B%0A            entry = expired.remove(cacheKey);%0A            if (entry != null) %7B%0A                cache.put(cacheKey, entry);%0A                if (Logger.VERBOSE) Log.v(TAG, %22Loaded %22 + request + %22 from expired memory%22);%0A            %7D%0A        %7D
 %0A%0A        return
@@ -2992,23 +2992,16 @@
 private 
-static 
 class Bi
@@ -3237,16 +3237,16 @@
 itmap;%0A%0A
-
         
@@ -3284,21 +3284,234 @@
 );%0A        %7D
+%0A%0A        @Override%0A        protected void entryRemoved(boolean evicted, String key, Entry oldValue, Entry newValue) %7B%0A            if (evicted) %7B%0A                expired.put(key, oldValue);%0A            %7D%0A        %7D
 %0A    %7D%0A%7D%0A

Store evicted bitmaps using WeakHashMap