import Comparator;

/**
 * Compares two objects by considering their values as integers.
 * The objects should be Integers, Smarts that can be transformed
 * to Integers, or ....
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of October 1999
 */
public class SmartIntegerComparator
  implements Comparator
{
  /**
   * Determines if the integer corresponding to the first object 
   * is the same as the integer corresponding to the second object.
   * Pre: The two objects are initialized and can be converted to integers.
   * Post: Returns true if they are equal and false otherwise.
   *
   * @exception IncomparableException
   *   If either or both canot be converted to integers.
   */
  public boolean equals(Object first, Object second)
    throws IncomparableException
  {
    // An error message.  Becomes something other than the
    // empty string if there is an error.
    String error = "";
    // The integers that correspond to the two parameters.
    // Initialized to keep Java happy.
    int firstInt = 0;
    int secondInt = 0;
    // Try to get the first integer.
    try { firstInt = Integer.parseInt(first.toString()); }
    catch (Exception e) {
      error = error + "'" + first.toString() + "' is not an integer. ";
    }
    // Try to get the second integer.
    try { secondInt = Integer.parseInt(second.toString()); }
    catch (Exception e) {
      error = error + "'" + second.toString() + "' is not an integer. ";
    }
    // If we've hit an error, tell the user.
    if (!error.equals("")) {
      throw new IncomparableException(error);
    }
    // Otherwise, do the normal comparison. 
    return firstInt == secondInt;
  } // equals(Object,Object)

  /* Determines if the integer corresonding to the first object
   * is less than the integer corresponding to the second object.
   * Pre: The objects must be convertable to integers.
   * Post: Return true if the first precedes the second and
   *   false otherwise.
   *
   * @exception IncomparableException
   *   If either or both cannot be converted to integers.
   */
  public boolean lessThan(Object first, Object second)
    throws IncomparableException
  {
    // An error message.  Becomes something other than the
    // empty string if there is an error.
    String error = "";
    // The integers that correspond to the two parameters.
    // Initialized to keep Java happy.
    int firstInt = 0;
    int secondInt = 0;
    // Try to get the first integer.
    try { firstInt = Integer.parseInt(first.toString()); }
    catch (Exception e) {
      error = error + "'" + first.toString() + "' is not an integer. ";
    }
    // Try to get the second integer.
    try { secondInt = Integer.parseInt(second.toString()); }
    catch (Exception e) {
      error = error + "'" + second.toString() + "' is not an integer. ";
    }
    // If we've hit an error, tell the user.
    if (!error.equals("")) {
      throw new IncomparableException(error);
    }
    // Otherwise, do the normal comparison. 
    return firstInt < secondInt;
  } // lessThan(Object,Object)
} // class SmartIntegerComparator
