package org.openmole.commons.aspect.caching;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.collections15.map.ReferenceMap;

public class SoftMethodCache {

    final static Logger LOGGER = Logger.getLogger(SoftMethodCache.class.getName());
    final Map<Object, Map<String, Object>> cache = Collections.synchronizedMap(new WeakHashMap<Object, Map<String, Object>>());
  
    void putCachedMethodResult(Object object, String method, Object result) {
        LOGGER.log(Level.FINE, "Softcache size {0}", size());

        final Map<String, Object> methodMap = getMethodMap(object);
        methodMap.put(method, result);
    }

    Object getCachedMethodResult(Object object, String method) {
        Map<String, Object> methodMap = cache.get(object);
        if (methodMap == null) {
            return null;
        }

        return methodMap.get(method);
    }

    private Map<String, Object> getMethodMap(Object object) {
        Map<String, Object> methodMap = cache.get(object);
        if (methodMap != null) {
            return methodMap;
        }

        synchronized (cache) {
            methodMap = cache.get(object);
            if (methodMap == null) {
                methodMap = Collections.synchronizedMap(new ReferenceMap<String, Object>(ReferenceMap.HARD, ReferenceMap.SOFT));
                cache.put(object, methodMap);
            }
            return methodMap;
        }
    }

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

    void clear(Object obj) {
        cache.remove(obj);
    }
}

@@ -624,17 +624,45 @@
  %7B0%7D
-%22, size()
+ (%7B1%7D)%22, new Object%5B%5D%7Bsize(), method%7D
 );%0A%0A
@@ -836,24 +836,119 @@
 g method) %7B%0A
+        LOGGER.log(Level.FINE, %22Get softcached size %7B0%7D (%7B1%7D)%22, new Object%5B%5D%7Bsize(), method%7D);%0A
         Map%3C

Add log.