package fi.nls.oskari.cache;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Generic cache factory for Oskari.
 * TODO: make Cache implementation parametrizable via PropertyUtil. Same as logger and UserService.
 * Usage:
 * <pre>
 * {@code
 *  private Cache<String> cache = CacheManager.getCache("MyCacheName");
 *  final String response = getUrl("http://www.paikkatietoikkuna.fi");
 *
 *  public String getUrl(final String url) {
 *      String response = cache.get(propertyName);
 *      if(response != null) {
 *          return response;
 *      }
 *      response = IOHelper.getURL(url);
 *      cache.put(url, response);
 *      return response;
 *  }
 * }
 * </pre>
 */
public class CacheManager {

    private static ConcurrentMap<String, Cache> CACHE_STORE = new ConcurrentHashMap<String, Cache>();

    /**
     * Returns a cache matching name or creates one if it doesn't exist.
     *
     * @param name name of the cache
     * @param <T>  type mapping for cache
     * @return
     */
    public static <T> Cache<T> getCache(final String name) {
        final Cache existing = CACHE_STORE.get(name);
        if (existing != null) {
            return existing;
        }

        // create a new one
        final Cache<T> cache = new Cache<T>();
        cache.setName(name);
        CACHE_STORE.put(name, cache);
        return cache;
    }

    /**
     * Returns names of registered caches
     */
    public static Set<String> getCacheNames() {
        return CACHE_STORE.keySet();
    }

    /**
     * Registers cache manually to manager.
     * @return true if successful, false if params missing or a cache already exists with same name
     */
    public static boolean addCache(final String name, final Cache cache) {
        if (null == name || null == cache || CACHE_STORE.containsKey(name)) {
            return false;
        }
        cache.setName(name);
        CACHE_STORE.put(name, cache);
        return true;
    }

}

@@ -134,16 +134,52 @@
 rentMap;
+%0Aimport java.util.function.Supplier;
 %0A%0A/**%0A *
@@ -1158,24 +1158,189 @@
 ing name) %7B%0A
+        return getCache(name, () -%3E new Cache%3C%3E());%0A    %7D%0A%0A    public static %3CT%3E Cache%3CT%3E getCache(final String name, final Supplier%3C? extends Cache%3CT%3E%3E supplier) %7B%0A
         fina
@@ -1516,20 +1516,20 @@
 e = 
-new Cache%3CT%3E
+supplier.get
 ();%0A

Add method getCache(String name, Supplier<? extends Cache<T>>) to CacheManager