/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.util;

/**
 * A utility class used by <CODE>International</CODE> that stores a country code and name pair.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 * @see International
 */
public final class Country implements Comparable
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_code;     // the country code
  private String m_name;     // the country name

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * Constructs a new <CODE>Country</CODE> object.
   *
   * @param code The country code.
   * @param name The country name.
   */
  Country(String code, String name)
  {
    m_code = code.trim().toUpperCase();
    m_name = name.trim();

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  /**
   * Indicates whether some other object is "equal to" this one.
   *
   * @param o The reference object with which to compare.
   * @return <CODE>true</CODE> if this object is the same as the <CODE>o</CODE> argument; <CODE>false</CODE> otherwise.
   */
  public boolean equals(Object o)
  {
    if ((o==null) || !(o instanceof Country))
      return false;
    if (this==(Country)o)
      return true;
    Country other = (Country)o;
    return m_code.equals(other.m_code);

  } // end equals

  /**
   * Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those
   * provided by <CODE>java.util.Hashtable</CODE>.
   *
   * @return A hash code value for this object.
   */
  public int hashCode()
  {
    return m_code.hashCode();

  } // end hashCode

  /**
   * Returns a string representation of the object. In general, the <CODE>toString</CODE> method returns a string
   * that "textually represents" this object. 
   *
   * @return A string representation of the object.
   */
  public String toString()
  {
    return m_name + " [" + m_code + "]";

  } // end toString

  /*--------------------------------------------------------------------------------
   * Implementations from interface Comparable
   *--------------------------------------------------------------------------------
   */

  /**
   * Compares this object with the specified object for order. Returns a negative integer, zero, or a positive
   * integer as this object is less than, equal to, or greater than the specified object.  <CODE>Country</CODE>
   * objects are compared on their country name.
   *
   * @param o The Object to be compared.
   * @return A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than
   *         the specified object.
   * @exception java.lang.ClassCastException If the specified object's type prevents it from being compared to
   *            this Object.
   */
  public int compareTo(Object o)
  {
    if (o==null)
      return 1;
    if (o instanceof Country)
    { // compare country names
      Country c = (Country)o;
      return m_name.compareTo(c.m_name);

    } // end if

    return m_name.compareTo(o.toString());

  } // end compareTo

  /*--------------------------------------------------------------------------------
   * External getters
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the 2-letter country code for this country.
   *
   * @return The 2-letter country code for this country.
   */
  public final String getCode()
  {
    return m_code;

  } // end code

  /**
   * Returns the name of this country.
   *
   * @return The name of this country.
   */
  public final String getName()
  {
    return m_name;

  } // end name

} // end class Country
