/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper.cache.algorithm;
import java.util.LinkedHashMap;
import java.util.Map;
public class LRU implements ReplacementAlgorithm {
public static final String ALGORITHM_NAME = "LRU";
public LRU(int cacheSize) {
if (cacheSize <= 0) {
throw new IllegalArgumentException("cacheSize must be larger than 0");
} else {
this.sizeBoundary = cacheSize;
this.map = new LinkedHashMap(cacheSize + 1, 1, true) {
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > sizeBoundary;
}
};
}
}
public int getSizeBoundary() {
return this.sizeBoundary;
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public Object put(Object key, Object value) {
return map.put(key, value);
}
public Object get(Object key) {
return map.get(key);
}
public Object remove(Object key) {
return map.remove(key);
}
public void clear() {
map.clear();
}
public int size() {
return map.size();
}
private final int sizeBoundary;
private LinkedHashMap map;
}