KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > util > KeyLimitedStack


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.util;
8
9
10 /**
11  * Extension of the Limited Stack class, which doesn't allow duplicates.
12  *
13  * @author Laurent Etiemble
14  * @version $Revision: 1.7 $
15  */

16 public class KeyLimitedStack extends LimitedStack
17 {
18    /** Constructor for KeyLimitedStack. */
19    public KeyLimitedStack()
20    {
21       super();
22    }
23
24
25    /**
26     * Constructor for KeyLimitedStack.
27     *
28     * @param size
29     */

30    public KeyLimitedStack(int size)
31    {
32       super(size);
33    }
34
35
36    /**
37     * Overrides the push method to limit the number of elements and skip duplicates.
38     *
39     * @param item Item to push onto the stack
40     * @return The item pushed
41     */

42    public Object JavaDoc push(Object JavaDoc item)
43    {
44       // Remove duplicate
45
if (this.contains(item))
46       {
47          this.remove(this.indexOf(item));
48       }
49       else
50       {
51          // If size is exceeded, remove the first in
52
if (this.size() >= this.maximumSize)
53          {
54             this.remove(0);
55          }
56       }
57
58       // Push the item
59
return super.push(item);
60    }
61 }
62
Popular Tags