001    package org.rakeshv.utils;
002    
003    import java.util.Map;
004    import java.util.Map.Entry;
005    
006    /**
007     * A <i><b>L</b>east <b>R</b>ecently <b>U</b>sed</i> implementation 
008     * using a <code>LinkedHashMap</code>.
009     *
010     * <p><b>Note:</b> This class uses the LinkedHashMap introducted in
011     * the Java 1.4 version, and replaces the old implementation that has
012     * been renamed as {@link SimpleLRU}.</p>
013     *
014     * <p>Copyright 2004-2006 Rakesh Vidyadharan</p>
015     * @author Rakesh Vidyadharan 2004 May 2
016     * @version $Id: LRU.java,v 1.9 2006/03/11 04:15:14 rakesh Exp $
017     */
018    class LRU<K,V> extends LinkedHashMap<K,V>
019    {
020      /**
021       * Default constructor.  Creates a new Map with {@link 
022       * #DEFAULT_CAPACITY} capacity and a load factor of {@link
023       * #DEFAULT_LOAD_FACTOR}.  If this form of the constructor is used,
024       * then the cache size will <b>not</b> be controlled.
025       *
026       * @see #LRU( int, float )
027       */
028      LRU()
029      {
030        this( LinkedHashMap.DEFAULT_CAPACITY, 
031            LinkedHashMap.DEFAULT_LOAD_FACTOR );
032        setMaxEntries( Integer.MAX_VALUE );
033      }
034    
035      /**
036       * Constructs an empty access-ordered instance with the specified 
037       * initial capacity and a default load factor of {@link
038       * #DEFAULT_LOAD_FACTOR}.
039       *
040       * @see #LRU( int, float )
041       * @param capacity The maximum size for this map.
042       */
043      LRU( int capacity )
044      {
045        this( capacity, LinkedHashMap.DEFAULT_LOAD_FACTOR );
046      }
047    
048      /**
049       * Constructs an empty access-ordered instance with the specified 
050       * initial capacity and load factor.  This is the designated
051       * constructor that is used by all other constructors.
052       *
053       * @param capacity The maximum size for this map.
054       * @param loadFactor The load-factor to use.
055       */
056      LRU( int capacity, float loadFactor )
057      {
058        super( capacity + 1, loadFactor, LinkedHashMap.LRU_CACHE );
059        setMaxEntries( capacity );
060      }
061    
062      /**
063       * Constructs an access-ordered LRU instance with the same 
064       * mappings as the specified map. The LRU instance is 
065       * created with a a default load factor of {@link
066       * #DEFAULT_LOAD_FACTOR} and a capacity sufficient to hold the 
067       * mappings in the specified map.
068       *
069       * @see #LRU( int )
070       * @param map The map to use to create the new instance.
071       * @throws NullPointerException If the specified map is null.
072       */
073      LRU( Map<K,V> map )
074      {
075        this( map.size() );
076    
077        if ( map == null )
078        {
079          throw new NullPointerException( 
080              "Null map specified in call to LRU( Map )" );
081        }
082    
083        this.putAll( map );
084      }
085    }