package org.rabix.engine.lru;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache<K, V> {

  private Map<K, V> cache;
  private int DEFAULT_CACHE_SIZE = 16;
  private int cacheSize;
  private String cacheName;

  public LRUCache(String cacheName) {
    this.cacheName = cacheName;
    this.cacheSize = DEFAULT_CACHE_SIZE;
    cache = new LinkedHashMap<K, V>(DEFAULT_CACHE_SIZE, 1, true);
  }

  public LRUCache(String cacheName, int cacheSize) {
    this.cacheName = cacheName;
    this.cacheSize = cacheSize;
    cache = new LinkedHashMap<K, V>(cacheSize, 1, true);
  }

  public String getCacheName() {
    return cacheName;
  }
  
  public int getCacheSize() {
    return cacheSize;
  }

  public V get(K key) {
    return cache.get(key) != null ? cache.get(key) : null;
  }

  public void put(K key, V val) {
    if (cacheFull()) {
      remove();
    }
    cache.put(key, val);
  }
  
  public int size() {
    return cache.size();
  }

  private boolean cacheFull() {
    return cache.size() == cacheSize;
  }
  
  private void remove() {
    Iterator<Map.Entry<K, V>> it = cache.entrySet().iterator();
    Map.Entry<K, V> remove = it.next();
    cache.remove(remove.getKey(), remove.getValue());
  }
  
  public String toString() {
    StringBuffer result =new StringBuffer("Cache: \n");  
    for(Map.Entry<K, V> entry : cache.entrySet()) {
      result.append(entry.getKey() + ": " + entry.getValue() + "\n");
    }
    return result.toString();
  }
  
  
  public static void main(String[] args) {
    LRUCache<String, Integer> cache = new LRUCache<String, Integer>("Test", 8);
    cache.put("1", 1);
    cache.put("2", 2);
    cache.put("3", 3);
    cache.put("4", 4);
    cache.get("2");
    cache.get("3");
    cache.get("4");
    cache.get("2");
    cache.put("5", 5);
    System.out.println(cache);
  }
  
}

@@ -1512,365 +1512,6 @@
 %0A  %0A
-  %0A  public static void main(String%5B%5D args) %7B%0A    LRUCache%3CString, Integer%3E cache = new LRUCache%3CString, Integer%3E(%22Test%22, 8);%0A    cache.put(%221%22, 1);%0A    cache.put(%222%22, 2);%0A    cache.put(%223%22, 3);%0A    cache.put(%224%22, 4);%0A    cache.get(%222%22);%0A    cache.get(%223%22);%0A    cache.get(%224%22);%0A    cache.get(%222%22);%0A    cache.put(%225%22, 5);%0A    System.out.println(cache);%0A  %7D%0A  %0A
 %7D%0A

remove test