/**
 * AnyServer -- Praktikum Experimentierkasten -- ADS
 *
 * @author E. Mumprecht
 * @version 1.0 -- Geruest fuer irgendeinen Server
 */
package ch.zhaw.ads;

public class ListStack<T> implements CommandExecutor {

    @Override
    public String execute(String command) {
        StringBuffer result = new StringBuffer(100);
        result.append("Die Eingabe war \"");
        result.append(command);
        result.append("\"\n");
        return result.toString();
    }

    Object[] data;
    private int top;
    private int capacity;
    private static final int DEFAULT_CAPACITY = 100;

    public ListStack() {
        this(DEFAULT_CAPACITY);
    }

    public ListStack(int capacity) {
        this.capacity= capacity;
        removeAll();
    }

    public void removeAll() {
        data = new Object[capacity];
        top = 0;
    }

    public void push(Object x) {
    // public void push(Object x) throws StackOverflowException {
    //     if (isFull()){
    //         throw new StackOverflowException();
    //     }
        data[top] = x;
        top++;
    }

    public Object pop() {
        if (isEmpty()){
            return null;
        }
        top--;
        Object topItem= data [top];
        data [top] = null;
        return topItem;
    }

    public Object peek() {
        if (isEmpty()){
            return null;
        }
        return data [top-1];
    }

    public boolean isEmpty() {
        return (top == 0);
    }

    public boolean isFull() {
        return top == data.length;
    }
}
