/* Copyright (c) 2008, Insomnia 24/7 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer. Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions and
 the following disclaimer in the documentation and/or other materials
 provided with the distribution. Neither the name of Insomnia 24/7 nor
 the names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 DAMAGE.
*/
 
/**
 * 
 * @author Cool Fire
 * @param If no type is define using <> brackets, any Java object can be added. However, a typecast might be needed to get the object from the stack again.
 */
public class UberStack <T>{
 
    private int pointer = 0;
    private T stack[];
 
    /**
     * If no stack size is passed to the constructor, a default stack size of 20 is used. 
     */
    public UberStack(){
        stack = (T[]) (new Object[20]);
    }
 
    /**
     * Create a new stack.
     * @param size of the stack.
     */
    public UberStack(int size){
        stack = (T[]) (new Object[size]);
    }
 
    /**
     * @return the element at the top of the stack.
     */
    public T pop() {
        if (pointer != 0) {
            pointer--;
            return stack[pointer];
        } else {
            return null;
        }
    }
 
    /**
     * @return the element at the top of the stack WHITHOUT removing it. 
     */
    public T peek() {
        if (pointer != 0) {
            return stack[pointer-1];
        } else {
            return null;
        }
    }
 
    /**
     * @param the number of the element you want returned.
     * @return the element at the specified position of the stack WHITHOUT removing it. 
     */
    public T peek(int n) {
        if (pointer > 0 && pointer < n) {
            return stack[n];
        } else {
            return null;
        }
    }
 
     /**
     * This method is used to return elements of the queue, even if they have already been pop'ed or the stack was emptied.
     * @param the number of the element you want returned.
     * @return the element at the specified position of the stack WHITHOUT removing it. 
     */
    public T forcePeek(int n) throws RuntimeException {
        if(n < 0){
            throw new java.lang.NegativeArraySizeException();
        } else if (stack[n] == null){
            throw new java.lang.NullPointerException();
        } else{
            return stack[n];
        }
    }
 
    /** 
     * Puts a new element on the stack.
     *  @param Object element to be added.
     */
    public void push(T var) {
        if(pointer <= stack.length) {
            stack[pointer] = var;
            pointer++;
        }
    }
 
    /** 
     * @return the position of the stack pointer.
     */
    public int getPointer() {
        return pointer;
    }
 
     /** 
     * @return the size of the current stack.
     */
    public int size() {
        return pointer;
    }
 
    /**
     * Resets the stack pointer, making the stack appear empty, the objects still exist
     */
    public void empty() {
        pointer = 0;
    }
 
    public boolean isEmpty() {
        return(pointer == 0);
    }
 
}