KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Stack JavaDoc;
10
11 /**
12  * Extension of the Stack class, which has a limited depth (FIFO stack). This class doesn't allow duplicates.
13  *
14  * @author Laurent Etiemble
15  * @version $Revision: 1.7 $
16  */

17 public class LimitedStack extends Stack JavaDoc
18 {
19    /** Maximum number elements allowed in the stack */
20    protected int maximumSize;
21
22
23    /** Constructor for the LimitedStack object */
24    public LimitedStack()
25    {
26       this(10);
27    }
28
29
30    /**
31     * Constructor for the LimitedStack object
32     *
33     * @param size Depth size of the stack
34     */

35    public LimitedStack(int size)
36    {
37       this.maximumSize = size;
38    }
39
40
41    /**
42     * Overrides the push method to limit the number of elements.
43     *
44     * @param item Item to push onto the stack
45     * @return The item pushed
46     */

47    public Object JavaDoc push(Object JavaDoc item)
48    {
49       // If size is exceeded, remove the first in
50
if (this.size() >= this.maximumSize)
51       {
52          this.remove(0);
53       }
54
55       // Push the item
56
return super.push(item);
57    }
58 }
59
60
Popular Tags