package rpi.goldsd.container;

/**
 * The <tt>Bool</tt> class provides a wrapper-class for the primitive data
 * type <tt>boolean</tt>.  The actual boolean value (the <tt>boolean</tt>
 * instance variable) is a public instance variable.
 *
 * @see Comparable
 * @see Hashable
 * @version 1.0, 4/16/98
 * @author David Goldschmidt
 */
public class Bool implements Hashable
{
  /**
   * Initializes the underlying <tt>boolean</tt> primitive data type.
   * @param v the initial boolean value.
   */
  public Bool( boolean v )  { value = v; }


  /** Constructs a <tt>Bool</tt> object with default value <tt>false</tt>. */
  public Bool()  { value = false; }


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


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


  /**
   * Returns the hash value of this <tt>Bool</tt> object, which is simply
   * either 0 (<tt>false</tt>) or 1 (<tt>true</tt>).  This is fairly useless,
   * but included for completeness.
   * @return the hash value of this <tt>Bool</tt> object.
   */
  public int hash()  { return ( value == true ? 1 : 0 ); }


  /**
   * Returns the hash value of this <tt>Bool</tt> object, based on the
   * given hashtable size.  The hash value is simply either 0
   * (<tt>false</tt>) or 1 (<tt>true</tt>).  This is fairly useless,
   * but included for completeness.
   * @param tableSize the size of the hashtable making use of
   *                  this <tt>Bool</tt> object.
   * @return the hash value of this <tt>Bool</tt> object.
   */
  public int hash( int tableSize )
  {
    return ( Math.abs( hash() ) % tableSize );
  }


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


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

