import Comparator; /** * Compares two objects by considering their string representations. * Does case-insensitive comparision. Particularly useful for * comparing strings, but can also be used for other objects. * * @author Samuel A. Rebelsky * @version 1.1 of October 1999 */ public class StringComparator implements Comparator { /** * Determines if the string reperesentation of the first object * is the same as the string representation of the second object. * Pre: The two objects are initialized and can be converted to strings. * Post: Returns true if they are equal and false otherwise. */ public boolean equals(Object first, Object second) { String firstString = first.toString().toLowerCase(); String secondString = second.toString().toLowerCase(); return firstString.equals(secondString); } // equals(Object,Object) /* Determines if the string representation of the first object * is less than the string representation of the second object. * One string is "less than" another iff letters 0 through i-1 * of the two strings are the same and either (i) letter i of the * first string is less than letter i of the second string or (ii) * the first string has only i letters and the second string has more * than i letters. */ public boolean lessThan(Object first, Object second) { String firstString = first.toString().toLowerCase(); String secondString = second.toString().toLowerCase(); // The compareTo operator returns a negative value if // the first string precedes the second. return firstString.compareTo(secondString) < 0; } // lessThan(Object,Object) } // class StringComparator