package rpi.goldsd.container;

/**
 * The <tt>Str</tt> class provides a wrapper-class for the <tt>String</tt>
 * class 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
 * <tt>String</tt> object is a public instance variable.
 *
 * @see Comparable
 * @see Hashable
 * @version 1.0, 3/29/98
 * @author David Goldschmidt
 */
public class Str implements Hashable
{
  /**
   * Initializes the underlying <tt>String</tt> object.
   * @param s the initial <tt>String</tt> object.
   */
  public Str( String s )  { string = s; }


  /** Constructs a <tt>Str</tt> object with a default value of "". */
  public Str()  { string = ""; }


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


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


  /**
   * Returns the hash value of this <tt>Str</tt> object, which is simply
   * the hash value of the underlying <tt>String</tt> object.
   * @return the hash value of this <tt>Str</tt> object.
   */
  public int hash()  { return string.hashCode(); }


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


  /**
   * Displays this <tt>Str</tt> object in the form of a <tt>String</tt> object.
   * This method simply returns a <tt>String</tt> representation of the
   * underlying <tt>String</tt> object.
   * @return a <tt>String</tt> respresenting this <tt>Str</tt> object.
   */
  public String toString()  { return ( string ); }


  /** The underlying <tt>String</tt> object. */
  public String string;
}

