001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.turbo.lru;
015
016import java.util.ArrayList;
017import java.util.LinkedHashMap;
018import java.util.List;
019import java.util.Map;
020
021/**
022 * An lru cache based on Java's LinkedHashMap.
023 * 
024 * @author Ceki Gulcu
025 *
026 * @param <K>
027 * @param <V>
028 */
029public class X_LRUCache<K, V> extends LinkedHashMap<K, V> {
030    private static final long serialVersionUID = -6592964689843698200L;
031
032    final int cacheSize;
033
034    public X_LRUCache(int cacheSize) {
035        super((int) (cacheSize * (4.0f / 3)), 0.75f, true);
036        if (cacheSize < 1) {
037            throw new IllegalArgumentException("Cache size cannnot be smaller than 1");
038        }
039        this.cacheSize = cacheSize;
040    }
041
042    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
043        return (size() > cacheSize);
044    }
045
046    List<K> keyList() {
047        ArrayList<K> al = new ArrayList<K>();
048        al.addAll(keySet());
049        return al;
050    }
051}