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