|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| KeyLimitedStack.java | 100% | 85,7% | 66,7% | 85,7% |
|
||||||||||||||
| 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 | 0 |
public KeyLimitedStack()
|
| 20 |
{
|
|
| 21 | 0 |
super();
|
| 22 |
} |
|
| 23 |
|
|
| 24 |
|
|
| 25 |
/**
|
|
| 26 |
* Constructor for KeyLimitedStack.
|
|
| 27 |
*
|
|
| 28 |
* @param size
|
|
| 29 |
*/
|
|
| 30 | 16 |
public KeyLimitedStack(int size) |
| 31 |
{
|
|
| 32 | 16 |
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 | 128 |
public Object push(Object item)
|
| 43 |
{
|
|
| 44 |
// Remove duplicate
|
|
| 45 | 128 |
if (this.contains(item)) |
| 46 |
{
|
|
| 47 | 24 |
this.remove(this.indexOf(item)); |
| 48 |
} |
|
| 49 |
else
|
|
| 50 |
{
|
|
| 51 |
// If size is exceeded, remove the first in
|
|
| 52 | 104 |
if (this.size() >= this.maximumSize) |
| 53 |
{
|
|
| 54 | 16 |
this.remove(0);
|
| 55 |
} |
|
| 56 |
} |
|
| 57 |
|
|
| 58 |
// Push the item
|
|
| 59 | 128 |
return super.push(item); |
| 60 |
} |
|
| 61 |
} |
|
| 62 |
|
|
||||||||||