/** Class containing only a string for performing custom hash functions.
 *
 * @author Jerod Weinman
 * Grinnell College
 * 20 March 2009
 */
public class HashedString 
{
  /** The string */
  private String s;

  /** Construct a HashedString from a String object */
  public HashedString(String s)
  {
    this.s = s;
  }

  /** Get the string */
  public String toString()
  {
    return s;
  }

  /** Test whether this equals another object */
  public boolean equals(Object o)
  {
    /* If not the same type [not an inheritance safe check]
       or it is null [s can't be null] they are not equal */
    if (!(o instanceof HashedString) || o==null)
      return false;
    
    return s.equals(((HashedString)o).s);
  }

  /** Custom defined hashing function */
  public int hashCode()
  {

    int hash = 0;
    
    /* Declare and store string-length outside of loop for efficiency */
    int len = s.length(); 

    for (int i=0 ; i<len ; i++)
    {
      hash = 31*hash + s.charAt(i);
    }

    return hash;
    
  }
}

