package org.hanuna.gitalk.common;

import java.util.HashMap;
import java.util.Map;

/**
 * @author erokhins
 */
public class CacheGet<K, V> implements Get<K, V>{
    private final Get<K, V> getFunction;
    private final int size;
    private Map<K, V> map;
    private Map<K, V> moreMap;

    public CacheGet(Get<K, V> getFunction, int size) {
        this.getFunction = getFunction;
        this.size = size;
        this.map = new HashMap<K, V>(size);
        this.moreMap = new HashMap<K, V>(size);
    }

    @Override
    public V get(K key) {
        V value = moreMap.get(key);
        if (value != null) {
            return value;
        } else {
            value = getFunction.get(key);
            moreMap.put(key, value);
            map.put(key, value);
            return value;
        }
    }

    private void checkSize() {
        if (map.size() >= size) {
            moreMap = map;
            map = new HashMap<K, V>(size);
        }
    }
}

@@ -433,32 +433,36 @@
 w HashMap%3CK, V%3E(
+2 * 
 size);%0A        t
@@ -485,32 +485,36 @@
 w HashMap%3CK, V%3E(
+2 * 
 size);%0A    %7D%0A%0A  
@@ -763,32 +763,57 @@
 ut(key, value);%0A
+            checkSize();%0A
             retu
@@ -896,32 +896,73 @@
 ze() %3E= size) %7B%0A
+            Map%3CK, V%3E tempMap = moreMap;%0A
             more
@@ -994,30 +994,39 @@
 p = 
-new HashMap%3CK, V%3E(size
+tempMap;%0A            map.clear(
 );%0A 

fix CacheGet periodical clear