view src/uk/ac/qmul/eecs/ccmi/utilities/Stack.java @ 1:66b3a838feca logging tip

Added logging of user interaction
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Tue, 12 Feb 2013 15:31:48 +0000
parents e0ee6ac3a45f
children
line wrap: on
line source
/*  
 CCmI Diagram Editor for Android 
  
 Copyright (C) 2012  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.utilities;

import java.util.ArrayList;

/**
 * 
 * The stack is an {@code ArrayList} with a simplified interface for pushing and popping elements 
 * on and from the top 
 *
 * @param <T> The type of objects contained in this stack
 */
public class Stack<T> extends ArrayList<T> {
	
	/**
	 * Create a stack with an initial capacity of {@code size}
	 *  
	 * @param size the initial capacity 
	 */
	public Stack(int size){
		super(size);
	}
	
	/**
	 * Returns the size of the stack 
	 * 
	 * @return the size of the stack
	 */
	@Override
	public int size(){
		return super.size();
	}
	
	/**
	 * Return the index of the top element for the stack 
	 * 
	 * @return index of the top element for the stack or -1 if the stack is empty
	 */
	public int level(){
		return size()-1;
	}
	
	/**
	 * Adds a new element at the top of the stack. Like in an {@code ArrayList}, 
	 * the same object can be inserted more than once in the stack
	 * 
	 * @param n the new element to add
	 */
	public void push(T n){
		add(n);
	}
	
	/**
	 * Removes the top element of the stack
	 * 
	 * @return the removed element or {@code null} if the stack is empty 
	 */ 
	public T pop(){
		if(size() == 0)
			return null;
		return remove(size()-1);
	}
	
	/**
	 * Returns a reference to the top element of the stack, the element is not removed 
	 * 
	 * @return the top element of the stack 
	 */
	public T current(){
		if(size() == 0)
			return null;
		return get(size()-1);
	}
	
	private static final long serialVersionUID = 1L;
}