panda.lang.collection.LRUMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.lang.collection;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* A simple LRU cache that implements the Map
interface. Instances
* are not thread-safe and should be synchronized externally, for instance by
* using {@link java.util.Collections#synchronizedMap}.
*
* @param the key type
* @param the value type
*/
public class LRUMap extends LinkedHashMap implements Serializable {
private static final long serialVersionUID = 1L;
private final int maxEntries;
public LRUMap(int maxEntries) {
super( maxEntries, .75f, true );
this.maxEntries = maxEntries;
}
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return ( size() > maxEntries );
}
}