/*
 *
 * Copyright (c) 2007
 * Adrian Michel
 * http://www.tradery.com
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Adrian Michel makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
*/


package com.tradery.collections;

import com.tradery.contract.Contract;

import java.util.Stack;

/**
 * A stack of integer values
 */
public class IntegerStack
{
  private Stack _stack = null;

  /**
   * Default constructor. Constructs an empty IntegerStack object
   */
  public IntegerStack()
  {
    _stack = new Stack();
  }

  /**
   * Pushes a new integer value on the stack
   * 
   * @param n      The value to be pushed on the stack
   */
  public void push( int n )
  {
    _stack.push( new Integer( n ) );
  }

  /**
   * Pops the value from the top of the stack and returns it
   * 
   * Note: no protection is provided for stack underflow, so the application
   * is responsible for preventing or handling this case.
   * 
   * @return The value previously at the top of the stack
   */
  public int pop()
  {
    return ((Integer)_stack.pop()).intValue();
  }

  /**
   * Returns the integer value at the top of the stack
   * 
   * @return The value at the top of the stack
   */
  public int peek()
  {
    return ((Integer)_stack.peek()).intValue();
  }

  /**
   * Replaces the value at the top of the stack with a new value. It is equivalent to popping the old value and
   * pushing the new one.
   * 
   * @param n      The new value to be put at the top of the stack.
   */
  public void setTop( int n )
  {
    _stack.setElementAt( new Integer( n ), _stack.size() - 1 );
  }
}