/*
 *
 * 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 java.util.Vector;
import java.util.Enumeration;
import java.io.OutputStream;
import java.io.IOException;

import com.tradery.contract.Contract;

/**
 * A vector of integer values
 */
public class IntegerVector extends Vector
{
  /**
   * Default constructor. Constructs an empty IntegerVector
   */
  public IntegerVector() { super(); }
  
  /**
   * Constructs an IntegerVector object from an IntegerSet, 
   * by copying all the values in the set into the vector
   * 
   * @param set    The set containing the source for the integer values
   */
  public IntegerVector( IntegerSet set )
  {
    if( Contract.REQUIRE )
      Contract.require( set != null );
    for( Enumeration e = set.elements(); e.hasMoreElements(); )
    {
      addElement( e.nextElement() );
    }
  }
  
  /**
   * Retrievs the integer value at a certain index in the vector
   * 
   * Note: no protection is in place for invalid indexes, so the application should 
   * handle this, by using size() to determine the size of the vector
   * 
   * @param n      The index whose value is to be retrieved
   * @return The integer value at index n
   */
  public Integer getInteger( int n ) { return (Integer)elementAt( n ); }
  
  /**
   * Dumps the string representation of the IntegerSet to the output stream
   * 
   * @param os     The output stream
   * @exception IOException
   */
  public void dump( OutputStream os )
    throws IOException
  {
    os.write( toString().getBytes() );
  }

  /**
   * Generates a string representation of the IntegerVector object.
   * The format is "{1,2,3}".
   * 
   * @return The string representation of the vector
   */
  public String toString()
  {
    String str = new String();

    str += "{";

    boolean b = true;

    for( int n = 0; n < size(); n++ )
    {
      if( !b )
        str += ",";
      else
        b=false;
      str += elementAt( n ).toString();
    }
    str += "}";

    return str;
  }
}