fiore@0: /* fiore@0: CCmI Diagram Editor for Android fiore@0: fiore@0: Copyright (C) 2012 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: package uk.ac.qmul.eecs.ccmi.utilities; fiore@0: fiore@0: import java.util.ArrayList; fiore@0: fiore@0: /** fiore@0: * fiore@0: * The stack is an {@code ArrayList} with a simplified interface for pushing and popping elements fiore@0: * on and from the top fiore@0: * fiore@0: * @param The type of objects contained in this stack fiore@0: */ fiore@0: public class Stack extends ArrayList { fiore@0: fiore@0: /** fiore@0: * Create a stack with an initial capacity of {@code size} fiore@0: * fiore@0: * @param size the initial capacity fiore@0: */ fiore@0: public Stack(int size){ fiore@0: super(size); fiore@0: } fiore@0: fiore@0: /** fiore@0: * Returns the size of the stack fiore@0: * fiore@0: * @return the size of the stack fiore@0: */ fiore@0: @Override fiore@0: public int size(){ fiore@0: return super.size(); fiore@0: } fiore@0: fiore@0: /** fiore@0: * Return the index of the top element for the stack fiore@0: * fiore@0: * @return index of the top element for the stack or -1 if the stack is empty fiore@0: */ fiore@0: public int level(){ fiore@0: return size()-1; fiore@0: } fiore@0: fiore@0: /** fiore@0: * Adds a new element at the top of the stack. Like in an {@code ArrayList}, fiore@0: * the same object can be inserted more than once in the stack fiore@0: * fiore@0: * @param n the new element to add fiore@0: */ fiore@0: public void push(T n){ fiore@0: add(n); fiore@0: } fiore@0: fiore@0: /** fiore@0: * Removes the top element of the stack fiore@0: * fiore@0: * @return the removed element or {@code null} if the stack is empty fiore@0: */ fiore@0: public T pop(){ fiore@0: if(size() == 0) fiore@0: return null; fiore@0: return remove(size()-1); fiore@0: } fiore@0: fiore@0: /** fiore@0: * Returns a reference to the top element of the stack, the element is not removed fiore@0: * fiore@0: * @return the top element of the stack fiore@0: */ fiore@0: public T current(){ fiore@0: if(size() == 0) fiore@0: return null; fiore@0: return get(size()-1); fiore@0: } fiore@0: fiore@0: private static final long serialVersionUID = 1L; fiore@0: }