package fi.nls.oskari.cache;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

public class ComputeOnceCache<T> {

    private static final long EXPIRATION_DEFAULT = TimeUnit.MINUTES.toMillis(30);

    private final Cache<T> cache;
    private final ConcurrentHashMap<String, T> tmp;
    
    public ComputeOnceCache(int limit) {
        this(limit, EXPIRATION_DEFAULT);
    }

    public ComputeOnceCache(int limit, long expiration) {
        cache = new Cache<>();
        cache.setLimit(limit);
        cache.setExpiration(expiration);
        tmp = new ConcurrentHashMap<>();
    }

    public T get(final String key, final Function<String, T> mappingFunction) {
        T value = cache.get(key);
        if (value != null) {
            return value;
        }

        final AtomicBoolean b = new AtomicBoolean(false);
        value = tmp.computeIfAbsent(key, (String k) -> {
            // Re-check the cache - maybe someone just completed this
            // and executed the if (b.get()) {}-block after we
            // had already finished the first cache.get(key) call;
            T val = cache.get(k);
            if (val != null) {
                return val;
            }
            b.set(true);
            return mappingFunction.apply(k);
        });

        if (b.get()) {
            // I was the one to do the computation
            // Add the value to the actual cache
            cache.put(key, value);
            // And remove the value from the computation map
            tmp.remove(key);
            // Do this after and not within the computeIfAbsent() call since
            // we're not supposed to modify the underlying map within computeIfAbsent()
        }

        return value;
    }

}

@@ -227,19 +227,36 @@
 Cache%3CT%3E
+ extends Cache%3CT%3E
  %7B%0A
-
 %0A    pri
@@ -335,42 +335,8 @@
 );%0A%0A
-    private final Cache%3CT%3E cache;%0A
     
@@ -387,13 +387,9 @@
 mp;%0A
-    
 %0A
+
     
@@ -543,45 +543,8 @@
     
-cache = new Cache%3C%3E();%0A        cache.
 setL
@@ -564,22 +564,16 @@
         
-cache.
 setExpir
@@ -737,21 +737,21 @@
 value = 
-cache
+super
 .get(key
@@ -1154,21 +1154,21 @@
 T val = 
-cache
+super
 .get(k);
@@ -1459,21 +1459,21 @@
         
-cache
+super
 .put(key

Make ComputeOnceCache extend Cache