package rpi.goldsd.container;

/**
 * The <tt>Int</tt> class provides a wrapper-class for the primitive data
 * type <tt>int</tt> that may be used in various associative containers
 * by implementing the <tt>Hashable</tt> interface (note that the
 * <tt>Hashable</tt> interface is actually an extension of the
 * <tt>Comparable</tt> interface).  The actual integer value
 * (the <tt>int</tt> instance variable) is a public instance variable.
 *
 * @see Comparable
 * @see Hashable
 * @version 1.2, 4/16/98
 * @author David Goldschmidt
 */
public class Int implements Hashable
{
  /** The smallest value of type <tt>Int</tt>. */
  public final static int MIN_VALUE = Integer.MIN_VALUE;


  /** The largest value of type <tt>Int</tt>. */
  public final static int MAX_VALUE = Integer.MAX_VALUE;


  /**
   * Initializes the underlying <tt>int</tt> primitive data type.
   * @param v the initial integer value.
   */
  public Int( int v )  { value = v; }


  /** Constructs an <tt>Int</tt> object with a default value of 0. */
  public Int()  { value = 0; }


  /**
   * Returns <tt>true</tt> if this <tt>Int</tt> object is equal to the
   * given <tt>Comparable</tt> argument <tt>C</tt>, which must be of
   * type <tt>Int</tt>.
   * @param C the right-hand side of the comparison.
   * @return <tt>true</tt> if this <tt>Int</tt> object is equal to the
   *         given <tt>Int</tt> object <tt>C</tt>; <tt>false</tt> otherwise.
   * @exception IllegalArgumentException if the <tt>Comparable</tt> argument
   *                                     is not of type <tt>Int</tt>.
   */
  public boolean isEqualTo( Comparable C ) throws IllegalArgumentException
  {
    if ( C instanceof Int )
      return ( value == ((Int)C).value );
    else
      throw new IllegalArgumentException( "Must be an Int type." );
  }


  /**
   * Returns <tt>true</tt> if this <tt>Int</tt> object is less than the
   * given <tt>Comparable</tt> argument <tt>C</tt>, which must be of type
   * <tt>Int</tt>.
   * @param C the right-hand side of the comparison.
   * @return <tt>true</tt> if this <tt>Int</tt> object is less than the
   *         given <tt>Int</tt> object <tt>C</tt>; <tt>false</tt> otherwise.
   * @exception IllegalArgumentException if the <tt>Comparable</tt> argument
   *                                     is not of type <tt>Int</tt>.
   */
  public boolean isLessThan( Comparable C ) throws IllegalArgumentException
  {
    if ( C instanceof Int )
      return ( value < ((Int)C).value );
    else
      throw new IllegalArgumentException( "Must be an Int type." );
  }


  /**
   * The static <tt>isPrime()</tt> method is used to determine if
   * a given <tt>Int</tt> object is a prime number.
   * @param I the <tt>Int</tt> object to be tested.
   * @return <tt>true</tt> if the given <tt>Int</tt> object is prime;
   *         <tt>false</tt> otherwise.
   */
  public static final boolean isPrime( Int I )
  {
    return isPrime( I.value );
  }


  /**
   * The static <tt>isPrime()</tt> method is used to determine if
   * a given <tt>int</tt> value is a prime number.
   * @param i the <tt>int</tt> value to be tested.
   * @return <tt>true</tt> if the given <tt>int</tt> value is prime;
   *         <tt>false</tt> otherwise.
   */
  public static final boolean isPrime( int i )
  {
    if ( i == 2 ) return true;
    if ( i < 2 || i % 2 == 0 ) return false;
    for ( int stop = i, j = 3 ; j < stop ; stop = i / j, j += 2 )
      if ( i % j == 0 ) return false;
    return true;
  }


  /**
   * Returns the hash value of this <tt>Int</tt> object, which is simply
   * the underlying integer value.  If a more complex hashing function is
   * required, this method may be overridden.
   * @return the hash value of this <tt>Int</tt> object.
   */
  public int hash()  { return value; }


  /**
   * Returns the hash value of this <tt>Int</tt> object, based on the
   * given hashtable size.
   * @param tableSize the size of the hashtable making use of
   *                  this <tt>Int</tt> object.
   * @return the hash value of this <tt>Int</tt> object.
   */
  public int hash( int tableSize )
  {
    return ( Math.abs( hash() ) % tableSize );
  }


  /**
   * Displays this <tt>Int</tt> object in the form of a <tt>String</tt>
   * object.  This method simply returns a <tt>String</tt> representation
   * of the underlying integer value.
   * @return a <tt>String</tt> representing this <tt>Int</tt> object.
   */
  public String toString()  { return ( "" + value ); }


  /** The underlying primitive data type. */
  public int value;
}

