comparison src/uk/ac/qmul/eecs/ccmi/utilities/Stack.java @ 0:e0ee6ac3a45f

first import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Thu, 13 Dec 2012 20:00:21 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e0ee6ac3a45f
1 /*
2 CCmI Diagram Editor for Android
3
4 Copyright (C) 2012 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package uk.ac.qmul.eecs.ccmi.utilities;
20
21 import java.util.ArrayList;
22
23 /**
24 *
25 * The stack is an {@code ArrayList} with a simplified interface for pushing and popping elements
26 * on and from the top
27 *
28 * @param <T> The type of objects contained in this stack
29 */
30 public class Stack<T> extends ArrayList<T> {
31
32 /**
33 * Create a stack with an initial capacity of {@code size}
34 *
35 * @param size the initial capacity
36 */
37 public Stack(int size){
38 super(size);
39 }
40
41 /**
42 * Returns the size of the stack
43 *
44 * @return the size of the stack
45 */
46 @Override
47 public int size(){
48 return super.size();
49 }
50
51 /**
52 * Return the index of the top element for the stack
53 *
54 * @return index of the top element for the stack or -1 if the stack is empty
55 */
56 public int level(){
57 return size()-1;
58 }
59
60 /**
61 * Adds a new element at the top of the stack. Like in an {@code ArrayList},
62 * the same object can be inserted more than once in the stack
63 *
64 * @param n the new element to add
65 */
66 public void push(T n){
67 add(n);
68 }
69
70 /**
71 * Removes the top element of the stack
72 *
73 * @return the removed element or {@code null} if the stack is empty
74 */
75 public T pop(){
76 if(size() == 0)
77 return null;
78 return remove(size()-1);
79 }
80
81 /**
82 * Returns a reference to the top element of the stack, the element is not removed
83 *
84 * @return the top element of the stack
85 */
86 public T current(){
87 if(size() == 0)
88 return null;
89 return get(size()-1);
90 }
91
92 private static final long serialVersionUID = 1L;
93 }