Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.util.LinkedHashMap;
   20   import java.util.Map;
   21   
   22   /**
   23    * A Simple LRU Cache
   24    * 
   25    * @version $Revision: 712224 $
   26    * @param <K>
   27    * @param <V>
   28    */
   29   
   30   public class LRUCache<K, V> extends LinkedHashMap<K, V> {
   31       private static final long serialVersionUID = -342098639681884413L;
   32       protected int maxCacheSize = 10000;
   33   
   34       /**
   35        * Default constructor for an LRU Cache The default capacity is 10000
   36        */
   37       public LRUCache() {
   38           this(0,10000, 0.75f, true);
   39       }
   40   
   41       /**
   42        * Constructs a LRUCache with a maximum capacity
   43        * 
   44        * @param maximumCacheSize
   45        */
   46       public LRUCache(int maximumCacheSize) {
   47           this(0, maximumCacheSize, 0.75f, true);
   48       }
   49   
   50       /**
   51        * Constructs an empty <tt>LRUCache</tt> instance with the specified
   52        * initial capacity, maximumCacheSize,load factor and ordering mode.
   53        * 
   54        * @param initialCapacity the initial capacity.
   55        * @param maximumCacheSize
   56        * @param loadFactor the load factor.
   57        * @param accessOrder the ordering mode - <tt>true</tt> for access-order,
   58        *                <tt>false</tt> for insertion-order.
   59        * @throws IllegalArgumentException if the initial capacity is negative or
   60        *                 the load factor is non-positive.
   61        */
   62   
   63       public LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, boolean accessOrder) {
   64           super(initialCapacity, loadFactor, accessOrder);
   65           this.maxCacheSize = maximumCacheSize;
   66       }
   67   
   68       /**
   69        * @return Returns the maxCacheSize.
   70        */
   71       public int getMaxCacheSize() {
   72           return maxCacheSize;
   73       }
   74   
   75       /**
   76        * @param maxCacheSize The maxCacheSize to set.
   77        */
   78       public void setMaxCacheSize(int maxCacheSize) {
   79           this.maxCacheSize = maxCacheSize;
   80       }
   81   
   82       protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
   83           return size() > maxCacheSize;
   84       }
   85   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]