@@ -0,0 +1,2261 @@
+//%0A// $Id: RecentList.java,v 1.1 2003/01/21 21:46:31 mdb Exp $%0A%0Apackage com.samskivert.util;%0A%0Aimport java.util.Arrays;%0A%0A/**%0A * An array list that maintains a list of the N most recent entries added%0A * to it.%0A */%0Apublic class RecentList%0A%7B%0A    /**%0A     * Constructs a recent list that keeps the specified number of most%0A     * recently added objects.%0A     */%0A    public RecentList (int keepCount)%0A    %7B%0A        _list = new Object%5BkeepCount%5D;%0A    %7D%0A%0A    /**%0A     * Returns the number of elements in this list.%0A     */%0A    public int size ()%0A    %7B%0A        return Math.min(_lastPos, _list.length);%0A    %7D%0A%0A    /**%0A     * Clears the list.%0A     */%0A    public void clear ()%0A    %7B%0A        Arrays.fill(_list, null);%0A        _lastPos = 0;%0A    %7D%0A%0A    /**%0A     * Adds the specified value to the list.%0A     */%0A    public void add (Object value)%0A    %7B%0A        _list%5B_lastPos++ %25 _list.length%5D = value;%0A        // keep last pos cycling from keepCount to 2*keepCount-1%0A        if (_lastPos == 2*_list.length) %7B%0A            _lastPos = _list.length;%0A        %7D%0A    %7D%0A%0A    /**%0A     * Returns true if the supplied value is equal (using %7B@link%0A     * Object#equals%7D) to any value in the list.%0A     */%0A    public boolean contains (Object value)%0A    %7B%0A        for (int ii = 0, ll = _list.length; ii %3C ll; ii++) %7B%0A            if (_list%5Bii%5D != null && _list%5Bii%5D.equals(value)) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    /**%0A     * Gets the Nth most recently added value from the list.%0A     * %3Ccode%3Enull%3C/code%3E is returned if the list does not contain at least%0A     * %3Ccode%3Eindex+1%3C/code%3E entries.%0A     */%0A    public Object get (int index)%0A    %7B%0A        return _list%5B(_lastPos-index-1+_list.length)%25_list.length%5D;%0A    %7D%0A%0A    public static void main (String%5B%5D args)%0A    %7B%0A        RecentList list = new RecentList(5);%0A        for (int ii = 0; ii %3C 10; ii++) %7B%0A            list.add(new Integer(ii));%0A        %7D%0A        System.out.println(%22Contains 3 %22 + list.contains(new Integer(3)));%0A        System.out.println(%22Contains 7 %22 + list.contains(new Integer(7)));%0A        for (int ii = 0; ii %3C list.size(); ii++) %7B%0A            System.out.println(ii + %22 =%3E %22 + list.get(ii));%0A        %7D%0A    %7D%0A%0A    protected Object%5B%5D _list;%0A    protected int _lastPos;%0A%7D%0A

A container for keeping a list of recent objects.