001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.util;
018    
019    import java.util.LinkedHashMap;
020    import java.util.Map;
021    
022    /**
023     * A Simple LRU Cache
024     * 
025     * 
026     * @param <K>
027     * @param <V>
028     */
029    
030    public class LRUCache<K, V> extends LinkedHashMap<K, V> {
031        private static final long serialVersionUID = -342098639681884413L;
032        protected int maxCacheSize = 10000;
033    
034        /**
035         * Default constructor for an LRU Cache The default capacity is 10000
036         */
037        public LRUCache() {
038            this(0,10000, 0.75f, true);
039        }
040    
041        /**
042         * Constructs a LRUCache with a maximum capacity
043         * 
044         * @param maximumCacheSize
045         */
046        public LRUCache(int maximumCacheSize) {
047            this(0, maximumCacheSize, 0.75f, true);
048        }
049    
050        /**
051         * Constructs an empty <tt>LRUCache</tt> instance with the specified
052         * initial capacity, maximumCacheSize,load factor and ordering mode.
053         * 
054         * @param initialCapacity the initial capacity.
055         * @param maximumCacheSize
056         * @param loadFactor the load factor.
057         * @param accessOrder the ordering mode - <tt>true</tt> for access-order,
058         *                <tt>false</tt> for insertion-order.
059         * @throws IllegalArgumentException if the initial capacity is negative or
060         *                 the load factor is non-positive.
061         */
062    
063        public LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, boolean accessOrder) {
064            super(initialCapacity, loadFactor, accessOrder);
065            this.maxCacheSize = maximumCacheSize;
066        }
067    
068        /**
069         * @return Returns the maxCacheSize.
070         */
071        public int getMaxCacheSize() {
072            return maxCacheSize;
073        }
074    
075        /**
076         * @param maxCacheSize The maxCacheSize to set.
077         */
078        public void setMaxCacheSize(int maxCacheSize) {
079            this.maxCacheSize = maxCacheSize;
080        }
081    
082        protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
083            if( size() > maxCacheSize ) {
084                onCacheEviction(eldest);
085                return true;
086            }
087            return false;
088        }
089    
090        protected void onCacheEviction(Map.Entry<K,V> eldest) {
091        }
092    
093    }