587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.internaldata.ant.listener;
|
|
19 |
|
|
20 |
import java.util.Stack;
|
|
21 |
/**
|
|
22 |
* Object to keep the elements in a stack, it supports both pop and peek (return element is kept in the stack).
|
|
23 |
*
|
|
24 |
* @param <E> The type applied to the stack.
|
|
25 |
*/
|
|
26 |
public class EndLessStack<E> extends Stack<E> {
|
|
27 |
public static final long serialVersionUID = -1L;
|
|
28 |
private E defaultElement;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Create a stack with a defaultElement as the default element.
|
|
32 |
* @param defaultElement The default element to use.
|
|
33 |
*/
|
|
34 |
public EndLessStack(E defaultElement) {
|
|
35 |
super();
|
|
36 |
this.defaultElement = defaultElement;
|
|
37 |
}
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Create a stack with a null default element.
|
|
41 |
*/
|
|
42 |
public EndLessStack() {
|
|
43 |
super();
|
|
44 |
this.defaultElement = null;
|
|
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* This return the top most element out from the stack.
|
|
49 |
* If the stack is empty it returns the defaultElement.
|
|
50 |
* @return element
|
|
51 |
*/
|
|
52 |
public E pop() {
|
|
53 |
E element = super.pop();
|
628
|
54 |
if (element != null) {
|
587
|
55 |
return element;
|
628
|
56 |
}
|
587
|
57 |
return defaultElement;
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Endless stack is never empty.
|
|
62 |
*/
|
|
63 |
public boolean empty() {
|
|
64 |
return false;
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* This return the top most element from the stack.
|
|
69 |
* If the stack is empty it returns the defaultElement.
|
|
70 |
* The return element is kept in the stack.
|
|
71 |
* @return element
|
|
72 |
*/
|
|
73 |
public E peek() {
|
|
74 |
if (!super.empty()) {
|
|
75 |
E element = super.peek();
|
|
76 |
return element;
|
|
77 |
}
|
|
78 |
return defaultElement;
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Get default element.
|
|
83 |
* This is the object which will get returned if the internal stack gets empty.
|
|
84 |
* @return element
|
|
85 |
*/
|
|
86 |
public Object getDefaultElement() {
|
|
87 |
return defaultElement;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Set default element.
|
|
92 |
* @return element
|
|
93 |
*/
|
|
94 |
public void setDefaultElement(E defaultElement) {
|
|
95 |
this.defaultElement = defaultElement;
|
|
96 |
}
|
|
97 |
|
|
98 |
}
|