package basictype;
/**
 * A stack is an ordered collection of items.
 * You can push items onto the stack, 
 * pop items from the top of the stack, and get its size.
 * 
 * You can also get an iterator on its elements.
 * 
 * @author Frederic Boulanger frederic.boulanger@supelec.fr
 *
 * @param <T> the type of elements in the stack
 * 
 * In the specification, we split the pop method into a top() 
 * operation which yields the top of the stack, and a pop() 
 * operation which removes it from the stack.
 * 
 * @spec
 * Stack : -> Stack
 * push : Stack, Item -> Stack
 * pop : Stack -> Stack
 * top : Stack -> Item
 * size : Stack -> Nat
 * 
 * pre(pop(s)) = size(s) > 0
 * pre(top(s)) = size(s) > 0
 * 
 * size(Stack) = 0
 * size(push(s,i)) = succ(size(s))
 * pop(push(s,i)) = s
 * top(push(s,i)) = i
 * 
 */
public abstract class Stack<T> implements Iterable<T> {
  public abstract void push(T item);
  public abstract T pop();
  public abstract T top();
  public abstract int size();
  @Override
  public String toString() {
    StringBuffer buf = new StringBuffer(":");
    boolean first = true;
    for (T item : this) {
      if (first) {
        first = false;
      } else {
        buf.append(", ");
      }
      buf.append(item.toString());
    }
    buf.append("|");
    return buf.toString();
  }
}
