001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2009-2010 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.util;
011
012import java.util.ArrayList;
013import java.util.EmptyStackException;
014
015/**
016 * Stack implementation based on {@link java.util.ArrayList}.
017 *
018 * <p>More efficient than {@link java.util.Stack}, which extends
019 * {@link java.util.Vector} and is
020 * therefore synchronized whether you like it or not.
021 *
022 * @param <E> Element type
023 *
024 * @author jhyde
025 */
026public class ArrayStack<E> extends ArrayList<E> {
027    /**
028     * Default constructor.
029     */
030    public ArrayStack() {
031        super();
032    }
033
034    /**
035     * Copy Constructor
036     * @param toCopy Instance of {@link ArrayStack} to copy.
037     */
038    public ArrayStack(ArrayStack<E> toCopy) {
039        super();
040        this.addAll(toCopy);
041    }
042
043    /**
044     * Analogous to {@link java.util.Stack#push}.
045     */
046    public E push(E item) {
047        add(item);
048        return item;
049    }
050
051    /**
052     * Analogous to {@link java.util.Stack#pop}.
053     */
054    public E pop() {
055        int len = size();
056        E obj = peek();
057        remove(len - 1);
058        return obj;
059    }
060
061    /**
062     * Analogous to {@link java.util.Stack#peek}.
063     */
064    public E peek() {
065        int len = size();
066        if (len <= 0) {
067            throw new EmptyStackException();
068        }
069        return get(len - 1);
070    }
071}
072
073// End ArrayStack.java